-
Bug
-
Resolution: Duplicate
-
P4
-
7, 8
-
None
FULL PRODUCT VERSION :
A DESCRIPTION OF THE PROBLEM :
When calculating public fields of a class " Foo " which is extending a class " Baa " , the public fields of " Baa " are calculated in a recursive call
skipping those parent interfaces that have allready been calculated for class " Foo " .
Nevertheless, the reduced set of fields recursivly calculated for " Baa " is stored in " Baa " 's own static cache and will be retured on a direct " getFields() " call on " Baa.class " not including those parent interfaces of " Foo " skipped earlier.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try example code with " TRY_WITH_AND_WITHOUT = true " and then with
" TRY_WITH_AND_WITHOUT = false " . That leads to diffferent results although the output must be the same.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
With " TRY_WITH_AND_WITHOUT = false " :
Class C has the follwing fields: CD
Class C should have the follwing fields: CD
With " TRY_WITH_AND_WITHOUT = true " :
Class C has the follwing fields: CD
Class C should have the follwing fields: CD
ACTUAL -
With " TRY_WITH_AND_WITHOUT = false " (correct result):
Class C has the follwing fields: CD
Class C should have the follwing fields: CD
With " TRY_WITH_AND_WITHOUT = true " (wrong result):
Class C has the follwing fields: C
Class C should have the follwing fields: CD
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.lang.reflect.Field;
public class Main
{
public interface A extends B, C
{
public static final String A = " a " ;
}
public interface B extends D
{
public static final String B = " b " ;
}
public interface C extends D
{
public static final String C = " c " ;
}
public interface D
{
public static final String D = " d " ;
}
public static void main(String[] args) throws Exception
{
boolean TRY_WITH_AND_WITHOUT = true;
if (TRY_WITH_AND_WITHOUT)
{
A.class.getFields();
}
System.out.print( " Class C has the following fields: " );
for (Field field : C.class.getFields())
{
System.out.print(field.getName());
}
System.out.println();
System.out.println( " Class C should have the following fields: CD " );
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Set the system property " -Dsun.reflect.noCaches=true " .
A DESCRIPTION OF THE PROBLEM :
When calculating public fields of a class " Foo " which is extending a class " Baa " , the public fields of " Baa " are calculated in a recursive call
skipping those parent interfaces that have allready been calculated for class " Foo " .
Nevertheless, the reduced set of fields recursivly calculated for " Baa " is stored in " Baa " 's own static cache and will be retured on a direct " getFields() " call on " Baa.class " not including those parent interfaces of " Foo " skipped earlier.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try example code with " TRY_WITH_AND_WITHOUT = true " and then with
" TRY_WITH_AND_WITHOUT = false " . That leads to diffferent results although the output must be the same.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
With " TRY_WITH_AND_WITHOUT = false " :
Class C has the follwing fields: CD
Class C should have the follwing fields: CD
With " TRY_WITH_AND_WITHOUT = true " :
Class C has the follwing fields: CD
Class C should have the follwing fields: CD
ACTUAL -
With " TRY_WITH_AND_WITHOUT = false " (correct result):
Class C has the follwing fields: CD
Class C should have the follwing fields: CD
With " TRY_WITH_AND_WITHOUT = true " (wrong result):
Class C has the follwing fields: C
Class C should have the follwing fields: CD
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.lang.reflect.Field;
public class Main
{
public interface A extends B, C
{
public static final String A = " a " ;
}
public interface B extends D
{
public static final String B = " b " ;
}
public interface C extends D
{
public static final String C = " c " ;
}
public interface D
{
public static final String D = " d " ;
}
public static void main(String[] args) throws Exception
{
boolean TRY_WITH_AND_WITHOUT = true;
if (TRY_WITH_AND_WITHOUT)
{
A.class.getFields();
}
System.out.print( " Class C has the following fields: " );
for (Field field : C.class.getFields())
{
System.out.print(field.getName());
}
System.out.println();
System.out.println( " Class C should have the following fields: CD " );
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Set the system property " -Dsun.reflect.noCaches=true " .
- duplicates
-
JDK-8186961 Class.getFields() does not return fields of previously visited super interfaces/classes.
-
- Resolved
-
- relates to
-
JDK-8013529 java.lang.Class#getFields() is not deterministic
-
- Closed
-