FULL PRODUCT VERSION :
java version "1.5.0_09"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_09-b03)
Java HotSpot(TM) Server VM (build 1.5.0_09-b03, mixed mode)
and
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Server VM (build 1.6.0-b105, mixed mode)
A DESCRIPTION OF THE PROBLEM :
Using the reflection API to look at the parameter types for the synthetic enum
constructor produces different results depending upon whether
getParameterTypes() or getGenericParameterTypes() is used.
I'm guessing it is related to this?
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4989987
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See the testcase
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The methods getParameterTypes() and getGenericParameterTypes()
should provide the same results (at least arrays of the same size).
ACTUAL -
The getGenericParameterTypes() returns an empty array of parameter types.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.util.Arrays;
public class Main
{
public static void main(String[] args) throws Exception
{
Class<MyEnum> clazz = MyEnum.class;
Constructor<MyEnum> constructor = clazz.getDeclaredConstructor(new Class[] { String.class, Integer.TYPE });
Type[] types = constructor.getParameterTypes();
Type[] generic = constructor.getGenericParameterTypes();
System.out.println("types = " + Arrays.asList(types));
System.out.println("generic = " + Arrays.asList(generic));
if (Arrays.equals(types, generic) == false)
throw new RuntimeException("They should be the same?");
}
public enum MyEnum { ONE, TWO, THREE };
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
A simple workaround is the following code,
but only if you know the problem exists and can assume
the problem is for types that don't use generic parameters?
// Use the normal types if the generic types are inconsistent
Type[] types = constructor.getParameterTypes();
Type[] generic = constructor.getGenericParameterTypes();
if (types.length != generic.length)
generic = types;
java version "1.5.0_09"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_09-b03)
Java HotSpot(TM) Server VM (build 1.5.0_09-b03, mixed mode)
and
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Server VM (build 1.6.0-b105, mixed mode)
A DESCRIPTION OF THE PROBLEM :
Using the reflection API to look at the parameter types for the synthetic enum
constructor produces different results depending upon whether
getParameterTypes() or getGenericParameterTypes() is used.
I'm guessing it is related to this?
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4989987
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See the testcase
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The methods getParameterTypes() and getGenericParameterTypes()
should provide the same results (at least arrays of the same size).
ACTUAL -
The getGenericParameterTypes() returns an empty array of parameter types.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.util.Arrays;
public class Main
{
public static void main(String[] args) throws Exception
{
Class<MyEnum> clazz = MyEnum.class;
Constructor<MyEnum> constructor = clazz.getDeclaredConstructor(new Class[] { String.class, Integer.TYPE });
Type[] types = constructor.getParameterTypes();
Type[] generic = constructor.getGenericParameterTypes();
System.out.println("types = " + Arrays.asList(types));
System.out.println("generic = " + Arrays.asList(generic));
if (Arrays.equals(types, generic) == false)
throw new RuntimeException("They should be the same?");
}
public enum MyEnum { ONE, TWO, THREE };
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
A simple workaround is the following code,
but only if you know the problem exists and can assume
the problem is for types that don't use generic parameters?
// Use the normal types if the generic types are inconsistent
Type[] types = constructor.getParameterTypes();
Type[] generic = constructor.getGenericParameterTypes();
if (types.length != generic.length)
generic = types;
- duplicates
-
JDK-8292275 javac does not emit SYNTHETIC and MANDATED flags for parameters by default
- Resolved
- relates to
-
JDK-8004729 Add java.lang.reflect.Parameter and related changes for parameter reflection
- Closed
-
JDK-4989987 Verify synthetic enum constructor parameters are not visible in mirror api
- Resolved
-
JDK-5087240 (reflect) Constructor.getGenericParameterTypes for nested class w/ generics fail
- Closed