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

Inconsistent handling of declaration annotations on receiver parameters

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: New
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: tools
    • Labels:
      None

      Description

      The intent of `tools/javac/annotations/typeAnnotations/failures/common/receiver/DeclarationAnnotation.java` [1] is that declaration annotations are not allowed on receiver parameters, but javac handles annotations that explicitly and implicitly target parameter declarations differently.

      Annotations without an @Target (which implicitly target parameter declarations, but currently not type annotations; see JDK-8231436) are disallowed:

      ```
      $ cat T.java
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;

      @Retention(RetentionPolicy.RUNTIME)
      @interface A {}

      class T {
          void f(@A T this) {}
      }
      $ javac -fullversion T.java
      javac full version "16-ea+34-2216"
      T.java:8: error: annotation @A not applicable in this type context
          void f(@A T this) {}
                 ^
      ```

      However annotations with an explicit @Target that includes parameter declarations are accepted, but the annotation is ignored and does not end up in the class output:

      ```
      $ cat T.java
      import java.lang.annotation.ElementType;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;

      @Target(ElementType.PARAMETER)
      @Retention(RetentionPolicy.RUNTIME)
      @interface A {}

      class T {
        void f(@A T this) {}
      }
      $ javac -fullversion T.java
      javac full version "16-ea+34-2216"
      $ javap -v T
      ...
        void f();
          descriptor: ()V
          flags: (0x0000)
          Code:
            stack=0, locals=1, args_size=1
               0: return
            LineNumberTable:
              line 11: 0
      ```

      Should both cases be rejected?

      [1] https://github.com/openjdk/jdk/blob/08f7454fa961e00f9f5ec370a339ae89134dcbf6/test/langtools/tools/javac/annotations/typeAnnotations/failures/common/receiver/DeclarationAnnotation.java

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated: