JDK-8074977 : Constructor.getAnnotatedParameterTypes returns wrong value
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 8,9
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2015-03-11
  • Updated: 2021-10-02
  • Resolved: 2017-05-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 10 JDK 9
10Fixed 9 b172Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
The following minimized test shows that j.l.r.Constructor.getAnnotatedParameterTypes returns wrong array:

---------------------------------------------------------------------------
public class MinimizedTest {

    public static void main(String[] args) throws NoSuchMethodException {
        Constructor constructor = Inner.class.getConstructor(MinimizedTest.class, Object.class);
        for (AnnotatedType at : constructor.getAnnotatedParameterTypes()) {
            System.out.println(at.getType());
            System.out.println(Arrays.toString(at.getAnnotations()));
            System.out.println();
        }
    }

    class Inner {
        public Inner(@Anno Object param) {}
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE_USE)
    @interface Anno {

    }
}
---------------------------------------------------------------------------

The output will be:
---------------------------------------------------------------------------
class MinimizedTest
[@MinimizedTest$Anno()]

class java.lang.Object
[]
---------------------------------------------------------------------------

As we can see, the first annotated parameter type has a type MinimizedTest (this is receiver) and has one annotation, but the receiver parameter is not annotated. The second parameter type doesn't has annotation, but it should.
Receiver parameter is not a formal parameter, so it's annotated type should not be presented here.
The spec of j.l.r.Executable.getAnnotatedParameterTypes says:
"Returns an array of AnnotatedType objects that represent the use of types to specify formal parameter types of the method/constructor represented by this Executable. "

Checked with JDK9 b53.
Failed JCK tests is under development.
Comments
URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/47032f7eebb1 User: lana Date: 2017-05-31 20:16:38 +0000
31-05-2017

Filed JDK-8180892 as follow-up work for JDK 10.
24-05-2017

URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/47032f7eebb1 User: darcy Date: 2017-05-23 21:36:17 +0000
23-05-2017

This is a P2 because it's a conformance issue. Joe has identified 3 issues, only one of which is covered by the JLS. The proposed fix is to address the issue covered by the JLS and is approved for JDK 9.
23-05-2017

Review thread: http://mail.openjdk.java.net/pipermail/core-libs-dev/2017-May/047793.html
19-05-2017

Initial version of fix at http://cr.openjdk.java.net/~darcy/8074977.0/ test still needed. Analogous problem in declaration annotations also addressed in the patch. Existing regression tests for java.lang.Class, java.lang.reflect, and java.lang.annotation pass with the change.
17-05-2017

See discussion in JDK-8024694.
26-10-2016

The type annotations (and normal) annotations on parameters of constructors with implicit arguments is indeed problematic. Continuing the investigation on possible solutions.
10-08-2016