1 /*
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 
17 package com.android.tradefed.config;
18 
19 import com.android.tradefed.log.LogUtil.CLog;
20 
21 import java.lang.reflect.Field;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import 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  */
30 public 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      */
copyOptions(Object origObject, Object destObject)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     /**
55      * Identical to {@link #copyOptions(Object, Object)} but will log instead of throw if exception
56      * occurs.
57      */
copyOptionsNoThrow(Object source, Object dest)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     /**
67      * 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      */
copyOptions(Object origObject, Object destObject, String optionName)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      */
copyOptionsNoThrow(Object source, Object dest, String optionName)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     /**
104      * Build a map of {@link Option#name()} to {@link Field} for given {@link Object}.
105      *
106      * @param destObject
107      * @return a {@link Map}
108      */
getFieldOptionMap(Object destObject)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 }
119