• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      * Build a map of {@link Option#name()} to {@link Field} for given {@link Object}.
68      *
69      * @param destObject
70      * @return a {@link Map}
71      */
getFieldOptionMap(Object destObject)72     private static Map<String, Field> getFieldOptionMap(Object destObject) {
73         Collection<Field> destFields = OptionSetter.getOptionFieldsForClass(destObject.getClass());
74         Map<String, Field> fieldMap = new HashMap<String, Field>(destFields.size());
75         for (Field field : destFields) {
76             Option o = field.getAnnotation(Option.class);
77             fieldMap.put(o.name(), field);
78         }
79         return fieldMap;
80     }
81 }
82