--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java Mon Jan 08 17:16:37 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java Mon Jan 08 14:06:25 2018 -0500
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2018, 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
@@ -421,6 +421,12 @@
// We are seeing a member class.
c = syms.enterClass(env.toplevel.modle, tree.name, (TypeSymbol)owner);
if (c.owner != owner) {
+ if (c.name != tree.name) {
+ log.error(tree.pos(), Errors.SameBinaryName(c.name, tree.name));
+ result = types.createErrorType(tree.name, (TypeSymbol)owner, Type.noType);
+ tree.sym = (ClassSymbol)result.tsym;
+ return;
+ }
//anonymous class loaded from a classfile may be recreated from source (see below)
//if this class is a member of such an anonymous class, fix the owner:
Assert.check(owner.owner.kind != TYP, owner::toString);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties Mon Jan 08 17:16:37 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties Mon Jan 08 14:06:25 2018 -0500
@@ -443,6 +443,10 @@
compiler.err.duplicate.class=\
duplicate class: {0}
+# 0: name, 1: name
+compiler.err.same.binary.name=\
+ classes: {0} and {1} have the same binary name
+
compiler.err.duplicate.case.label=\
duplicate case label
--- a/test/langtools/tools/javac/NestedInnerClassNames.out Mon Jan 08 17:16:37 2018 +0100
+++ b/test/langtools/tools/javac/NestedInnerClassNames.out Mon Jan 08 14:06:25 2018 -0500
@@ -6,7 +6,7 @@
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:109:5: compiler.err.same.binary.name: foo, foo$foo
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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8187487/CrashWithDuplicateClassNamesTest.java Mon Jan 08 14:06:25 2018 -0500
@@ -0,0 +1,20 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8187487
+ * @summary crash with duplicate class name
+ * @compile/fail/ref=CrashWithDuplicateClassNamesTest.out -XDrawDiagnostics CrashWithDuplicateClassNamesTest.java
+ */
+
+class CrashWithDuplicateClassNamesTest {
+ static class C1$C2 {}
+
+ static class C1 {
+ static class C2 {}
+ }
+
+ static class C3 {
+ static class C4 {}
+ }
+
+ static class C3$C4 {}
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8187487/CrashWithDuplicateClassNamesTest.out Mon Jan 08 14:06:25 2018 -0500
@@ -0,0 +1,3 @@
+CrashWithDuplicateClassNamesTest.java:12:16: compiler.err.same.binary.name: C1$C2, C2
+CrashWithDuplicateClassNamesTest.java:19:12: compiler.err.same.binary.name: C4, C3$C4
+2 errors
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/diags/examples/SameBinaryName.java Mon Jan 08 14:06:25 2018 -0500
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2018, 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.same.binary.name
+
+class SameBinaryName {
+ private static final class Foo$Bar {}
+
+ private static final class Foo {
+ private static final class Bar {}
+ }
+}