User account creation filtered due to spam.

Bug 8618 - call to private constructor allowed for anonymous inner class
Summary: call to private constructor allowed for anonymous inner class
Alias: None
Product: gcc
Classification: Unclassified
Component: java (show other bugs)
Version: 3.2
: P3 normal
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
Keywords: accepts-invalid
Depends on:
Blocks: 13607
  Show dependency treegraph
Reported: 2002-11-17 20:06 UTC by jmr
Modified: 2004-07-10 05:44 UTC (History)
2 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2004-05-20 23:48:57


Note You need to log in before you can comment on or make changes to this bug.
Description jmr 2002-11-17 20:06:01 UTC
Calls to private constructors are (incorrectly) allowed when  creating an anonymous inner class.

; cat
public class X {
    public static void main(String[] args) {
        // new Y();
        new Y() { };  // should be illegal

; cat
public class Y {
    private Y() {

; gcj --main=X

gcj doesn't give an error, whereas Sun's javac does

; javac Y() has private access in Y
        new Y() { };
        ^ Y() has private access in Y
        new Y() { };
2 errors

If you switch around which line is commented out in X.main, then gcj correctly
gives an error:

gcj --main=X Can't access private constructor `Y.<init>' from `X'.
           new Y();
1 error

gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
Comment 1 Andrew Pinski 2003-05-25 23:06:35 UTC
Confirmed bug on the mainline (20030525).
Even swaping them now does not work.
tin:~/src/gnu/gcctest/pr8614>gcj --main=X
tin:~/src/gnu/gcctest/pr8614>gcj --main=X
tin:~/src/gnu/gcctest/pr8614>javac *.java Y() has private access in Y
        new Y() { };  // should be illegal
        ^ Y() has private access in Y
        new Y() { };  // should be illegal
2 errors
Comment 2 CVS Commits 2004-07-10 05:38:21 UTC
Subject: Bug 8618

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	2004-07-10 05:38:16

Modified files:
	gcc/java       : ChangeLog class.c java-tree.h jcf-write.c 
	                 parse.h parse.y 

Log message:
	2004-07-09  Bryce McKinlay  <>
	PR java/8618
	* parse.y (create_anonymous_class): Remove 'location' argument. Use
	the WFL from TYPE_NAME to get line number for the decl. Fix comment.
	(craft_constructor): Inherit access flags for implicit constructor
	from the enclosing class.
	(create_class): Fix comment typo.
	(resolve_qualified_expression_name): Pass type of qualifier to
	not_accessible_p, not the type in which target field was found.
	(not_accessible_p): Handle inner classes. Expand protected
	qualifier-subtype check to enclosing instances, but don't apply this
	check to static members. Allow protected access to inner classes
	of a subtype. Allow private access within common enclosing context.
	(build_super_invocation): Get WFL line number info from current
	class decl.
	(build_incomplete_class_ref): Update for new create_anonymous_class
	* class.c (common_enclosing_context_p): New. Determine if types
	share a common enclosing context, even across static contexts.
	(common_enclosing_instance_p): Renamed from
	common_enclosing_context_p. Determines if types share a common
	non-static enclosing instance.
	* java-tree.h (common_enclosing_instance_p): Declare.
	* jcf-write.c (get_method_access_flags): New. Surpress private flag
	for inner class constructors.
	(generate_classfile): Use get_method_access_flags.


Comment 3 Bryce McKinlay 2004-07-10 05:44:42 UTC
Fix checked in to HEAD.