[kaffe] Should IllegalAccessException be thrown or not?

Ito Kazumitsu kaz at maczuka.gcd.org
Sun Dec 11 20:21:59 PST 2005


The test case attached below gives interesting results when run on
various environments. It seems that kaffe, Sun's JDK, and ecj have
some problem of their own.

Kaffe:

Call a private constructor in the same class
OK
Call a private constructor in the same pakeage
OK (*1)
Call a private constructor in another pakeage
java.lang.IllegalAccessException

   (*1) Maybe a bug in kaffe/kaffevm/access.c

Sun's JDK (the test programs compiled with Sun's javac):

Call a private constructor in the same class
java.lang.IllegalAccessException: Class A can not access a member of class A$PrivateClass with modifiers "private"  (*2)
Call a private constructor in the same pakeage
java.lang.IllegalAccessException: Class A can not access a member of class B$PrivateClass with modifiers "private"
Call a private constructor in another pakeage
java.lang.IllegalAccessException: Class A can not access a member of class b.B$PrivateClass with modifiers "private"

   (*2) JDK seems to be missing the fact that the target is declared
        within the caller. 

Sun's JDK (the test programs compiled with ecj):

Call a private constructor in the same class
OK
Call a private constructor in the same pakeage
OK  (*3)
Call a private constructor in another pakeage
java.lang.IllegalAccessException: Class A can not access a member of class b.B$PrivateClass with modifiers ""

   (*3) Ecj fails to put the private flag to the constructor.

The source of the test case follows.

$ cat A.java
public class A {

    private static class PrivateClass {
        private PrivateClass() {}
    }

    public static void main(String[] args) {
        System.err.println("Call a private constructor in the same class");
        try {
            PrivateClass.class.newInstance();
            System.err.println("OK");
        }
        catch (Exception e) {
            System.err.println(e);
        }

        System.err.println("Call a private constructor in the same pakeage");
        try {
            B.getPrivateClass().newInstance();
            System.err.println("OK");
        }
        catch (Exception e) {
            System.err.println(e);
        }

        System.err.println("Call a private constructor in another pakeage");
        try {
            b.B.getPrivateClass().newInstance();
            System.err.println("OK");
        }
        catch (Exception e) {
            System.err.println(e);
        }
    }

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



More information about the kaffe mailing list