JDK-4986034 : (reflect) Field.toString() returns native modifier for elements of enum type
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2004-01-30
  • Updated: 2012-09-28
  • Resolved: 2004-01-31
Related Reports
Duplicate :  
Description

Name: vrR10176			Date: 01/30/2004



The API spec (JDK1.5-b36) for method Field.toString() says:

"public String toString()

  Returns a string describing this Field. The format is the access modifiers for 
the field, if any, followed by the field type, followed by a space, followed by 
the fully-qualified name of the class declaring the field, followed by a period, 
followed by the name of the field. For example:
  public static final int java.lang.Thread.MIN_PRIORITY
  private int java.io.FileDescriptor.fd

The modifiers are placed in canonical order as specified by "The Java Language 
Specification". This is public, protected or private first, and then other modifiers 
in the following order: static, final, transient, volatile. "

But the method sets method modifier 'native' for elements of an enumerated type.

To reproduce the issue execute the following test.

------------ test.java -------------------------------
import java.lang.reflect.Field;

enum EnumClass001 {
    f1, f2;
}

public class test {
    public static void main(String argv[]) {
        try {
            Class cl = Class.forName("EnumClass001");
            Field f = cl.getField("f1");
            System.out.println("Field.toString() prints: " + f.toString());
        } catch(Throwable e) {
            System.out.println("unexpected exception " + e);
        }
    }
}

------------ Logs ---------------------------------------------
$javac -source 1.5 -d . test.java
$
$java -version
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b36)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b36, mixed mode)
$
$java test
Field.toString() prints: public static final native EnumClass001 EnumClass001.f1
$
$
-------------------------------------------------------------------

======================================================================

Comments
EVALUATION This behavior is exepected, if misleading. The current spec reuses the native bit to mark a field as an enum constant and not just an ordinary field. (The native bit only currently has meaning for methods and constructors so there is no conflict at the vm level.) The toString method of the Modifier class doesn't know what kind of entity is being modified so it can't suppress native in this case. It would be clearer if the modifier bits of fields, methods, constructors, and classes remained as disjoint as they were in 1.4. ###@###.### 2004-01-30 Issue is dup of 4975724. ###@###.### 2004-01-30
30-01-2004