AnnotationFormatError vs. IllegalArgumentException for invalid annotation interface members (methods)

Peter Levart peter.levart at gmail.com
Sat Jan 7 08:26:30 UTC 2017


Hi Joe,

While investigating the fix for:

     https://bugs.openjdk.java.net/browse/JDK-8029019

I encountered what I believe is an inconsistency in exception reporting. 
The following jtreg test:


public class AnnotationVerifier {

     @AnnotationWithParameter
     @AnnotationWithVoidReturn
     static class BadAnnotation {
     }

     @Test
     @ExpectedExceptions(IllegalArgumentException.class)
     public void annotationValidationIAE() {
         AnnotationType.getInstance(AnnotationWithParameter.class);
     }

     @Test(expectedExceptions = AnnotationFormatError.class)
     public void annotationValidationAFE() {
BadAnnotation.class.getAnnotation(AnnotationWithVoidReturn.class);
     }
}


...treats methods in an annotation interface differently when they have 
a parameter (it throws IllegalArgumentException because parameter counts 
are checked in AnnotationType constructor) vs. when they have invalid 
return type such as void (it throws AnnotationFormatError when the 
member's default value is retrieved in AnnotationType constructor, but 
AnnotationType does not explicitly check for member return types). I 
believe both situations should result in AnnotationFormatError when such 
annotation is requested.

I'm thinking of moving the validateAnnotationMethods() method from 
AnnotationInvocationHandler where methods are being checked for each 
annotation instance constructed to AnnotationType, where they would be 
checked only once per annotation type and make exception reporting 
consistent for any invalid situation.

What do you think?

Regards, Peter



More information about the core-libs-dev mailing list