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

(reflect) Field.getGenericType() incorrectly identifies GenericArrayType

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      java version "1.7.0_04-ea"
      Java(TM) SE Runtime Environment (build 1.7.0_04-ea-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 23.0-b16, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:47:41 PST 2012; root:xnu-1699.24.23~1/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      Calling java.lang.reflect.Field.getGenericType() on a field that has a generic type array fails. In Java 1.6 if you invoke getGenericType on a field that has a generic array the type information correctly identifies that the field is an instance of GenericArrayType. In Java 1.7 it does not detect this correctly.

      REGRESSION. Last worked in version 6u29

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the main method for the attached source code and it will emit the failed message when run in Java 1.7. Running the same main method in Java 1.6 prints the success message.

      The class uses java.lang.reflect.Field.getGenericType() to get the type information for a class that has a java.lang.reflect.ParameterizedType member variable. The member variable looks like:

      Map<String, String[]>

      so it is a map with a GenericArrayType as the value type.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      "Success!! identified GenericArrayType"
      ACTUAL -
      Failed to correctly identify GenericArrayType, type was: class [Ljava.lang.String;

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.lang.reflect.Field;
      import java.lang.reflect.GenericArrayType;
      import java.lang.reflect.ParameterizedType;
      import java.util.HashMap;
      import java.util.Map;

      public class GenericsTest
      {

      private class Foo
      {
      private Map<String, String[]> map = new HashMap<String, String[]>();
      }

      public static void main(String[] args) throws NoSuchFieldException
      {
      Field field = Foo.class.getDeclaredField("map");
      ParameterizedType type = (ParameterizedType) field.getGenericType();
      Object f1 = type.getActualTypeArguments()[1];
      if (f1 instanceof GenericArrayType)
      {
      System.out.println("Success!! identified GenericArrayType");
      }
      else
      {
      System.err.println("Failed to correctly identify GenericArrayType, type was: " + f1);
      }
      }
      }

      ---------- END SOURCE ----------

      Attachments

        Activity

          People

            darcy Joe Darcy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: