JDK-5087240 : (reflect) Constructor.getGenericParameterTypes for nested class w/ generics fail
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-08-16
  • Updated: 2024-06-14
  • Resolved: 2024-06-14
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.
Other
tbdResolved
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
Name: js151677			Date: 08/16/2004


FULL PRODUCT VERSION :
build 1.5.0-beta2-b51

ADDITIONAL OS VERSION INFORMATION :
Windows XP

A DESCRIPTION OF THE PROBLEM :
Constructor.getGenericParameterTypes for a nested class fail with a parameterized type.
The number of parameters is wrong (The first is missing).

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See code

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
ConstructorBugTest$Nested(ConstructorBugTest,java.util.List,long)
        class ConstructorBugTest class java.lang.Class
        java.util.List<java.lang.String> class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
        long class java.lang.Class

ACTUAL -
ConstructorBugTest$Nested(ConstructorBugTest,java.util.List,long)
        java.util.List<java.lang.String> class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
        long class java.lang.Class


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.lang.reflect.*;
import java.util.*;
public class ConstructorBugTest {
   public static void main(String... args){
      Class c = Nested.class;
      Constructor[] constructors = c.getDeclaredConstructors();
      for(Constructor cons : constructors){
         System.out.println(cons);
         Type[] types = cons.getGenericParameterTypes();
         for(Type t : types){
            System.out.println("\t" + t + " " + t.getClass());
         }
      }
   }
   private class Nested{
      Nested(List list, int x){
      }
      Nested(List<String> list, long x){
      }
   }
}

---------- END SOURCE ----------
(Incident Review ID: 297027) 
======================================================================

Comments
Fixed by JDK-8292275. Please use constructor.getParameters()[i].getParameterizedType().
14-06-2024

Once the synthetic and synthesized information is available in the platform from JDK-8004729, it will be possible to better address this bug.
13-06-2024

Still happens as of JDK 20: import java.lang.reflect.*; import java.util.*; public class Foo { public class Inner { public Inner(Optional<String> st) {} } public static void main(String[] args) throws Exception { var ctor = Foo.Inner.class.getDeclaredConstructor(Foo.class, Optional.class); System.out.println(ctor.getParameters()[1].getParameterizedType()); } } Since this produces incorrect parameterized types, it also affects nested type annotations. JDK-8292275 can fix this problem on the javac side.
28-04-2023

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
19-08-2004

EVALUATION The root cause of this problem is a difference between the Java vm and language views of a inner class constructor. At the vm level, there is a synthetic parameter prepended to the argument list so the enclosing object reference can be passed down. When generic information is present, that information correctly represents the source view of the constructor. However, the existing getParameterTypes method models the vm view of the constructor. The proper fix is having getGenericParameterTypes always model the source semantics even when generic information is not present. This should be addressed in a future release. ###@###.### 2004-08-18
18-08-2004