简体   繁体   English

Java - 如何从代理类获取注释?

[英]Java - How to get annotations from Proxy class?

I'm not really familiar with proxy class and I have no idea how does this("annotation") became a proxy object.我对代理类不是很熟悉,我不知道 this("annotation") 是如何成为代理对象的。 Can I retrieve annotations from Proxy object?我可以从 Proxy 对象中检索注释吗?

public static void test(Annotation annotation) {
    System.out.println("ValidBoolean annotation len:" + ValidBoolean.class.getAnnotations().length);
    System.out.println(annotation.getClass().getName() + ":" + annotation.getClass().getAnnotations().length);
    if (annotation instanceof ValidBoolean) {
        ValidBoolean validBoolean = (ValidBoolean) annotation;
        System.out.println("[BOOLEAN]" + validBoolean.getClass().getName() + ":" + validBoolean.getClass().getAnnotations().length);
    }
}

the result is:结果是:

ValidBoolean annotation len:3
com.sun.proxy.$Proxy28:0
[BOOLEAN]com.sun.proxy.$Proxy28:0

I'm still not really understanding this Proxy mechanism.我仍然没有真正理解这种代理机制。 But I can get Annotation real class by annotation.annotationType() rather than annotation.getClass() , because annotation is just an interface so annotation.getClass() will only produce a Java Proxy object!但是我可以通过annotation.annotationType()而不是annotation.getClass()来获取 Annotation 真正的类,因为 annotation 只是一个接口,所以annotation.getClass()只会产生一个 Java Proxy 对象!

And it seems, Proxy object doesn't inherit Annotations, this is the most important thing i've learned.看起来,Proxy 对象没有继承 Annotations,这是我学到的最重要的事情。

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

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