Summary
When Class.getAnnotation()
is called to retrieve an annotation, and the annotation has an array value that references a missing class, attempting to read that value should result in a TypeNotPresentException
.
Problem
The call to getAnnotation()
currently crashes with an ArrayStoreException
.
Solution
The solution is to add additional error handling to annotation parsing and throw a TypeNotPresentException
if the array value is retrieved. This is consistent with the specification, and with how similar errors are currently handled.
There are situations where it might be useful to allow access to the array elements that were successfully parsed, and defer the TypeNotPresentException
until any elements that reference missing classes are read. However this is not supported by the current implementation, it is not required by the specification, and it's not clear how to achieve it with the current API: there's no way to return an array that throws an exception when particular elements are accessed.
Specification
The existing specification of AnnotatedElement includes:
If an annotation returned by a method in this interface contains (directly or indirectly) a
Class
-valued member referring to a class that is not accessible in this VM, attempting to read the class by calling the relevantClass
-returning method on the returned annotation will result in aTypeNotPresentException
.
- csr of
-
JDK-7183985 (ann) Class.getAnnotation() throws an ArrayStoreException when the annotation class not present
-
- Closed
-