JDK-4546736 : (reflect) class literals adds unexpected synthetic Fields to class
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2001-12-05
  • Updated: 2012-09-28
  • Resolved: 2001-12-05
Related Reports
Duplicate :  
Description

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) 
======================================================================

Comments
EVALUATION Using a class literal adds a synthetic field to the class which is detected by Class.getDeclaredFields(). This behaviour has existed since jdk1.1, when reflection was introduced. It is doubtful that we would change how this method works; however it would be reasonable to allow the user to screen out these synthetic fields via additional APIs. Bug 4407429 references this work. -- iag@sfbay 2001-12-05
05-12-2001