6987475: Order of declarations affects whether abstract method considered overridden
authormcimadamore
Thu, 06 Nov 2014 14:31:56 +0000
changeset 27390 02221d4ed524
parent 27389 301be76491af
child 27391 17f7b6c40c6d
6987475: Order of declarations affects whether abstract method considered overridden Summary: Types.implementation erroneously returns first matching method in hierarchy. Reviewed-by: vromero
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java
langtools/test/tools/javac/generics/6987475/T6987475neg.java
langtools/test/tools/javac/generics/6987475/T6987475neg.out
langtools/test/tools/javac/generics/6987475/T6987475pos.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java	Wed Nov 05 19:48:42 2014 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java	Thu Nov 06 14:31:56 2014 +0000
@@ -2677,10 +2677,19 @@
                 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;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/6987475/T6987475neg.java	Thu Nov 06 14:31:56 2014 +0000
@@ -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<A> {
+        public void go(String s) { }
+        public abstract void go(A a);
+    }
+
+    static abstract class BaseReverse<A> {
+        public abstract void go(A a);
+        public void go(String s) { }
+    }
+
+    static abstract class Sub<A> extends Base<A> {
+        public abstract void go(A a);
+    }
+    static abstract class SubReverse<A> extends BaseReverse<A> {
+        public abstract void go(A a);
+    }
+
+    static class Impl1 extends Sub<String> { }
+    static class Impl2 extends SubReverse<String> { }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/6987475/T6987475neg.out	Thu Nov 06 14:31:56 2014 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/6987475/T6987475pos.java	Thu Nov 06 14:31:56 2014 +0000
@@ -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<A> {
+        public void go(String s) { }
+        public abstract void go(A a);
+    }
+
+    static abstract class BaseReverse<A> {
+        public abstract void go(A a);
+        public void go(String s) { }
+    }
+
+    static class Impl1 extends Base<String> { }
+    static class Impl2 extends BaseReverse<String> { }
+}