繁体   English   中英

是否可以将非最终变量传递给匿名类的方法?

[英]Is it possible to pass a non-final variable to a method of an anonymous class?

我需要将代码发送到另一个方法,因为类中方法的数量可能会发生变化,所以我使用反射。 但是问题是我无法在循环中枚举它们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);
}

我想我会补充一点,我有一个技巧可以将非最终变量传递给匿名类。 采取您上面编写的代码的一部分,我做了一些小的更改以显示如何执行。

我在这里假设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
    }
}

...

一种方法可能是避免匿名内部类。 即有一个类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语句,该语句允许将变量声明为final:

for (final Method method : benchmarkMethods) {
    ...
    MeasureAndRecord(name, new IFunc() {
        @Override
        public Object onEvent() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
            return method.invoke(set, (Object[]) null);
        }
    });

}

您可以使用可变的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);
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM