I want to get annotations from annotation, but the weird thing is I can't get single annotation from annotation instance. How do I solve this? I want to get annotations from this annotation instance.
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);
}
}
print result is:
ValidBoolean annotation len:3
com.sun.proxy.$Proxy28:0
[BOOLEAN]com.sun.proxy.$Proxy28:0
I believe you can do
Please check
in java.lang.annotation.Annotation
If I understand you correctly, you want to do
Generally,
annotationType()
works such thatsomeClass.getAnnotation(someAnnotationClass).annotationType()==someAnnotationClass
.So
annotation instanceof ValidBoolean
also impliesannotation.annotationType() == ValidBoolean.class
, thus invoking.getAnnotations()
on them will lead to the same annotations.It looks like annotation is a proxy object