JDK-8265097 : AnnotationParser throws NullPointerException on incompatible member type
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 17
  • Submitted: 2021-04-12
  • Updated: 2021-04-20
  • Resolved: 2021-04-20
Related Reports
CSR :  
Description
Summary
-------

This change proposes to always throw an AnnotationTypeMismatchException if the type of an annotation member is changed and the member is accessed for an annotation that was compiled prior to this change. The javadoc for AnnotationTypeMismatchException documents that this exception is intended for this purpose but is not currently thrown out of an oversight in the annotation parser implementation.

Problem
-------

If an annotation member type is changed from being an enum to an annotation or vice versa, the current implementation of the annotation parser in the reflection API throws a NullPointerException upon processing a previously compiled annotation. This is the result of that change not being explicitly handled in the code. Unfortunately, the null pointer is already thrown when processing the annotation as no exception proxy is generated. This makes the entire annotation inaccessible as parsing an annotation is impossible and always yields the error, even if another, compatible member is supposed to be accessed.

Solution
--------

Change the existing implementation to throw an AnnotationTypeMismatchException if a member type is changed from an enum to an annotation type or vice versa. This avoids the null pointer altogether.

Specification
-------------

The behavior is already specified in the annotation parser's javadoc but currently incorrectly implemented.

```
    /**
     * (...)
     *
     * The member must be of the indicated type. If it is not, this
     * method returns an AnnotationTypeMismatchExceptionProxy.
     */
     public static Object parseMemberValue(Class<?> memberType,
                                           ByteBuffer buf,
                                           ConstantPool constPool,
                                           Class<?> container);
```
Comments
Moving to Approved.
20-04-2021

Moving to Provisional. [~jfranck], please review the request before it is Finalized. This behavioral change probably falls under the bar of what should have a release note.
13-04-2021