blob: 5f339dfe3de363039ef7180a3e3562ccd14964b4 [file] [log] [blame]
Brett Chabot09a17ad2012-04-03 15:20:01 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.tradefed.config;
18
Brett Chabotc40859d2014-03-07 11:19:48 -080019import com.android.tradefed.log.LogUtil.CLog;
20
Brett Chabot09a17ad2012-04-03 15:20:01 -070021import java.lang.reflect.Field;
22import java.util.Collection;
23import java.util.HashMap;
24import java.util.Map;
25
26/**
27 * A helper class that can copy {@link Option} field values with same names from one object to
28 * another.
29 */
30public class OptionCopier {
31
32 /**
33 * Copy the values from {@link Option} fields in <var>origObject</var> to <var>destObject</var>
34 *
35 * @param origObject the {@link Object} to copy from
36 * @param destObject the {@link Object} tp copy to
37 * @throws ConfigurationException if options failed to copy
38 */
39 public static void copyOptions(Object origObject, Object destObject)
40 throws ConfigurationException {
41 Collection<Field> origFields = OptionSetter.getOptionFieldsForClass(origObject.getClass());
42 Map<String, Field> destFieldMap = getFieldOptionMap(destObject);
43 for (Field origField : origFields) {
44 final Option option = origField.getAnnotation(Option.class);
45 Field destField = destFieldMap.remove(option.name());
46 if (destField != null) {
47 Object origValue = OptionSetter.getFieldValue(origField,
48 origObject);
49 OptionSetter.setFieldValue(option.name(), destObject, destField, origValue);
50 }
51 }
52 }
53
54 /**
Brett Chabotc40859d2014-03-07 11:19:48 -080055 * Identical to {@link #copyOptions(Object, Object)} but will log instead of throw if exception
56 * occurs.
57 */
58 public static void copyOptionsNoThrow(Object source, Object dest) {
59 try {
60 copyOptions(source, dest);
61 } catch (ConfigurationException e) {
62 CLog.e(e);
63 }
64 }
65
66 /**
Julien Desprezdb506662019-07-26 09:04:39 -070067 * Copy the given option from {@link Option} fields in <var>origObject</var> to
68 * <var>destObject</var>
69 *
70 * @param origObject the {@link Object} to copy from
71 * @param destObject the {@link Object} tp copy to
72 * @param optionName the name of the option to copy.
73 * @throws ConfigurationException if options failed to copy
74 */
75 public static void copyOptions(Object origObject, Object destObject, String optionName)
76 throws ConfigurationException {
77 Collection<Field> origFields = OptionSetter.getOptionFieldsForClass(origObject.getClass());
78 Map<String, Field> destFieldMap = getFieldOptionMap(destObject);
79 for (Field origField : origFields) {
80 final Option option = origField.getAnnotation(Option.class);
81 if (option.name().equals(optionName)) {
82 Field destField = destFieldMap.remove(option.name());
83 if (destField != null) {
84 Object origValue = OptionSetter.getFieldValue(origField, origObject);
85 OptionSetter.setFieldValue(option.name(), destObject, destField, origValue);
86 }
87 }
88 }
89 }
90
91 /**
92 * Identical to {@link #copyOptions(Object, Object, String)} but will log instead of throw if
93 * exception occurs.
94 */
95 public static void copyOptionsNoThrow(Object source, Object dest, String optionName) {
96 try {
97 copyOptions(source, dest, optionName);
98 } catch (ConfigurationException e) {
99 CLog.e(e);
100 }
101 }
102
103 /**
Brett Chabot09a17ad2012-04-03 15:20:01 -0700104 * Build a map of {@link Option#name()} to {@link Field} for given {@link Object}.
105 *
106 * @param destObject
107 * @return a {@link Map}
108 */
109 private static Map<String, Field> getFieldOptionMap(Object destObject) {
110 Collection<Field> destFields = OptionSetter.getOptionFieldsForClass(destObject.getClass());
111 Map<String, Field> fieldMap = new HashMap<String, Field>(destFields.size());
112 for (Field field : destFields) {
113 Option o = field.getAnnotation(Option.class);
114 fieldMap.put(o.name(), field);
115 }
116 return fieldMap;
117 }
118}