Bug 77473 - [compiler] private nested class constructors are not private
Summary: [compiler] private nested class constructors are not private
Status: RESOLVED DUPLICATE of bug 117758
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0.1   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 3.2 M6   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-11-02 08:18 EST by Mark Wilkinson CLA
Modified: 2006-03-01 12:54 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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