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

AnnotatedType interfaces provide no way to get annotations on owner type

    XMLWordPrintable

Details

    Description

      A DESCRIPTION OF THE REQUEST :
      In a nested parameterized type like the following:

      @TypeUse1 TopLevelType<T, U>. @TypeUse2 EnclosedType<T>

      Both the @TypeUse1 and @TypeUse2 annotations are stored in the class file. However, it is not possible to access the @TypeUse1 annotation via the reflection API. The annotations on the corresponding AnnotatedType include only @TypeUse2. Unlike ParameterizedType, the AnnotatedParameterizedType does not provide a way to query for the AnnotatedType corresponding to its owner type (which is where I'd expect to find @TypeUse1). Instead, I can only get annotations on actual type arguments.

      JUSTIFICATION :
      The AnnotatedType interfaces are intended to provide runtime reflective access to runtime-visible annotations on type uses.

      The TypeMirror interfaces do adequately provide access to these annotations. It doesn't make sense for there to be such incongruity between the two sets of APIs.

      I know the Checker API uses TypeMirror and was a primary motivation for annotating type uses. But the reflection APIs were also updated to support querying type annotations, so it seems clear that the intent was to provide them reflectively, and this omission is likely a defect.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Ideally, a new method would be added AnnotatedParameterizedType so it parallels the ParameterizedType interface in an intuitive way. For compatibility, it could have a default implementation that simply returns null or an AnnotatedType that simply wraps the underlying Type's owner type and exposes no annotations.

      public interface AnnotatedParameterizedType extends AnnotatedType {
        /* ADD: */
        AnnotatedType getAnnotatedOwnerType();

        AnnotatedType[] getAnnotatedActualTypeArguments();
      }

      Another possible option is a sub-interface that adds this method, which would preserve interface compatibility without requiring a default implementation.
      ACTUAL -
      No programmatic way to access such annotations reflectively. Instead, one would have to use a library like ASM to read the class bytes and extract/parse the annotations therein.

      CUSTOMER SUBMITTED WORKAROUND :
      ASM's ClassReader can visit these type annotations, so it is possible to extract them by reading the class's bytes, just not via the reflection API.

      Attachments

        Issue Links

          Activity

            People

              jfranck Joel Borggrén-Franck (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: