1 /* 2 * Copyright (C) 2019 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.device.metric; 18 19 import com.android.tradefed.config.OptionCopier; 20 import com.android.tradefed.testtype.IRemoteTest; 21 22 import java.lang.reflect.InvocationTargetException; 23 import java.util.ArrayList; 24 import java.util.List; 25 26 /** Helper to do some {@link IMetricCollector} operations needed in several places. */ 27 public class CollectorHelper { 28 29 /** 30 * Helper to clone {@link IMetricCollector}s in order for each {@link IRemoteTest} to get a 31 * different instance, and avoid internal state and multi-init issues. 32 * 33 * @param originalCollectors the list of original collectors to be cloned. 34 * @return The list of cloned {@link IMetricCollector}. 35 */ cloneCollectors( List<IMetricCollector> originalCollectors)36 public static List<IMetricCollector> cloneCollectors( 37 List<IMetricCollector> originalCollectors) { 38 List<IMetricCollector> cloneList = new ArrayList<>(); 39 if (originalCollectors == null) { 40 return cloneList; 41 } 42 for (IMetricCollector collector : originalCollectors) { 43 try { 44 // TF object should all have a constructore with no args, so this should be safe. 45 IMetricCollector clone = 46 collector.getClass().getDeclaredConstructor().newInstance(); 47 OptionCopier.copyOptionsNoThrow(collector, clone); 48 cloneList.add(clone); 49 } catch (InstantiationException 50 | IllegalAccessException 51 | InvocationTargetException 52 | NoSuchMethodException e) { 53 throw new RuntimeException(e); 54 } 55 } 56 return cloneList; 57 } 58 } 59