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

(reflect) class literals adds unexpected synthetic Fields to class

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs



      Name: ddT132432 Date: 12/04/2001


      java version "1.3.1_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_01)
      Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)

      I have been working with java.lang.reflect and I have been noticing this wierd
      behaviour.

      I simply want to get the list of the Fields of a Class. This is my simple code.

      ---

      import java.lang.reflect.Field;

      public class TestIntro {
       
       private int intero;
       public String stringa;

       public TestIntro() {
       }

       public static void main(String[] args) {
        try {
         Field[] fields = TestIntro.class.getDeclaredFields();
         for (int count = 0; count < fields.length; count++) {
          System.out.println(fields[count].getName()+" "+fields[count].getType());
         }
        } catch (Exception e) {
         System.out.println("aiuto");
        }
       }
      }

      ---

      If I run it (on Windows 2000, jdk 1.3.1_01) I get:

      intero int
      stringa class java.lang.String
      class$TestIntro class java.lang.Class


      not only I get intero and stringa, but also a more misterious Field! :-)

      If you change TestIntro.class in Class.forName("TestIntro"), the extra field
      disappers!

      But if you put other .class around in the code (ex. Integer.class,
      String.class) you get an extra misterious Field for every class, but only one
      for class. That is, if you have Integer.class twice in your code, you get only
      one extra field.

      As I understand, class literals should be equivalent to Class.forName(). I
      haven't found any description of such behaviour in any documentation.
      (Review ID: 135495)
      ======================================================================

            iris Iris Clark
            ddressersunw Daniel Dresser (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: