我需要将代码发送到另一个方法,因为类中的方法数量可能会改变,我使用反射。但问题是我无法在循环中枚举它们benchmarkMethods.get(i).invoke(set, null);
,因为对我来说匿名类可能只能传递最终变量。在这种情况下我该怎么办?
public void RunAllBenchmarks(final Object set, boolean randomOrder) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
final List<Method> benchmarkMethods = new ArrayList<Method >();
final Class benchClass = set.getClass();
Method[] methods = benchClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++){
Annotation[] annotations = methods[i].getAnnotations();
if (annotations.length != 0){
for (int j = 0; j < annotations.length; j++)
if (annotations[j].annotationType().toString().contentEquals("interface Benchmark"))
benchmarkMethods.add(methods[i]);
}
}
for (int i = 0; i < benchmarkMethods.size(); i++){
String name = null;
Method method = benchmarkMethods.listIterator().next();
MeasureAndRecord(name, new IFunc() {
@Override
public Object onEvent() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
return benchmarkMethods.get(i).invoke(set, null);
}
});
}
PrintWriter writer = new PrintWriter(System.out);
PrintResults(writer);
}