[kaffe] Should IllegalAccessException be thrown or not?

Ito Kazumitsu kaz at maczuka.gcd.org
Sat Dec 10 17:51:52 PST 2005


From: Ito Kazumitsu <kaz at maczuka.gcd.org>
Date: Sat, 10 Dec 2005 11:18:13 +0900 (JST)

> public class TestPrivateClass {
> 	private static class Test0001 {
> 	}
> 
> 	public static void main(String[] args) {
> 		try {
> 			Object obj = Test0001.class.newInstance();
> 		}
> 		catch (Exception e) {
> 			e.printStackTrace();
> 		}
> 	}
> }
> 
> This ends normally when run on Kaffe, but when run on Sun's JDK,
> 
> java.lang.IllegalAccessException: Class TestPrivateClass can not access a member of class TestPrivateClass$Test0001 with modifiers "private"

According to "8.8.9 Default Constructor" in The Java Language Specification,
if the class is declared private, then the default constructor is
implicitly given the access modifier private.

So IllegalAccessException should be thrown.

The next test case will illustrate this problem more clearly.

$ cat A.java
public class A {
        public static void main(String[] args) {
            try {
                B.getPrivateClass().newInstance();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

}
$ cat B.java
public class B {
    private static class PrivateClass {}
    public static Class getPrivateClass() { return PrivateClass.class; }
}

In this case, Sun's JDK throws IllegalAccessException as expected.
But Kaffe does not.



More information about the kaffe mailing list