我需要将代码发送到另一个方法,因为类中的方法数量可能会改变,所以我使用反射。但问题是,我不能在循环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);
}发布于 2013-07-29 09:53:08
一种方法可以是避免匿名内部类。即,有一个类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中有一个双迭代,我假设这不是实际的代码)可以使用允许变量被声明为最终的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);
}
});
}发布于 2014-02-25 11:58:18
我想补充一下,我用了一个技巧把非最终变量传递到一个匿名类中。以您上面所写代码的一部分为例,我做了一些小小的更改,以说明我是如何做到的。
这里我假设measureAndRecord是一种方法(我已经将第一个字符标记为这个方法),而IFunc是您正在创建匿名扩展的类。我还假设Method method ...应该是您希望传递给匿名类的方法(因为在您的代码中它什么都不做)。
这里的诀窍是添加一个名为init的新方法,您可以将变量传递给(最终的或非最终的)并返回this (在本例中是匿名类)。所发生的情况是调用匿名类,立即调用init方法,然后返回并使用对象(根据需要是IFunc )。
必须在构造时(在赋值之前)调用init方法,而不能以这种方式将链方法放在一起。原因是,一旦对象从第一个方法调用中返回,它就是一个IFunc,而不是--您创建的匿名子类,所以Java将不再识别任何东西,只会识别之后的覆盖方法。
不管怎么说,玩玩吧。我发现它非常非常方便,如果有点欺骗。即使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
}
}
...发布于 2013-07-29 10:14:24
您可以使用可变的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);
}
});
}https://stackoverflow.com/questions/17920447
复制相似问题