JDK-8181410 : Regular accessibility, not reflective access
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 8,9
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2017-05-31
  • Updated: 2017-06-13
  • Resolved: 2017-06-13
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :


A DESCRIPTION OF THE PROBLEM :
I have two classes A and B, where B extends A and A is package protected. Both classes are in the same package.

class A
{
      public static final String VALUE = "foo";
}

public class B extends A
{
}

// The static constant VALUE from A is accessible through B:
System.out.println( B.VALUE );

// Reflective access doesn't work without making the field accessible, which is not always an option.
// And whenever regular access is possible, reflective access should be as well from the same context.

Class< B > clazz = ...;
Field field = clazz.getField( "VALUE" );
String value = (String) field.get( null );
System.out.println( value );

REGRESSION.  Last worked in version 8u131

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Whenever regular access is possible, reflective access should be as well from the same context.

ACTUAL -
Reflective access doesn't work without making the field accessible, which is not always an option.

REPRODUCIBILITY :
This bug can be reproduced always.


Comments
This is a duplicate of JDK-4283544.
13-06-2017

To reproduce the issue, run the attached test case. Both the classes are in different packages. Following are the results : JDK 8 - Fail JDK 8u131 - Fail JDK 9ea+170 - Fail Output: foo Exception in thread "main" java.lang.IllegalAccessException: class Test cannot access a member of class JI9049336.A with modifiers "public static final" at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Unknown Source) at java.base/java.lang.reflect.AccessibleObject.checkAccess(Unknown Source) at java.base/java.lang.reflect.Field.checkAccess(Unknown Source) at java.base/java.lang.reflect.Field.get(Unknown Source) at Test.main(Test.java:11)
09-06-2017