8151018: javac should emit a clearer diagnostic when a <> inferred anonymous type's non-private methods don't override super's
Reviewed-by: mcimadamore
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java Wed Mar 02 15:00:22 2016 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java Thu Mar 03 06:10:58 2016 +0530
@@ -2001,10 +2001,11 @@
}
}
+ final boolean explicitOverride = m.attribute(syms.overrideType.tsym) != null;
// Check if this method must override a super method due to being annotated with @Override
// or by virtue of being a member of a diamond inferred anonymous class. Latter case is to
// be treated "as if as they were annotated" with @Override.
- boolean mustOverride = m.attribute(syms.overrideType.tsym) != null ||
+ boolean mustOverride = explicitOverride ||
(env.info.isAnonymousDiamond && !m.isConstructor() && !m.isPrivate());
if (mustOverride && !isOverrider(m)) {
DiagnosticPosition pos = tree.pos();
@@ -2014,7 +2015,9 @@
break;
}
}
- log.error(pos, "method.does.not.override.superclass");
+ log.error(pos,
+ explicitOverride ? Errors.MethodDoesNotOverrideSuperclass :
+ Errors.AnonymousDiamondMethodDoesNotOverrideSuperclass(Fragments.DiamondAnonymousMethodsImplicitlyOverride));
}
}
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties Wed Mar 02 15:00:22 2016 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties Thu Mar 03 06:10:58 2016 +0530
@@ -214,6 +214,11 @@
Unexpected @FunctionalInterface annotation\n\
{0}
+# 0: message segment
+compiler.err.anonymous.diamond.method.does.not.override.superclass=\
+ method does not override or implement a method from a supertype\n\
+ {0}
+
# 0: symbol
compiler.misc.not.a.functional.intf=\
{0} is not a functional interface
@@ -1196,6 +1201,9 @@
## miscellaneous strings
##
+compiler.misc.diamond.anonymous.methods.implicitly.override=\
+ (due to <>, every non-private method declared in this anonymous class must override or implement a method from a supertype)
+
compiler.misc.source.unavailable=\
(source unavailable)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/DiamondMethodDoesNotOverride.java Thu Mar 03 06:10:58 2016 +0530
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2016, 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.anonymous.diamond.method.does.not.override.superclass
+// key: compiler.misc.diamond.anonymous.methods.implicitly.override
+
+class X {
+ interface Foo<T> {
+ void g(T t);
+ }
+ void m() {
+ Foo<String> fs = new Foo<>() {
+ public void g(String s) { }
+ void someMethod() { }
+ };
+ }
+}
--- a/langtools/test/tools/javac/generics/diamond/neg/Neg15.out Wed Mar 02 15:00:22 2016 -0800
+++ b/langtools/test/tools/javac/generics/diamond/neg/Neg15.out Thu Mar 03 06:10:58 2016 +0530
@@ -1,4 +1,4 @@
-Neg15.java:48:28: compiler.err.method.does.not.override.superclass
-Neg15.java:52:21: compiler.err.method.does.not.override.superclass
-Neg15.java:56:31: compiler.err.method.does.not.override.superclass
+Neg15.java:48:28: compiler.err.anonymous.diamond.method.does.not.override.superclass: (compiler.misc.diamond.anonymous.methods.implicitly.override)
+Neg15.java:52:21: compiler.err.anonymous.diamond.method.does.not.override.superclass: (compiler.misc.diamond.anonymous.methods.implicitly.override)
+Neg15.java:56:31: compiler.err.anonymous.diamond.method.does.not.override.superclass: (compiler.misc.diamond.anonymous.methods.implicitly.override)
3 errors