changeset 2704:7b6a6aeeb544

6987475: Order of declarations affects whether abstract method considered overridden Summary: Types.implementation erroneously returns first matching method in hierarchy. Reviewed-by: vromero
author mcimadamore
date Thu, 06 Nov 2014 14:31:56 +0000
parents d32c4f52e295
children 4235749f4989
files src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java test/tools/javac/generics/6987475/T6987475neg.java test/tools/javac/generics/6987475/T6987475neg.out test/tools/javac/generics/6987475/T6987475pos.java
diffstat 4 files changed, 91 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java	Wed Nov 05 19:48:42 2014 -0800
+++ b/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/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/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/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> { }
+}