JDK-5047224 : (reflect) exception thrown from sun.reflect.Reflection.filterFields in Tiger b49
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2004-05-14
  • Updated: 2012-09-28
  • Resolved: 2004-05-28
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 b54Fixed
Related Reports
Relates :  
Description
The symptom is a NegativeArraySizeException thrown by a newly introduced 
method, filterFields():

java.lang.NegativeArraySizeException
        at sun.reflect.Reflection.filterFields(Reflection.java:235)
        at java.lang.Class.privateGetDeclaredFields(Class.java:1884)
        at java.lang.Class.privateGetPublicFields(Class.java:1916)
        at java.lang.Class.getFields(Class.java:1007)
        at 
com.bea.util.jam.internal.reflect.ReflectClassBuilder.populate(ReflectCl
assBuilder.java:128)

A comment on this problem:

>So I looked at the Sun source. filterFields is new in b49 and looks
like this (the line in question is where newFields gets allocated):
>
>    public static Field[] filterFields(Class containingClass,
>                                       Field[] fields) {
>        if (fieldFilterMap == null) {
>            // Bootstrapping
>            return fields;
>        }
>        String[] filteredFieldNames = (String[])
fieldFilterMap.get(containingClass);
>        if (filteredFieldNames == null) {
>            return fields;
>        }
>        Field[] newFields = new Field[fields.length -
filteredFieldNames.length];
>        int destIdx = 0;
>        for (int srcIdx = 0; srcIdx < fields.length; srcIdx++) {
>            boolean shouldSkip = false;
>            Field field = fields[srcIdx];
>            for (int tmp = 0; tmp < filteredFieldNames.length; tmp++) {
>                if (field.getName() == filteredFieldNames[tmp]) {
>                    shouldSkip = true;
>                    break;
>                }
>            }
>            if (!shouldSkip) {
>                newFields[destIdx++] = field;
>            }
>        }
>        return newFields;
>    }
>
>Clearly there are more filtered fields than fields which seems a little
impossible (and a clear bug).
>

test case:


public class Repro {

    public static void main(String[] args) throws ClassNotFoundException {
      Class c=Class.forName("sun.reflect.ConstantPool");
      java.lang.reflect.Field[] f=c.getFields();
      System.out.println("Fields Found: " + f.length);
    }
}

Try this against build 49 or 50, and you'll get:

Exception in thread "main" java.lang.NegativeArraySizeException
        at sun.reflect.Reflection.filterFields(Reflection.java:235)
        at java.lang.Class.privateGetDeclaredFields(Class.java:1884)
        at java.lang.Class.privateGetPublicFields(Class.java:1916)
        at java.lang.Class.getFields(Class.java:1007)
        at Repro.main(Repro.java:5)

    

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-rc FIXED IN: tiger-rc INTEGRATED IN: tiger-b54 tiger-rc
14-06-2004

EVALUATION New method added to fix bug 5012949. -- iag@sfbay 2004-05-13 Need to be flexible if filtered fields are not in input set. ###@###.### 2004-05-14 Fixed in 1.5.0 build 54. ###@###.### 2004-05-25
13-05-2004