JDK-8198584 : (ann) Class.getAnnotation() throws an ArrayStoreException when the annotation class not present
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.lang
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 11
  • Submitted: 2018-02-22
  • Updated: 2018-03-14
  • Resolved: 2018-02-28
Related Reports
CSR :  
Description
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](https://docs.oracle.com/javase/9/docs/api/java/lang/reflect/AnnotatedElement.html) 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 relevant `Class`-returning method on the returned annotation will result in a `TypeNotPresentException`.


Comments
Moving to Approved after marking the parent issue as worthy of a release note.
28-02-2018