JDK-4520558 : (reflect) Inner objects should have access via reflection to private fields
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2001-10-29
  • Updated: 2021-04-24
Related Reports
Relates :  
Description

Name: nt126004			Date: 10/29/2001


java version "1.4.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)

Inner objects can access fields of the outer object.
However, when an inner object tries to access the value of a field
of its outer object via reflection, Field.getInt( this$0 ) throws
an unjustified IllegalAccessException.

Access control should be identical regardless of the access method, i.e.
regardless of whether reflection is used or not.

1. Run the following source code and the run time engine throws an unjustified
IllegalAccessException.

2. import java.io.*;
import java.lang.reflect.*;

public class test {
    public static void main( String[] args ) {
        test t = new test();
        t.newInner().accessFromInner();
    }
            
    private int i = 1;
    
    public inner newInner() { return new inner(); }
    
    class inner {
        public void accessFromInner() {
            try {
                i = 0;
                System.out.println( test.class.getDeclaredField("i").getInt(
this$0 ) );
            } catch( Exception e ) {
                System.out.println( e );
            }
        }
    }

}

3. java.lang.IllegalAccessException: test occurs
(Review ID: 134531) 
======================================================================

Comments
EVALUATION This bug is essentially requesting overload resolution for fields. A similar request for methods and constructors is in bug 4287725. Exactly when such functionality is provided would depend largely on interest and potential users. -- iag@sfbay 2002-04-24
24-04-2002