7086595: Error message bug: name of initializer is 'null'
authormcimadamore
Tue, 13 Sep 2011 14:14:57 +0100
changeset 10626 83f0c2860f5b
parent 10462 1a59beaa5cb3
child 10627 d8e900b62e02
7086595: Error message bug: name of initializer is 'null' Summary: Implementation of MethodSymbol.location() should take into account static/instance initializers Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/code/Kinds.java
langtools/src/share/classes/com/sun/tools/javac/code/Printer.java
langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java
langtools/src/share/classes/com/sun/tools/javac/comp/Check.java
langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/src/share/classes/com/sun/tools/javac/util/RichDiagnosticFormatter.java
langtools/test/tools/javac/7086595/T7086595.java
langtools/test/tools/javac/7086595/T7086595.out
langtools/test/tools/javac/Diagnostics/6860795/T6860795.out
langtools/test/tools/javac/LocalClasses_2.out
langtools/test/tools/javac/NestedInnerClassNames.out
langtools/test/tools/javac/TryWithResources/BadTwr.out
langtools/test/tools/javac/TryWithResources/DuplicateResourceDecl.out
langtools/test/tools/javac/diags/examples/AlreadyDefinedClinit.java
langtools/test/tools/javac/diags/examples/KindnameInstanceInit.java
langtools/test/tools/javac/diags/examples/KindnameStaticInit.java
langtools/test/tools/javac/generics/6910550/T6910550d.out
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Kinds.java	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Kinds.java	Tue Sep 13 14:14:57 2011 +0100
@@ -103,6 +103,8 @@
         VAL("kindname.value"),
         METHOD("kindname.method"),
         CLASS("kindname.class"),
+        STATIC_INIT("kindname.static.init"),
+        INSTANCE_INIT("kindname.instance.init"),
         PACKAGE("kindname.package");
 
         private String name;
@@ -170,9 +172,11 @@
             return KindName.CONSTRUCTOR;
 
         case METHOD:
+            return KindName.METHOD;
         case STATIC_INIT:
+            return KindName.STATIC_INIT;
         case INSTANCE_INIT:
-            return KindName.METHOD;
+            return KindName.INSTANCE_INIT;
 
         default:
             if (sym.kind == VAL)
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Printer.java	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Printer.java	Tue Sep 13 14:14:57 2011 +0100
@@ -311,7 +311,7 @@
 
     @Override
     public String visitMethodSymbol(MethodSymbol s, Locale locale) {
-        if ((s.flags() & BLOCK) != 0) {
+        if (s.isStaticOrInstanceInit()) {
             return s.owner.name.toString();
         } else {
             String ms = (s.name == s.name.table.names.init)
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java	Tue Sep 13 14:14:57 2011 +0100
@@ -149,7 +149,8 @@
      * the default package; otherwise, the owner symbol is returned
      */
     public Symbol location() {
-        if (owner.name == null || (owner.name.isEmpty() && owner.kind != PCK && owner.kind != TYP)) {
+        if (owner.name == null || (owner.name.isEmpty() &&
+                (owner.flags() & BLOCK) == 0 && owner.kind != PCK && owner.kind != TYP)) {
             return null;
         }
         return owner;
@@ -1299,10 +1300,17 @@
                 return ElementKind.CONSTRUCTOR;
             else if (name == name.table.names.clinit)
                 return ElementKind.STATIC_INIT;
+            else if ((flags() & BLOCK) != 0)
+                return isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT;
             else
                 return ElementKind.METHOD;
         }
 
+        public boolean isStaticOrInstanceInit() {
+            return getKind() == ElementKind.STATIC_INIT ||
+                    getKind() == ElementKind.INSTANCE_INIT;
+        }
+
         public Attribute getDefaultValue() {
             return defaultValue;
         }
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Tue Sep 13 14:14:57 2011 +0100
@@ -306,7 +306,16 @@
      */
     void duplicateError(DiagnosticPosition pos, Symbol sym) {
         if (!sym.type.isErroneous()) {
-            log.error(pos, "already.defined", sym, sym.location());
+            Symbol location = sym.location();
+            if (location.kind == MTH &&
+                    ((MethodSymbol)location).isStaticOrInstanceInit()) {
+                log.error(pos, "already.defined.in.clinit", kindName(sym), sym,
+                        kindName(sym.location()), kindName(sym.location().enclClass()),
+                        sym.location().enclClass());
+            } else {
+                log.error(pos, "already.defined", kindName(sym), sym,
+                        kindName(sym.location()), sym.location());
+            }
         }
     }
 
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Sep 13 14:14:57 2011 +0100
@@ -68,9 +68,13 @@
 compiler.err.already.annotated=\
     {0} {1} has already been annotated
 
-# 0: symbol, 1: symbol
+# 0: symbol kind, 1: symbol, 2: symbol kind, 3: symbol
 compiler.err.already.defined=\
-    {0} is already defined in {1}
+    {0} {1} is already defined in {2} {3}
+
+# 0: symbol kind, 1: symbol, 2: symbol kind, 3: symbol kind, 4: symbol
+compiler.err.already.defined.in.clinit=\
+    {0} {1} is already defined in {2} of {3} {4}
 
 # 0: string
 compiler.err.already.defined.single.import=\
@@ -1753,6 +1757,12 @@
 compiler.misc.kindname.package=\
     package
 
+compiler.misc.kindname.static.init=\
+    static initializer
+
+compiler.misc.kindname.instance.init=\
+    instance initializer
+
 #####
 
 compiler.misc.no.args=\
--- a/langtools/src/share/classes/com/sun/tools/javac/util/RichDiagnosticFormatter.java	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/util/RichDiagnosticFormatter.java	Tue Sep 13 14:14:57 2011 +0100
@@ -412,7 +412,7 @@
         @Override
         public String visitMethodSymbol(MethodSymbol s, Locale locale) {
             String ownerName = visit(s.owner, locale);
-            if ((s.flags() & BLOCK) != 0) {
+            if (s.isStaticOrInstanceInit()) {
                return ownerName;
             } else {
                 String ms = (s.name == s.name.table.names.init)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/7086595/T7086595.java	Tue Sep 13 14:14:57 2011 +0100
@@ -0,0 +1,32 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 7086595
+ * @summary Error message bug: name of initializer is 'null'
+ * @compile/fail/ref=T7086595.out -XDrawDiagnostics T7086595.java
+ */
+
+class T7086595 {
+
+    String s = "x";
+    String s = nonExistent;
+
+    int foo() {
+        String s = "x";
+        String s = nonExistent;
+    }
+
+    static int bar() {
+        String s = "x";
+        String s = nonExistent;
+    }
+
+    {
+        String s = "x";
+        String s = nonExistent;
+    }
+
+    static {
+        String s = "x";
+        String s = nonExistent;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/7086595/T7086595.out	Tue Sep 13 14:14:57 2011 +0100
@@ -0,0 +1,11 @@
+T7086595.java:11:12: compiler.err.already.defined: kindname.variable, s, kindname.class, T7086595
+T7086595.java:11:16: compiler.err.cant.resolve.location: kindname.variable, nonExistent, , , (compiler.misc.location: kindname.class, T7086595, null)
+T7086595.java:15:16: compiler.err.already.defined: kindname.variable, s, kindname.method, foo()
+T7086595.java:15:20: compiler.err.cant.resolve.location: kindname.variable, nonExistent, , , (compiler.misc.location: kindname.class, T7086595, null)
+T7086595.java:20:16: compiler.err.already.defined: kindname.variable, s, kindname.method, bar()
+T7086595.java:20:20: compiler.err.cant.resolve.location: kindname.variable, nonExistent, , , (compiler.misc.location: kindname.class, T7086595, null)
+T7086595.java:25:16: compiler.err.already.defined.in.clinit: kindname.variable, s, kindname.instance.init, kindname.class, T7086595
+T7086595.java:25:20: compiler.err.cant.resolve.location: kindname.variable, nonExistent, , , (compiler.misc.location: kindname.class, T7086595, null)
+T7086595.java:30:16: compiler.err.already.defined.in.clinit: kindname.variable, s, kindname.static.init, kindname.class, T7086595
+T7086595.java:30:20: compiler.err.cant.resolve.location: kindname.variable, nonExistent, , , (compiler.misc.location: kindname.class, T7086595, null)
+10 errors
--- a/langtools/test/tools/javac/Diagnostics/6860795/T6860795.out	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/test/tools/javac/Diagnostics/6860795/T6860795.out	Tue Sep 13 14:14:57 2011 +0100
@@ -1,2 +1,2 @@
-T6860795.java:10:27: compiler.err.already.defined: x, foo
+T6860795.java:10:27: compiler.err.already.defined: kindname.variable, x, kindname.method, foo
 1 error
--- a/langtools/test/tools/javac/LocalClasses_2.out	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/test/tools/javac/LocalClasses_2.out	Tue Sep 13 14:14:57 2011 +0100
@@ -1,2 +1,2 @@
-LocalClasses_2.java:15:13: compiler.err.already.defined: Local, foo()
+LocalClasses_2.java:15:13: compiler.err.already.defined: kindname.class, Local, kindname.method, foo()
 1 error
--- a/langtools/test/tools/javac/NestedInnerClassNames.out	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/test/tools/javac/NestedInnerClassNames.out	Tue Sep 13 14:14:57 2011 +0100
@@ -1,18 +1,18 @@
-NestedInnerClassNames.java:16:5: compiler.err.already.defined: NestedInnerClassNames, compiler.misc.unnamed.package
-NestedInnerClassNames.java:23:9: compiler.err.already.defined: NestedInnerClassNames.foo, NestedInnerClassNames
-NestedInnerClassNames.java:34:9: compiler.err.already.defined: NestedInnerClassNames, compiler.misc.unnamed.package
-NestedInnerClassNames.java:45:9: compiler.err.already.defined: NestedInnerClassNames.baz, NestedInnerClassNames
-NestedInnerClassNames.java:46:13: compiler.err.already.defined: NestedInnerClassNames.baz.baz, NestedInnerClassNames.baz
-NestedInnerClassNames.java:59:9: compiler.err.already.defined: NestedInnerClassNames.foo$bar, NestedInnerClassNames
-NestedInnerClassNames.java:76:13: compiler.err.already.defined: NestedInnerClassNames.$bar, NestedInnerClassNames
-NestedInnerClassNames.java:90:13: compiler.err.already.defined: NestedInnerClassNames.bar$bar.bar, NestedInnerClassNames.bar$bar
+NestedInnerClassNames.java:16:5: compiler.err.already.defined: kindname.class, NestedInnerClassNames, kindname.package, compiler.misc.unnamed.package
+NestedInnerClassNames.java:23:9: compiler.err.already.defined: kindname.class, NestedInnerClassNames.foo, kindname.class, NestedInnerClassNames
+NestedInnerClassNames.java:34:9: compiler.err.already.defined: kindname.class, NestedInnerClassNames, kindname.package, compiler.misc.unnamed.package
+NestedInnerClassNames.java:45:9: compiler.err.already.defined: kindname.class, NestedInnerClassNames.baz, kindname.class, NestedInnerClassNames
+NestedInnerClassNames.java:46:13: compiler.err.already.defined: kindname.class, NestedInnerClassNames.baz.baz, kindname.class, NestedInnerClassNames.baz
+NestedInnerClassNames.java:59:9: compiler.err.already.defined: kindname.class, NestedInnerClassNames.foo$bar, kindname.class, NestedInnerClassNames
+NestedInnerClassNames.java:76:13: compiler.err.already.defined: kindname.class, NestedInnerClassNames.$bar, kindname.class, NestedInnerClassNames
+NestedInnerClassNames.java:90:13: compiler.err.already.defined: kindname.class, NestedInnerClassNames.bar$bar.bar, kindname.class, NestedInnerClassNames.bar$bar
 NestedInnerClassNames.java:109:5: compiler.err.duplicate.class: NestedInnerClassNames.foo.foo
-NestedInnerClassNames.java:19:9: compiler.err.already.defined: NestedInnerClassNames, compiler.misc.unnamed.package
-NestedInnerClassNames.java:28:13: compiler.err.already.defined: foo, m2()
-NestedInnerClassNames.java:40:13: compiler.err.already.defined: NestedInnerClassNames, compiler.misc.unnamed.package
-NestedInnerClassNames.java:52:13: compiler.err.already.defined: baz, m4()
-NestedInnerClassNames.java:53:17: compiler.err.already.defined: baz.baz, baz
-NestedInnerClassNames.java:67:13: compiler.err.already.defined: foo$bar, m5()
-NestedInnerClassNames.java:83:17: compiler.err.already.defined: $bar, m6()
-NestedInnerClassNames.java:97:17: compiler.err.already.defined: bar$bar.bar, bar$bar
+NestedInnerClassNames.java:19:9: compiler.err.already.defined: kindname.class, NestedInnerClassNames, kindname.package, compiler.misc.unnamed.package
+NestedInnerClassNames.java:28:13: compiler.err.already.defined: kindname.class, foo, kindname.method, m2()
+NestedInnerClassNames.java:40:13: compiler.err.already.defined: kindname.class, NestedInnerClassNames, kindname.package, compiler.misc.unnamed.package
+NestedInnerClassNames.java:52:13: compiler.err.already.defined: kindname.class, baz, kindname.method, m4()
+NestedInnerClassNames.java:53:17: compiler.err.already.defined: kindname.class, baz.baz, kindname.class, baz
+NestedInnerClassNames.java:67:13: compiler.err.already.defined: kindname.class, foo$bar, kindname.method, m5()
+NestedInnerClassNames.java:83:17: compiler.err.already.defined: kindname.class, $bar, kindname.method, m6()
+NestedInnerClassNames.java:97:17: compiler.err.already.defined: kindname.class, bar$bar.bar, kindname.class, bar$bar
 17 errors
--- a/langtools/test/tools/javac/TryWithResources/BadTwr.out	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/test/tools/javac/TryWithResources/BadTwr.out	Tue Sep 13 14:14:57 2011 +0100
@@ -1,5 +1,5 @@
-BadTwr.java:13:46: compiler.err.already.defined: r1, main(java.lang.String...)
-BadTwr.java:18:20: compiler.err.already.defined: args, main(java.lang.String...)
+BadTwr.java:13:46: compiler.err.already.defined: kindname.variable, r1, kindname.method, main(java.lang.String...)
+BadTwr.java:18:20: compiler.err.already.defined: kindname.variable, args, kindname.method, main(java.lang.String...)
 BadTwr.java:21:13: compiler.err.cant.assign.val.to.final.var: thatsIt
-BadTwr.java:26:24: compiler.err.already.defined: name, main(java.lang.String...)
+BadTwr.java:26:24: compiler.err.already.defined: kindname.variable, name, kindname.method, main(java.lang.String...)
 4 errors
--- a/langtools/test/tools/javac/TryWithResources/DuplicateResourceDecl.out	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/test/tools/javac/TryWithResources/DuplicateResourceDecl.out	Tue Sep 13 14:14:57 2011 +0100
@@ -1,2 +1,2 @@
-DuplicateResourceDecl.java:12:56: compiler.err.already.defined: c, main(java.lang.String[])
+DuplicateResourceDecl.java:12:56: compiler.err.already.defined: kindname.variable, c, kindname.method, main(java.lang.String[])
 1 error
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/AlreadyDefinedClinit.java	Tue Sep 13 14:14:57 2011 +0100
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2011, 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.
+ */
+
+// key: compiler.err.already.defined.in.clinit
+
+class AlreadyDefinedClinit {
+    static {
+        int i;
+        int i;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/KindnameInstanceInit.java	Tue Sep 13 14:14:57 2011 +0100
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2011, 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.
+ */
+
+// key: compiler.err.already.defined.in.clinit
+// key: compiler.misc.kindname.instance.init
+// key: compiler.misc.kindname.class
+// key: compiler.misc.kindname.variable
+// key: compiler.misc.count.error
+// key: compiler.err.error
+// run: backdoor
+
+class KindnameInstanceInit {
+    {
+        int i;
+        int i;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/KindnameStaticInit.java	Tue Sep 13 14:14:57 2011 +0100
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2011, 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.
+ */
+
+// key: compiler.err.already.defined.in.clinit
+// key: compiler.misc.kindname.static.init
+// key: compiler.misc.kindname.class
+// key: compiler.misc.kindname.variable
+// key: compiler.misc.count.error
+// key: compiler.err.error
+// run: backdoor
+
+class KindnameStaticInit {
+    static {
+        int i;
+        int i;
+    }
+}
--- a/langtools/test/tools/javac/generics/6910550/T6910550d.out	Mon Sep 12 11:40:07 2011 -0700
+++ b/langtools/test/tools/javac/generics/6910550/T6910550d.out	Tue Sep 13 14:14:57 2011 +0100
@@ -1,2 +1,2 @@
-T6910550d.java:12:14: compiler.err.already.defined: <X>m(X), T6910550d
+T6910550d.java:12:14: compiler.err.already.defined: kindname.method, <X>m(X), kindname.class, T6910550d
 1 error