Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8198584

(ann) Class.getAnnotation() throws an ArrayStoreException when the annotation class not present

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 11
    • core-libs
    • None
    • behavioral
    • low
    • Change in behavior may perturb existing workarounds.
    • Java API
    • Implementation

      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 relevant Class-returning method on the returned annotation will result in a TypeNotPresentException.

            cushon Liam Miller-Cushon
            dkorbel David Korbel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: