A simpler model for repeating annotations
Alex Buckley
alex.buckley at oracle.com
Fri Dec 28 15:56:10 PST 2012
Broadly speaking, all the rules in 8misc.pdf section 9.7 - about combinations of base and container annotations - still apply. In particular, it's perfectly legal to have @Foo @FooContainer(...) together. Mike Keith's comments revealed that EE frameworks that read annotations take considerable care over this case. So, I would expect get[Declared]Annotations(Class) to return contained annotations along with any singular base annotation present. See 8misc.pdf example 1.2-3:
@Foo(0) @FooContainer({@Foo(1), at Foo(2)}) class A {}
A.class.get[Declared]Annotations(Foo.class) = [ @Foo(0), @Foo(1), @Foo(2) ]
A.class.get[Declared]Annotations(FooContainer.class) = [ @FooContainer(...) ]
Alex
----- Original Message -----
This spec is way better,
to be sure, do you agree that getDeclaredAnnotations can be written like
this:
A[] getDeclaredAnnotations(Class<A> annotationClass) {
Repeatable repeatable =
annotationClass.getDeclaredAnnotation(Repetable.class);
if (repeatable != null) {
Annotation container = getDeclaredAnnotation(repeatable.value());
if (container != null) { // if there is a container annotation,
no single annotation can be present ?
return (A[])callValuesByReflection(container);
}
}
A annotation = getDeclaredAnnotation(annotationClass);
if (annotation == null)
return (A[])Arrays.newInstance(annotationClass, 0)
A[] annotations = (A[])Arrays.newInstance(annotationClass, 1);
annotations[0] = annotation;
return annotations;
}
cheers,
Rémi
More information about the enhanced-metadata-spec-discuss
mailing list