8151018: javac should emit a clearer diagnostic when a <> inferred anonymous type's non-private methods don't override super's

Reviewed-by: mcimadamore
This commit is contained in:
Srikanth Adayapalam 2016-03-03 06:10:58 +05:30
parent 012ed935ee
commit 433d2a7dbb
4 changed files with 53 additions and 5 deletions

View File

@ -2001,10 +2001,11 @@ public class Check {
}
}
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 @@ public class Check {
break;
}
}
log.error(pos, "method.does.not.override.superclass");
log.error(pos,
explicitOverride ? Errors.MethodDoesNotOverrideSuperclass :
Errors.AnonymousDiamondMethodDoesNotOverrideSuperclass(Fragments.DiamondAnonymousMethodsImplicitlyOverride));
}
}

View File

@ -214,6 +214,11 @@ compiler.err.bad.functional.intf.anno.1=\
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 @@ compiler.misc.fatal.err.cant.close=\
## 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)

View File

@ -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() { }
};
}
}

View File

@ -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