Wed, 02 Mar 2011 10:56:39 +0000
6541876: "Enclosing Instance" error new in 1.6
Summary: unqualified 'this' should not be selected in a qualified super() call in a default constructor
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Mar 01 12:00:06 2011 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Mar 02 10:56:39 2011 +0000 1.3 @@ -1425,7 +1425,7 @@ 1.4 // qualifier omitted; check for existence 1.5 // of an appropriate implicit qualifier. 1.6 rs.resolveImplicitThis(tree.meth.pos(), 1.7 - localEnv, site); 1.8 + localEnv, site, true); 1.9 } 1.10 } else if (tree.meth.getTag() == JCTree.SELECT) { 1.11 log.error(tree.meth.pos(), "illegal.qual.not.icls",
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Lower.java Tue Mar 01 12:00:06 2011 -0800 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java Wed Mar 02 10:56:39 2011 +0000 2.3 @@ -2817,8 +2817,8 @@ 2.4 // local class or this() call 2.5 thisArg = makeThis(tree.meth.pos(), c.type.getEnclosingType().tsym); 2.6 } else { 2.7 - // super() call of nested class 2.8 - thisArg = makeOwnerThis(tree.meth.pos(), c, false); 2.9 + // super() call of nested class - never pick 'this' 2.10 + thisArg = makeOwnerThisN(tree.meth.pos(), c, false); 2.11 } 2.12 tree.args = tree.args.prepend(thisArg); 2.13 }
3.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java Tue Mar 01 12:00:06 2011 -0800 3.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java Wed Mar 02 10:56:39 2011 +0000 3.3 @@ -1736,24 +1736,26 @@ 3.4 */ 3.5 Symbol resolveSelfContaining(DiagnosticPosition pos, 3.6 Env<AttrContext> env, 3.7 - Symbol member) { 3.8 + Symbol member, 3.9 + boolean isSuperCall) { 3.10 Name name = names._this; 3.11 - Env<AttrContext> env1 = env; 3.12 + Env<AttrContext> env1 = isSuperCall ? env.outer : env; 3.13 boolean staticOnly = false; 3.14 - while (env1.outer != null) { 3.15 - if (isStatic(env1)) staticOnly = true; 3.16 - if (env1.enclClass.sym.isSubClass(member.owner, types) && 3.17 - isAccessible(env, env1.enclClass.sym.type, member)) { 3.18 - Symbol sym = env1.info.scope.lookup(name).sym; 3.19 - if (sym != null) { 3.20 - if (staticOnly) sym = new StaticError(sym); 3.21 - return access(sym, pos, env.enclClass.sym.type, 3.22 - name, true); 3.23 + if (env1 != null) { 3.24 + while (env1 != null && env1.outer != null) { 3.25 + if (isStatic(env1)) staticOnly = true; 3.26 + if (env1.enclClass.sym.isSubClass(member.owner, types)) { 3.27 + Symbol sym = env1.info.scope.lookup(name).sym; 3.28 + if (sym != null) { 3.29 + if (staticOnly) sym = new StaticError(sym); 3.30 + return access(sym, pos, env.enclClass.sym.type, 3.31 + name, true); 3.32 + } 3.33 } 3.34 + if ((env1.enclClass.sym.flags() & STATIC) != 0) 3.35 + staticOnly = true; 3.36 + env1 = env1.outer; 3.37 } 3.38 - if ((env1.enclClass.sym.flags() & STATIC) != 0) 3.39 - staticOnly = true; 3.40 - env1 = env1.outer; 3.41 } 3.42 log.error(pos, "encl.class.required", member); 3.43 return syms.errSymbol; 3.44 @@ -1764,9 +1766,13 @@ 3.45 * JLS2 8.8.5.1 and 15.9.2 3.46 */ 3.47 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) { 3.48 + return resolveImplicitThis(pos, env, t, false); 3.49 + } 3.50 + 3.51 + Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) { 3.52 Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0) 3.53 ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this) 3.54 - : resolveSelfContaining(pos, env, t.tsym)).type; 3.55 + : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type; 3.56 if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym) 3.57 log.error(pos, "cant.ref.before.ctor.called", "this"); 3.58 return thisType;
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/implicitThis/6541876/T6541876a.java Wed Mar 02 10:56:39 2011 +0000 4.3 @@ -0,0 +1,43 @@ 4.4 +/* 4.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/** 4.28 + * @test 4.29 + * @bug 6541876 4.30 + * @summary "Enclosing Instance" error new in 1.6 4.31 + * 4.32 + */ 4.33 + 4.34 +public class T6541876a { 4.35 + class X { 4.36 + class Y {} 4.37 + } 4.38 + 4.39 + class A extends X { 4.40 + class B extends X.Y {} 4.41 + } 4.42 + 4.43 + public static void main(String[] args) { 4.44 + new T6541876a().new A().new B(); 4.45 + } 4.46 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/implicitThis/6541876/T6541876b.java Wed Mar 02 10:56:39 2011 +0000 5.3 @@ -0,0 +1,54 @@ 5.4 +/* 5.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. 5.11 + * 5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 + * version 2 for more details (a copy is included in the LICENSE file that 5.16 + * accompanied this code). 5.17 + * 5.18 + * You should have received a copy of the GNU General Public License version 5.19 + * 2 along with this work; if not, write to the Free Software Foundation, 5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 + * 5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.23 + * or visit www.oracle.com if you need additional information or have any 5.24 + * questions. 5.25 + */ 5.26 + 5.27 +/** 5.28 + * @test 5.29 + * @bug 6541876 6569091 5.30 + * @summary "Enclosing Instance" error new in 1.6 5.31 + * 5.32 + */ 5.33 + 5.34 +public class T6541876b { 5.35 + 5.36 + enum ENUM { 5.37 + ENUM_CONST { 5.38 + public AbstractClass method() { 5.39 + return new AbstractClass() { 5.40 + public boolean method() { 5.41 + return true; 5.42 + } 5.43 + }; 5.44 + } 5.45 + }; 5.46 + 5.47 + public abstract AbstractClass method(); 5.48 + 5.49 + private abstract class AbstractClass { 5.50 + public abstract boolean method(); 5.51 + } 5.52 + } 5.53 + 5.54 + public static void main(String[] args) { 5.55 + ENUM.ENUM_CONST.method(); 5.56 + } 5.57 +}
6.1 --- a/test/tools/javac/implicitThis/NewBeforeOuterConstructed3.java Tue Mar 01 12:00:06 2011 -0800 6.2 +++ b/test/tools/javac/implicitThis/NewBeforeOuterConstructed3.java Wed Mar 02 10:56:39 2011 +0000 6.3 @@ -1,5 +1,5 @@ 6.4 /* 6.5 - * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved. 6.6 + * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved. 6.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.8 * 6.9 * This code is free software; you can redistribute it and/or modify it 6.10 @@ -25,7 +25,6 @@ 6.11 * @test 6.12 * @bug 4704371 6313120 6.13 * @summary compiler generates unverifiable code for implicit reference to uninit'd this 6.14 - * @compile/fail NewBeforeOuterConstructed3.java 6.15 */ 6.16 6.17 public class NewBeforeOuterConstructed3 {
7.1 --- a/test/tools/javac/nested/4903103/T4903103.java Tue Mar 01 12:00:06 2011 -0800 7.2 +++ b/test/tools/javac/nested/4903103/T4903103.java Wed Mar 02 10:56:39 2011 +0000 7.3 @@ -1,5 +1,5 @@ 7.4 /* 7.5 - * Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved. 7.6 + * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. 7.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.8 * 7.9 * This code is free software; you can redistribute it and/or modify it 7.10 @@ -25,11 +25,14 @@ 7.11 * @test 7.12 * @bug 4903103 7.13 * @summary Can't compile subclasses of inner classes 7.14 - * @compile T4903103.java 7.15 */ 7.16 7.17 public class T4903103 { 7.18 private class InnerSuperclass extends T4903103 {} 7.19 7.20 private class InnerSubclass extends InnerSuperclass {} 7.21 + 7.22 + public static void main(String[] args) { 7.23 + new T4903103().new InnerSubclass(); 7.24 + } 7.25 }