gpt4 book ai didi

java - 如何在代理类上获取方法注释?

转载 作者:行者123 更新时间:2023-11-30 12:02:14 27 4
gpt4 key购买 nike

我在读取代理类方法的注解时遇到问题。

有一个接口(interface),一个对象,一个方法的注解,这部分很简单:

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

interface I {
void method();
}

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

接下来,有一个什么都不做的 InvocationHandler,只是简单地调用传递参数的方法:

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类的一个实例,而局部变量proxied实际上是一个Proxy,所以它的方法没有任何注解。这是程序的输出:

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

我尝试寻找解决方案,但 this question是关于从注释中提取注释(我假设),this one太关于注解类了。 Some of them是关于其他代理实现的。

➥ 那么,有没有办法从代理对象中获取实际注释,或者公开隐藏在代理下的类(不过我想要前者)?

最佳答案

So, is there any way to get actual annotations off a proxied object, or to expose the class that is hidden under the proxy (I want the former one, though)?

不直接,不。

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();
}
}

然后您有一个具体的实例来反射(reflection)(或您需要做的任何其他处理)。

关于java - 如何在代理类上获取方法注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58750665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com