@Retention(Runtime) annotations silently omitted if not in classpath?
Dawid Weiss
dawid.weiss at gmail.com
Thu Nov 24 14:47:49 PST 2011
A quick follow-up: I could also get a nice internal exception by declaring:
@Buhu({Tainted.class})
public void abc()
and then calling getAnnotations() on method "abc" with Tainted.class
missing from classpath. The result is:
Exception in thread "Main Thread" java.lang.ArrayStoreException: Cannot
store sun.reflect.annotation.TypeNotPresentExceptionProxy in
[Ljava.lang.Class;
at
sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:653)
at
sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:460)
at
sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:286)
at
sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:222)
at
sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
at
sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
at java.lang.reflect.Method.declaredAnnotations(Method.java:693)
at java.lang.reflect.Method.getDeclaredAnnotations(Method.java:686)
at
java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:175)
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Dawid
On Thu, Nov 24, 2011 at 11:34 PM, Dawid Weiss <dawid.weiss at gmail.com> wrote:
>
> Hi everyone,
>
> I just discovered that Method#getDeclaredAnnotations() may or may not
> return an annotation with @Retention(RetentionPolicy.RUNTIME). Basically
> what happens is if the annotation class is present in classpath, it is
> returned. But if it isn't present in classpath, the returned array simply
> omits the missing annotation.
>
> I looked at the JLS and jsr-202 (class file format) but I can't find any
> fragment relevant to this -- where are the rules governing this? I found
> bits and pieces in the following places:
> * AnnotatedElement (TypeNotPresentException thrown if an annotation
> references a Class explicitly, not the case here),
> * OpenJDK's AnnotationParser source code -- there is clearly a distinction
> in the code between throwing/ not throwing a TypeNotPresentException, but
> no specific pointers to the rules.
>
> I would appreciate if somebody shared a link or info to where the rules of
> throw/not throw are explained. Thanks in advance,
> Dawid
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/type-annotations-dev/attachments/20111124/64f15d68/attachment.html
More information about the type-annotations-dev
mailing list