Bug 77473

Summary: [compiler] private nested class constructors are not private
Product: [Eclipse Project] JDT Reporter: Mark Wilkinson <mhw-eclipse>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: RESOLVED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: mlists
Version: 3.0.1   
Target Milestone: 3.2 M6   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Mark Wilkinson CLA 2004-11-02 08:18:47 EST
The compiler seems to generate the wrong code (or different from jdk 1.4.2_06,
at least) for the default constructor in private nested classes. Given the
following classes defined over two files:

package scribble;

public class Bar {
    public static void main(String[] args) throws Exception {
        Foo foo = new Foo();
        foo.frob(Baz.class);
    }

    private static class Baz {
    }
}

public class Foo {
    public void frob(Class cls) throws Exception {
        Object o = cls.newInstance();
    }
}

Compiling and running with the JDK gives:

Exception in thread "main" java.lang.IllegalAccessException: Class scribble.Foo
can not access a member of class scribble.Bar$Baz with modifiers "private"
        at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57)
        at java.lang.Class.newInstance0(Class.java:302)
        at java.lang.Class.newInstance(Class.java:261)
        at scribble.Foo.frob(Foo.java:5)
        at scribble.Bar.main(Bar.java:10)

Compiling with Eclipse and running with the JDK gives no error.

Comparing the output of javap shows why. javac gives us this:

Compiled from "Bar.java"
class scribble.Bar$Baz extends java.lang.Object{
    private scribble.Bar$Baz();
}
 
while Eclipse's compiler gives us this:

Compiled from "Bar.java"
class scribble.Bar$Baz extends java.lang.Object{
    scribble.Bar$Baz();
}
 
Interestingly, the same code is generated even if Baz has an explicit private
constructor.
Comment 1 Michel Onoff CLA 2005-04-28 13:08:46 EDT
This happens only with private constructors; private methods have the private
flag in the .class.

Why this assymmetry?
Comment 2 Philipe Mulet CLA 2006-03-01 05:22:42 EST
This behavior has been in for a long time. But I agree it is wrong in the end.
Will correct it.

Marking as dup of bug 117758 which had more discussions in it.

*** This bug has been marked as a duplicate of 117758 ***
Comment 3 Philipe Mulet CLA 2006-03-01 12:54:53 EST
Added InnerEmulationTest#test124