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

PARAMETER annotation on receiver type does not cause error

XMLWordPrintable

    • b20
    • Verified

      A DESCRIPTION OF THE PROBLEM :
      Annotating a receiver type with an annotation whose target is PARAMETER compiles fine, but the annotation is not present in byte code.

      It should fail because according to JLS 9.7.4:
      "It is a compile-time error if an annotation of type T is syntactically a modifier for: [...] a receiver parameter, but T is not applicable to type contexts."

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try compiling the code provided below

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      javac should fail because the `@Param` annotation is not valid on the receiver type.
      ACTUAL -
      javac compiles the code

      ---------- BEGIN SOURCE ----------
      import static java.lang.annotation.RetentionPolicy.RUNTIME;

      import java.lang.annotation.ElementType;
      import java.lang.annotation.Retention;
      import java.lang.annotation.Target;

      class ReceiverParamAnnotation {
          @Retention(RUNTIME)
          @Target({ElementType.TYPE_USE})
          @interface TypeUse { }
          
          @Retention(RUNTIME)
          @Target({ElementType.PARAMETER})
          @interface Param { }
          
          public void test(@TypeUse @Param ReceiverParamAnnotation this) { }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        1. ReceiverParamAnnotation.class
          0.5 kB
          Andrew Wang
        2. ReceiverParamAnnotation.java
          0.5 kB
          Andrew Wang
        3. ReceiverParamAnnotation.java
          0.5 kB
          Andrew Wang
        4. ReceiverParamAnnotation$Param.class
          0.5 kB
          Andrew Wang
        5. ReceiverParamAnnotation$TypeUse.class
          0.5 kB
          Andrew Wang

            gli Guoxiong Li
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: