簡體   English   中英

如何在代理 class 上獲取方法注釋?

[英]How to get method annotations on a proxied class?

我在讀取代理 class 方法的注釋時遇到問題。

有一個接口,一個object和一個方法上的注解,這部分真的很簡單:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface A {
}

interface I {
    void method();
}

class Test implements I {
    @A
    public void method() { }
}

接下來,有一個InvocationHandler什么都不做,只是簡單地調用傳遞了 arguments 的方法:

class DefaultInvocationHandler implements InvocationHandler {

    @Override
    public Object invoke(final Object o, final Method method, final Object[] args) throws Throwable {
        return method.invoke(o, args);
    }
}

並且有一個main方法可以打印Test實例的聲明方法及其代理副本:

class Main {
    public static void main(String[] args) {
        Object test = new Test();
        printMethods(test);         // Outputs that `I#method` has `A` annotation

        System.out.println();

        Object proxied = Proxy.newProxyInstance(test.getClass().getClassLoader(), test.getClass().getInterfaces(), new DefaultInvocationHandler());
        printMethods(proxied);      // Outputs that `I#method` does not have `A` annotation
    }

    static void printMethods(Object obj) {
        Arrays.stream(obj.getClass().getDeclaredMethods())
                .forEach(method -> System.out.println(method.toString() + " has A annotation: " + method.isAnnotationPresent(A.class)));
    }
}

問題來了:局部變量test has 是Test class 的一個實例,而局部變量proxied實際上是一個Proxy ,所以它的方法上沒有任何注釋。 這是程序的output:

public void Test.method() has A annotation: true                // <- good thing

public final boolean $Proxy2.equals(java.lang.Object) has A annotation: false
public final java.lang.String $Proxy2.toString() has A annotation: false
public final void $Proxy2.method() has A annotation: false      // <-  bad thing
public final int $Proxy2.hashCode() has A annotation: false

我試過尋找解決方案,但這個問題是關於從注釋中提取注釋(我想), 這個問題也與注釋 class 有關。 其中一些是關於其他代理實現的。

➥ 那么,有什么方法可以從代理的 object 中獲取實際注釋,或者暴露隱藏在代理下的 class(我想要前一個)?

那么,有什么方法可以從代理的 object 中獲取實際注釋,或者公開隱藏在代理下的 class(不過我想要前一個)?

不直接,不。

Proxy設計背后的想法是,實際包裝的實例(如果有的話)將隱藏在調用處理程序后面。 您可以從newProxyInstance方法中看到這一點:沒有對任何地方傳遞的test實例的引用。 Proxy實例不知道您的Test實例。

一種常見的模式是使用一個常見的InvocationHandler子類,該子類保留對包裝實例的引用並將其返回給您,您可以使用它來執行檢查。 例如,

abstract class InvocationHandlerWithTarget implements InvocationHandler {
    protected final Object target;

    public InvocationHandlerWithTarget(Object target) {
        this.target = target;
    }

    public Object getTarget() {
        return target;
    }
}

class DefaultInvocationHandler extends  InvocationHandlerWithTarget {
    public DefaultInvocationHandler(Object target) {
        super(target);
    }

    @Override
    public Object invoke(final Object o, final Method method, final Object[] args) throws Throwable {
        return method.invoke(target, args);
    }
}

然后檢查您是否正在使用Proxy以及它的InvocationHandler是否是您所期望的

Object proxied = Proxy.newProxyInstance(test.getClass().getClassLoader(), test.getClass().getInterfaces(),
            new DefaultInvocationHandler(test));

[...]

if (Proxy.isProxyClass(proxied.getClass())) {
    var handler = Proxy.getInvocationHandler(proxied);
    if (handler instanceof InvocationHandlerWithTarget) {
        var handlerWithTarget = (InvocationHandlerWithTarget) handler;

        // now process the target
        handlerWithTarget.getTarget();
    }
}

然后,您有一個具體的實例來反映(或您需要做的任何其他處理)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM