8034044: Class.getModifiers() returns "static" for anonymous classes
Summary: Javac sets ACC_STATIC bit for anonymous classes which contradicts the JLS
Reviewed-by: jfranck
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Apr 01 17:25:39 2014 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Apr 02 10:05:16 2014 +0400
@@ -2112,10 +2112,8 @@
//
// This expression is then *transformed* as follows:
//
- // (1) add a STATIC flag to the class definition
- // if the current environment is static
- // (2) add an extends or implements clause
- // (3) add a constructor.
+ // (1) add an extends or implements clause
+ // (2) add a constructor.
//
// For instance, if C is a class, and ET is the type of E,
// the expression
@@ -2132,7 +2130,6 @@
// }
// ...
// }
- if (Resolve.isStatic(env)) cdef.mods.flags |= STATIC;
if (clazztype.tsym.isInterface()) {
cdef.implementing = List.of(clazz);
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java Tue Apr 01 17:25:39 2014 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java Wed Apr 02 10:05:16 2014 +0400
@@ -1072,9 +1072,6 @@
if (sym.isLocal()) {
mask = LocalClassFlags;
if (sym.name.isEmpty()) { // Anonymous class
- // Anonymous classes in static methods are themselves static;
- // that's why we admit STATIC here.
- mask |= STATIC;
// JLS: Anonymous classes are final.
implicit |= FINAL;
}
--- a/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Apr 01 17:25:39 2014 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Wed Apr 02 10:05:16 2014 +0400
@@ -3360,7 +3360,7 @@
? arguments() : List.<JCExpression>nil();
JCClassDecl body = null;
if (token.kind == LBRACE) {
- JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM | Flags.STATIC);
+ JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM);
List<JCTree> defs = classOrInterfaceBody(names.empty, false);
body = toP(F.at(identPos).AnonymousClassDef(mods1, defs));
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/T8034044.java Wed Apr 02 10:05:16 2014 +0400
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8034044
+ * @summary An anonymous class should not be static
+ */
+
+import static java.lang.reflect.Modifier.*;
+
+public class T8034044 {
+ enum En {
+ V() {}
+ }
+
+ static class InnStat {
+ static Object o = new Object() {};
+ }
+
+ public static void main(String[] args)
+ throws Throwable {
+ Object o = new Object() {};
+ test(o.getClass());
+ test(En.V.getClass());
+ test(InnStat.o.getClass());
+ new T8034044().f();
+ }
+
+ public void f() {
+ Object o = new Object() {};
+ test(o.getClass());
+ }
+
+ static void test(Class clazz) {
+ if ((clazz.getModifiers() & STATIC) != 0)
+ throw new AssertionError(clazz.toString() +
+ " should not be static");
+ }
+}