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

(enum) Enum constructor has wrong generic parameter types

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P4
    • tbd
    • 5.0
    • core-libs
    • x86
    • solaris_2.5.1

    Description

      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;

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Imported:
                Indexed: