RFR JDK-7183985: (ann) Class.getAnnotation() throws an ArrayStoreException when the annotation class not present
Jan Lahoda
jan.lahoda at oracle.com
Tue Oct 8 13:14:29 UTC 2013
Hi,
Please review the fix for bug JDK-7183985, bug:
https://bugs.openjdk.java.net/browse/JDK-7183985
webrev:
http://cr.openjdk.java.net/~jlahoda/7183985/webrev.00/
The problem, as I understand it, is as follows. Consider this code:
-----
@WithArray({E.class})
class O {
}
@Retention(RetentionPolicy.RUNTIME)
@interface WithArray {
Class<?>[] value();
}
class E { }
-----
The code is compiled and is being inspected by reflection at runtime.
Assume the type "E" is not available at that time. At that
point calling:
O.class.getAnnotations();
or:
O.class.getAnnotation(WithArray.class);
will lead to:
java.lang.ArrayStoreException:
sun.reflect.annotation.TypeNotPresentExceptionProxy
The proposed fix is to throw TypeNotPresentException only when the
WithArray.value() method is invoked on the instance of the annotation
returned from O.class.getAnnotation(WithArray.class).
Something similar happens for enum constants missing at runtime (will
throw EnumConstantNotPresentException after the patch when queried).
There is also a somewhat related case when an annotation type is missing:
-----
@WithArray({@E})
class O {
}
@Retention(RetentionPolicy.RUNTIME)
@interface WithArray {
Class<?>[] value();
}
@interface E { }
-----
If "E" is missing at runtime, calling O.getAnnotations() leads to:
java.lang.NoClassDefFoundError: [Ltest/E;
After this fix, the O.getAnnotations() won't throw the exception, and
the resulting array won't contain the WithArray annotation.
Does this sound like a reasonable direction/patch?
Thanks,
Jan
More information about the core-libs-dev
mailing list