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

javac fails to reject inadmissable type annotations

    Details

    • Type: Bug
    • Status: Open
    • Priority: P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: tbd
    • Component/s: tools

      Description

      javac correctly rejects the following type annotation on `One.Inner`, since annotations on `One` are not admissible (JLS 9.7.4).

      ```
      import static java.lang.annotation.ElementType.TYPE_USE;

      import java.lang.annotation.Target;

      abstract class One {
        @Target(TYPE_USE)
        @interface A {}

        static class Inner {}

        @A One.Inner f;
      }
      ```

      One.java:11: error: scoping construct cannot be annotated with type-use annotation: @One.A
        @A One.Inner f;
              ^
      1 error

      However in this example the annotation has been modified to target both 'FIELD' and 'TYPE_USE' locations, and javac both emits the annotation as a declaration on the field, and as a type annotation on `One` (which is incorrect):

      ```
      import static java.lang.annotation.ElementType.FIELD;
      import static java.lang.annotation.ElementType.TYPE_USE;

      import java.lang.annotation.Target;

      abstract class Two {
        @Target({FIELD, TYPE_USE})
        @interface A {}

        static class Inner {}

        @A Two.Inner f;
      }
      ```

        Two$Inner f;
          descriptor: LTwo$Inner;
          flags: (0x0000)
          RuntimeInvisibleAnnotations:
            0: #12()
              Two$A
          RuntimeInvisibleTypeAnnotations:
            0: #12(): FIELD
              Two$A

      I think the compiler should reject both inputs.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated: