Wed, 02 Apr 2008 11:38:16 +0100
6509042: javac rejects class literals in enum constructors
Summary: javac now distinguish between enum class literals and static fields
Reviewed-by: jjg
src/share/classes/com/sun/tools/javac/comp/Attr.java | file | annotate | diff | comparison | revisions | |
test/tools/javac/enum/T6509042.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Apr 02 11:20:52 2008 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Apr 02 11:38:16 2008 +0100 1.3 @@ -2199,7 +2199,7 @@ 1.4 (env.tree.getTag() != JCTree.ASSIGN || 1.5 TreeInfo.skipParens(((JCAssign) env.tree).lhs) != tree)) { 1.6 1.7 - if (!onlyWarning || isNonStaticEnumField(v)) { 1.8 + if (!onlyWarning || isStaticEnumField(v)) { 1.9 log.error(tree.pos(), "illegal.forward.ref"); 1.10 } else if (useBeforeDeclarationWarning) { 1.11 log.warning(tree.pos(), "forward.ref", v); 1.12 @@ -2233,7 +2233,7 @@ 1.13 // initializer expressions of an enum constant e to refer 1.14 // to itself or to an enum constant of the same type that 1.15 // is declared to the right of e." 1.16 - if (isNonStaticEnumField(v)) { 1.17 + if (isStaticEnumField(v)) { 1.18 ClassSymbol enclClass = env.info.scope.owner.enclClass(); 1.19 1.20 if (enclClass == null || enclClass.owner == null) 1.21 @@ -2254,8 +2254,14 @@ 1.22 } 1.23 } 1.24 1.25 - private boolean isNonStaticEnumField(VarSymbol v) { 1.26 - return Flags.isEnum(v.owner) && Flags.isStatic(v) && !Flags.isConstant(v); 1.27 + /** Is the given symbol a static, non-constant field of an Enum? 1.28 + * Note: enum literals should not be regarded as such 1.29 + */ 1.30 + private boolean isStaticEnumField(VarSymbol v) { 1.31 + return Flags.isEnum(v.owner) && 1.32 + Flags.isStatic(v) && 1.33 + !Flags.isConstant(v) && 1.34 + v.name != names._class; 1.35 } 1.36 1.37 /** Can the given symbol be the owner of code which forms part
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/enum/T6509042.java Wed Apr 02 11:38:16 2008 +0100 2.3 @@ -0,0 +1,41 @@ 2.4 +/* 2.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 2.24 + * have any questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 6509042 2.30 + * 2.31 + * @summary javac rejects class literals in enum constructors 2.32 + * @author Maurizio Cimadamore 2.33 + * 2.34 + * @compile T6509042.java 2.35 + */ 2.36 +enum T6509042 { 2.37 + A, B; 2.38 + 2.39 + Class<T6509042> cl = T6509042.class; 2.40 + 2.41 + T6509042() { 2.42 + Class<T6509042> cl2 = T6509042.class; 2.43 + } 2.44 +}