User account creation filtered due to spam.

Bug 9369

Summary: wrong bytecode generated for static inner class with private constructor
Product: gcc Reporter: Jeff Sturm <jsturm>
Component: javaAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: minor CC: gcc-bugs, java-prs, mark, tromey
Priority: P3 Keywords: wrong-code
Version: 3.4.0   
Target Milestone: 4.3.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2005-09-30 13:33:55
Bug Depends on: 28067    
Bug Blocks: 18131    

Description Jeff Sturm 2003-01-19 18:16:01 UTC
javac and jikes generate a synthetic constructor for the example given:

5: invokespecial #3=<Method Outer$Inner.<init> (Outer$1)void>

gcj however attempts to access the constructor declared private, failing at runtime with an IllegalAccessError:

4: invokespecial #13=<Method Outer$Inner.<init> ()void>

Also gcj doesn't generate the synthetic Outer$Inner constructor at all.

Release:
gcj (GCC) 3.4 20021230 (experimental)

How-To-Repeat:
$ cat Outer.java
public class Outer {
	public Object getInner() {
		return new Inner();
	}
	private static class Inner {
		private Inner() {
		}
		public String toString() {
			return "OK";
		}
	}
	public static void main(String[] args) {
		Outer outer = new Outer();
		System.out.println(outer.getInner());
	}
}
$ gcj -C Outer.java
$ gij Outer
Exception in thread "main" java.lang.IllegalAccessError
   at _Jv_SearchMethodInClass(java.lang.Class, java.lang.Class, _Jv_Utf8Const, _Jv_Utf8Const) (/opt/gcc/lib/libgcj.so.4.0.0)
   at _Jv_ResolvePoolEntry(java.lang.Class, int) (/opt/gcc/lib/libgcj.so.4.0.0)
   at Outer.getInner() (Unknown Source)
   at Outer.main(java.lang.String[]) (Unknown Source)
Comment 1 Tom Tromey 2003-05-03 01:43:00 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Still a bug
Comment 2 Andrew Pinski 2004-05-20 23:41:10 UTC
Note to get the error when running with Sun's java, you have to use -Xfuture.
Comment 3 Andrew Pinski 2005-02-10 00:26:26 UTC
*** Bug 19870 has been marked as a duplicate of this bug. ***
Comment 4 Andrew Pinski 2005-05-12 02:03:11 UTC
Hmm, even though GCJ -C produces the same bytecode for Outer as far as I can see, we now produce 
something which actually works with both gij and java (with -Xfuture).
Comment 5 Tom Tromey 2005-05-13 23:27:23 UTC
This no longer fails due to a change that went in as part of the fix
for PR 8618.  In particular, we now emit the 'Inner' constructor as
package-private, not private.

This is kind of a slacker approach to implementing accessors.
For constructors and static methods it probably has the desired
effect without doing harm, though I haven't considered it in great
detail.  For ordinary methods it might be a problem due to overriding.

This PR should only be marked as "fixed" if there is another PR for the
case of accessors for methods and fields.  Even then it isn't entirely
clear if this is more hack or more fix.

Comment 6 Tom Tromey 2007-01-09 20:44:48 UTC
All gcj front end bugs have been fixed by the gcj-eclipse branch merge.
I'm mass-closing the affected PRs.
If you believe one of these was closed in error, please reopen it
with a note explaining why.
Thanks.