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

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

XMLWordPrintable

    • b09

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

        JLS 9.6.3 says that a repeatable annotation is applicable to at least the same kinds of program elements as its containing annotation type.

        JLS 9.6.4.1 says that if @Target is not present on an annotation, the annotation is applicable in all declaration contexts (and was updated in JDK-8231435 to all contexts, not just declaration contexts, although there's some ongoing discussion about the inclusion of type contexts; see e.g. https://github.com/openjdk/jdk/pull/2303).

        This means that a repeatable annotation without a @Target can have a container annotation with an explicit @Target, e.g. in the following example TC explicit targets methods, and T implicitly targets methods:


        ```
        import java.lang.annotation.ElementType;
        import java.lang.annotation.Repeatable;
        import java.lang.annotation.Target;

        @Target(ElementType.METHOD)
        @interface TC {
          T[] value() default {};
        }

        @Repeatable(TC.class)
        @interface T {}
        ```

        However this doesn't work for container annotations that explicitly target modules declarations:

        ```
        import java.lang.annotation.ElementType;
        import java.lang.annotation.Repeatable;
        import java.lang.annotation.Target;

        @Target(ElementType.MODULE)
        @interface TC {
          T[] value() default {};
        }

        @Repeatable(A.class)
        @interface T {}
        ```

        ```
        javac full version "16-ea+34-2216"
        error: containing annotation type (TC) is applicable to more targets than repeatable annotation type (T)
        @Repeatable(TC.class)
        ^
        ```

        JDK-8254023 updated the handling of annotations without a @Target to make them admissible to module declarations, but there's separate logic for handling repeatable annotations without @Target that wasn't updated: https://github.com/openjdk/jdk/blob/f025bc1d5d81532a3bdb87665537de4aaf15b7ea/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java#L3197

              cushon Liam Miller-Cushon
              cushon Liam Miller-Cushon
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: