A question about Depth of container annotations (original) (raw)

Joe Darcy joe.darcy at oracle.com
Tue Jun 24 18:23:37 UTC 2014


Hello,

The behavior you're interested in is defined in the AnnotatedElement specification:

http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/AnnotatedElement.html

In brief, none of the methods in AnnotatedElement look through more than one level of containment. (Likewise, when writing repeated annotation in source code, the compiler will only create one level of containers.)

You may be interested in reviewing design discussions of this feature that occurred on

http://mail.openjdk.java.net/mailman/listinfo/enhanced-metadata-spec-discuss

Cheers,

-Joe

On 06/24/2014 01:21 AM, deven you wrote:

Hi All,

I have a question about repeated annotation depth. If this is not the proper mailing list group, please tell me where I should send it to. My question will be about the depth of container annotations. For instance, assume there are 3 annotations. - RepeatedAnn - ContainerAnn - ContainerContainerAnn So, ContainerContainerAnn can have ContainerAnn and that can also have RepeatbleAnn in it. In this case, get*AnnotationsByType(RepeatableAnn) APIs wont return repeteableAnns in depth 2. Java docs don't talk about the depth. I wonder if the get*AnnotationsByType api should return the annotations of all depth? If we have below annotations: @Retention(RetentionPolicy.RUNTIME) @Repeatable(ContainerAnn.class) @interface RepeatableAnn { String value(); } @Inherited @Retention(RetentionPolicy.RUNTIME) @Repeatable(ContainerContainerAnn.class) @interface ContainerAnn { RepeatableAnn[] value(); } @Inherited @Retention(RetentionPolicy.RUNTIME) @interface ContainerContainerAnn { ContainerAnn[] value(); } And the main class is annotated by : @ContainerAnn({@RepeatableAnn("Parent-3")}) @ContainerAnn({@RepeatableAnn("Parent-4")}) public class Test { ... when we call getAnnotationsByType(RepeatableAnn.class) on this class, we get nothing because RepeatableAnn is contained by ContainerAnn which is also contained by ContainerContainerAnn. In other words, RepeatableAnn is in depth 2 and get*AnnotationsByType APIs only searches depth 1. The test results are: /home/deven/jdk820/jdk1.8.020/bin$ java repeated.Test CLASS = repeated.Test getDeclaredAnnotationsByType(RepeatableAnn.class) getDeclaredAnnotationsByType(ContainerAnn.class) @repeated.ContainerAnn(value=[@repeated.RepeatableAnn(value=Parent-3)]) @repeated.ContainerAnn(value=[@repeated.RepeatableAnn(value=Parent-4)]) getDeclaredAnnotationsByType(ContainerContainerAnn.class) @repeated.ContainerContainerAnn(value=[@repeated.ContainerAnn(value=[@repeated.RepeatableAnn(value=Parent-3)]), @repeated.ContainerAnn(value=[@repeated.RepeatableAnn(value=Parent-4)])]) getAnnotationsByType(RepeatableAnn.class) getAnnotationsByType(ContainerAnn.class) @repeated.ContainerAnn(value=[@repeated.RepeatableAnn(value=Parent-3)]) @repeated.ContainerAnn(value=[@repeated.RepeatableAnn(value=Parent-4)]) getAnnotationsByType(ContainerContainerAnn.class) @repeated.ContainerContainerAnn(value=[@repeated.ContainerAnn(value=[@repeated.RepeatableAnn(value=Parent-3)]), @repeated.ContainerAnn(value=[@repeated.RepeatableAnn(value=Parent-4)])])



More information about the core-libs-dev mailing list