簡體   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