From dawid.weiss at gmail.com Thu Nov 24 14:34:09 2011 From: dawid.weiss at gmail.com (Dawid Weiss) Date: Thu, 24 Nov 2011 23:34:09 +0100 Subject: @Retention(Runtime) annotations silently omitted if not in classpath? Message-ID: 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/254afe79/attachment.html From dawid.weiss at gmail.com Thu Nov 24 14:47:49 2011 From: dawid.weiss at gmail.com (Dawid Weiss) Date: Thu, 24 Nov 2011 23:47:49 +0100 Subject: @Retention(Runtime) annotations silently omitted if not in classpath? In-Reply-To: References: Message-ID: 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 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 From mernst at cs.washington.edu Sat Nov 26 13:17:35 2011 From: mernst at cs.washington.edu (Michael Ernst) Date: Sat, 26 Nov 2011 13:17:35 -0800 (PST) Subject: @Retention(Runtime) annotations silently omitted if not in classpath? In-Reply-To: References: Message-ID: <20111126.131735.294085338.mernst@cs.washington.edu> Dawid- Thanks for your messages. This seems like a problem that should be reported. However, this mailing list (type-annotations-dev at openjdk.java.net) is intended for discussions of type annotations (http://types.cs.washington.edu/jsr308/), not for all issues related to annotations. So, unfortunately, we can't help you here. Good luck! -Mike