我需要将代码发送到另一个方法,因为类中的方法数量可能会改变,我使用反射。但问题是我无法在循环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);
}
答案 0 :(得分:1)
我想我会补充说,我用一种技巧将非最终变量传递给匿名类。以上面编写的代码部分为例,我做了一些小改动来说明我是如何做到的。
我假设measureAndRecord
是一种方法(我已经对第一个字符进行了资本化以表示),而IFunc
是您正在创建匿名扩展名的类。我还假设Method method ...
应该是你要传递给你的匿名类的方法(因为在你的代码中它没有做任何事情)。
这里的技巧是添加一个名为init
的新方法,您可以将变量传递给(final或non-final)并返回this
(在本例中为匿名类) 。发生的是调用匿名类,立即调用init
方法,然后返回并使用对象(根据需要为IFunc)。
必须在构建时(分配前)调用init
方法,并且不能以这种方式将方法链接在一起。原因是一旦从第一个方法调用返回对象,它就是一个IFunc,不你创建的匿名子类,因此Java将不再识别除了之后的覆盖方法。< / p>
无论如何,玩一玩。如果有点作弊,我发现非常方便。即使Java 8出现并允许传递几乎最终的变量,这仍将适用于变量变量。
...
for (int i = 0; i < benchmarkMethods.size(); i++) {
String name = null;
Method method = benchmarkMethods.get(i); // Assumption on my part
measureAndRecord(name, new IFunc() {
// Anonymous class variables matching what we want to pass
Method method;
int i;
// Cheating "Constructor"
public IFunc init (Method method, int i) {
this.method = method;
this.i = i;
return this; // Must return this for assignment
}
@Override
public Object onEvent() throws InvocationTargetException,
IllegalAccessException, NoSuchMethodException {
return method.invoke(set, null);
}
}.init(method, i)); // Note that init is called BEFORE assignment
}
}
...
答案 1 :(得分:0)
一种方法可能是避免使用匿名内部类。即有一个类MethodIFunc implements IFunc
,它在onEvent()
即
for (...) {
Method method = ...
// this class calls method.invoke(..) inside its onEvent() method
MethodIFunc mi = new MethodIFunc(method, set);
MeasureAndRecord(name, mi);
}
另一种方法(顺便说一句:你在第二个for-loop
中有一个双重迭代我假设这不是实际的代码)可以使用允许变量声明为final的for-each
语句:
for (final Method method : benchmarkMethods) {
...
MeasureAndRecord(name, new IFunc() {
@Override
public Object onEvent() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
return method.invoke(set, (Object[]) null);
}
});
}
答案 2 :(得分:0)
您可以使用可变的int包装器,例如
final AtomicInteger ai = new AtomicInteger();
for (int i = 0; i < benchmarkMethods.size(); i++){
String name = null;
Method method = benchmarkMethods.listIterator().next();
ai.set(i);
MeasureAndRecord(name, new IFunc() {
@Override
public Object onEvent() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
return benchmarkMethods.get(ai.get()).invoke(set, null);
}
});
}