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

For classes declared in instance methods, constructor.getParameterAnnotations does not include implicit enclosing param

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 17.0.2, 17.0.6, 20, 21
    • core-libs

      Affects OpenJDK 17.0.6, 20+36-2344 (GA), 21-ea+10-784.

      With the following classes:

      public class TopLevel {
          public Class<?> myInstanceMethod() {
              class InstanceMethodNamedClass {
                  public InstanceMethodNamedClass(@MyParameterAnnotation String foo) {
                  }
              }
              return InstanceMethodNamedClass.class;
          }
      }

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

      ... calling `getParameterAnnotations()`on the constructor of `InstanceMethodNamedClass` will return an array that excludes the implicit enclosing parameter representing the instance of `TopLevel`:

      var clazz = new TopLevel().myInstanceMethod();
      var constructor = clazz.getDeclaredConstructors()[0];
      assert constructor.getParameters().length == 2; // succeeds
      assert constructor.getParameterAnnotations().length == 2; // fails; length is actually 1

      This behavior seems wrong since the javadoc of getParameterAnnotations() states:

      > Synthetic and mandated parameters (see explanation below), such as the outer "this" parameter to an
      > inner class constructor will be represented in the returned array


      -----

      Executable reproducer: see https://github.com/yrodiere/jdk-playground/tree/parameter-annotations, check out branch "parameter-annotations" and run `./mvnw clean install`

      -----

      Relates to JDK-8180892.

      This bug is similar to JDK-8074977, but different since it is still present after JDK-8074977 was fixed.

            liach Chen Liang
            yrodiere Yoann Rodiere
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: