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

Inconsistent handling of declaration annotations on receiver parameters

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • 18
    • 16, 17
    • tools
    • None

      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

            gli Guoxiong Li
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: