[英]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.