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

Repeatable annotations without @Target cannot have containers that target module declarations

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P4
    • Resolution: Approved
    • Fix Version/s: 17-pool, 18
    • Component/s: tools
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      source
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      Hide
      The impact is believed to be minimal because a `@Target` that explicitly lists all default contexts is redundant, can be removed. Additionally, a repeatable annotation that explicitly lists all default targets and whose container omits `@Target` will already have to be updated whenever new targets are added, such as `RECORD_COMPONENT`.
      Show
      The impact is believed to be minimal because a `@Target` that explicitly lists all default contexts is redundant, can be removed. Additionally, a repeatable annotation that explicitly lists all default targets and whose container omits `@Target` will already have to be updated whenever new targets are added, such as `RECORD_COMPONENT`.
    • Interface Kind:
      Language construct

      Description

      Summary

      Repeatable annotations without an @Target annotation should be allowed to have a container annotation with an @Target that explicitly includes module declarations.

      Problem

      Repeatable annotations are required to be applicable to at least the same kinds of program elements as their containing annotation type (JLS 9.6.3), and annotations without an @Target are applicable to module declarations (JLS 9.6.4.1).

      Repeatable annotations with a @Target that explicitly lists all default targets except for MODULE and whose container annotation do not have an @Target are currently erroneously accepted by javac, and should be rejected:

      import java.lang.annotation.ElementType;
      import java.lang.annotation.Repeatable;
      import java.lang.annotation.Target;
      
      @interface TC {
        T[] value() default {};
      }
      
      @Target({
        ElementType.CONSTRUCTOR,
        ElementType.PARAMETER,
        ElementType.TYPE,
        ElementType.METHOD,
        ElementType.LOCAL_VARIABLE,
        ElementType.PACKAGE,
        ElementType.ANNOTATION_TYPE,
        ElementType.FIELD,
        ElementType.RECORD_COMPONENT,
      })
      @Repeatable(TC.class) // error: containing annotation type (TC) is applicable to more targets than repeatable annotation type (T)
      @interface T {}

      Repeatable annotations without an @Target whose container annotations explicitly targets modules are currently erroneously rejected, and should be accepted by javac:

      import java.lang.annotation.ElementType;
      import java.lang.annotation.Repeatable;
      import java.lang.annotation.Target;
      
      @Target(ElementType.MODULE)
      @interface TC {
        T[] value() default {};
      }
      
      @Repeatable(TC.class)
      @interface T {}

      Solution

      The solution is to update javac to include module declarations in the set of default targets of repeatable annotations.

      See also discussion in https://github.com/openjdk/jdk/pull/2303.

      Specification

      No changes to the specification are required, this behaviour is already specified by JLS 9.6.3 and 9.6.4.1.

      The change to javac to reconcile it with the specification is in https://github.com/openjdk/jdk/pull/2412.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              cushon Liam Miller-Cushon
              Reporter:
              cushon Liam Miller-Cushon
              Reviewed By:
              Joel Borggrén-Franck (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: