From ded3a562b99d323c8ddb4c1dc5f710bd57b7d51a Mon Sep 17 00:00:00 2001 From: Maurizio Cimadamore Date: Thu, 6 Nov 2014 14:31:56 +0000 Subject: [PATCH] 6987475: Order of declarations affects whether abstract method considered overridden Types.implementation erroneously returns first matching method in hierarchy. Reviewed-by: vromero --- .../com/sun/tools/javac/code/Types.java | 15 ++++-- .../javac/generics/6987475/T6987475neg.java | 29 ++++++++++++ .../javac/generics/6987475/T6987475neg.out | 3 ++ .../javac/generics/6987475/T6987475pos.java | 47 +++++++++++++++++++ 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 langtools/test/tools/javac/generics/6987475/T6987475neg.java create mode 100644 langtools/test/tools/javac/generics/6987475/T6987475neg.out create mode 100644 langtools/test/tools/javac/generics/6987475/T6987475pos.java diff --git a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java index 602e40dee80..f0bd8258ded 100644 --- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java +++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java @@ -2677,10 +2677,19 @@ public class Types { while (t.hasTag(TYPEVAR)) t = t.getUpperBound(); TypeSymbol c = t.tsym; + Symbol bestSoFar = null; for (Symbol sym : c.members().getSymbolsByName(ms.name, implFilter)) { - if (sym != null && - sym.overrides(ms, origin, Types.this, checkResult)) - return (MethodSymbol)sym; + if (sym != null && sym.overrides(ms, origin, Types.this, checkResult)) { + bestSoFar = sym; + if ((sym.flags() & ABSTRACT) == 0) { + //if concrete impl is found, exit immediately + break; + } + } + } + if (bestSoFar != null) { + //return either the (only) concrete implementation or the first abstract one + return (MethodSymbol)bestSoFar; } } return null; diff --git a/langtools/test/tools/javac/generics/6987475/T6987475neg.java b/langtools/test/tools/javac/generics/6987475/T6987475neg.java new file mode 100644 index 00000000000..d4214e4fd5e --- /dev/null +++ b/langtools/test/tools/javac/generics/6987475/T6987475neg.java @@ -0,0 +1,29 @@ +/* + * @test /nodynamiccopyright/ + * @bug 6987475 + * + * @summary Order of declarations affects whether abstract method considered overridden + * @compile/fail/ref=T6987475neg.out -XDrawDiagnostics T6987475neg.java + */ + +class T6987475neg { + static abstract class Base { + public void go(String s) { } + public abstract void go(A a); + } + + static abstract class BaseReverse { + public abstract void go(A a); + public void go(String s) { } + } + + static abstract class Sub extends Base { + public abstract void go(A a); + } + static abstract class SubReverse extends BaseReverse { + public abstract void go(A a); + } + + static class Impl1 extends Sub { } + static class Impl2 extends SubReverse { } +} diff --git a/langtools/test/tools/javac/generics/6987475/T6987475neg.out b/langtools/test/tools/javac/generics/6987475/T6987475neg.out new file mode 100644 index 00000000000..1a288ba4f70 --- /dev/null +++ b/langtools/test/tools/javac/generics/6987475/T6987475neg.out @@ -0,0 +1,3 @@ +T6987475neg.java:27:12: compiler.err.does.not.override.abstract: T6987475neg.Impl1, go(java.lang.String), T6987475neg.Sub +T6987475neg.java:28:12: compiler.err.does.not.override.abstract: T6987475neg.Impl2, go(java.lang.String), T6987475neg.SubReverse +2 errors diff --git a/langtools/test/tools/javac/generics/6987475/T6987475pos.java b/langtools/test/tools/javac/generics/6987475/T6987475pos.java new file mode 100644 index 00000000000..04947182199 --- /dev/null +++ b/langtools/test/tools/javac/generics/6987475/T6987475pos.java @@ -0,0 +1,47 @@ +/* + * 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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 6987475 + * + * @summary Order of declarations affects whether abstract method considered overridden + * @compile T6987475pos.java + */ + +class T6987475pos { + static abstract class Base { + public void go(String s) { } + public abstract void go(A a); + } + + static abstract class BaseReverse { + public abstract void go(A a); + public void go(String s) { } + } + + static class Impl1 extends Base { } + static class Impl2 extends BaseReverse { } +}