JDK-4221909 : (reflect) Class.newInstance() throws IllegalAccessError when the class has an inner non-public class
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 1.2.2
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: solaris_2.6
  • CPU: sparc
  • Submitted: 1999-03-19
  • Updated: 2012-09-28
Description

Name: ksC84122			Date: 03/19/99


java.lang.Class.newInstance() throws IllegalAccessError if this class has an inner
not public class.

!!!
The following test should have MyClass1.class in the current directory file compiled 
from MyClass1.java file(see below):
===== test42.java ========
public class test42 {

   public static void main (String argv[]) {
       TestLdr tl = new TestLdr(); // custom ClassLoader
       Class cl = null;
    
       try {
           cl = tl.findClass("MyClass1");
           cl.newInstance();
           System.out.println("PASSED");
           return;
       } catch (Throwable e) {
           e.printStackTrace();
           System.out.println("FAILED: Unexpected " + e);
           return;
       }
  }  
}

class MyClassLoader extends ClassLoader {}

class TestLdr extends ClassLoader {
  private String testdirurl = null;
  
  protected Class findClass(String name) throws ClassNotFoundException {
    byte[] data;
    try {
      java.io.InputStream in  = new java.io.FileInputStream(name + ".class");
      java.io.ByteArrayOutputStream buf=new java.io.ByteArrayOutputStream();
      while (true) {
        int d=in.read();
        if (d==-1) break;
        buf.write(d);
      }
      in.close();
      data=buf.toByteArray();
    } catch(java.net.MalformedURLException e) {
      throw new ClassNotFoundException(e.toString());
    } catch(java.io.IOException e) {
      throw new RuntimeException(e.toString());
    }
    return defineClass(null, data, 0, data.length);
  }
}

===== MyClass1.java ========
public class MyClass1 {
    public MyClass1() {
          new Inner();
    }

    class Inner {}   
}

========= Sample run (JDK1.2.2) ==========
#>java test42
java.lang.IllegalAccessError: try to access class MyClass1$Inner from class MyClass1
        at java.lang.Class.newInstance0(Native Method)
        at java.lang.Class.newInstance(Class.java, Compiled Code)
        at test42.main(test42.java, Compiled Code)
FAILED: Unexpected java.lang.IllegalAccessError: try to access class MyClass1$Inner from class MyClass1


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

Comments
EVALUATION I think I reproduced the problem. anand.palaniswamy@Eng 1999-03-22
22-03-1999