8139255: javac reports "cannot override" messages instead of "cannot hide" messages for static methods
authorsadayapalam
Thu, 12 Nov 2015 08:39:23 +0530
changeset 33713 dc1d2632935c
parent 33712 fd1ce6d7ac63
child 33714 8064f484590e
8139255: javac reports "cannot override" messages instead of "cannot hide" messages for static methods Summary: Improve clarity of javac messages by discriminating hiding scenerio from overriding Reviewed-by: mcimadamore, sadayapalam Contributed-by: srinivas.dama@oracle.com
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/OverrideChecks/T4720359a.out
langtools/test/tools/javac/OverrideChecks/T8139255.java
langtools/test/tools/javac/OverrideChecks/T8139255.out
langtools/test/tools/javac/diags/examples/HideStatic.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Thu Nov 12 06:13:14 2015 +0530
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Thu Nov 12 08:39:23 2015 +0530
@@ -1730,11 +1730,18 @@
         boolean resultTypesOK =
             types.returnTypeSubstitutable(mt, ot, otres, overrideWarner);
         if (!resultTypesOK) {
-            log.error(TreeInfo.diagnosticPositionFor(m, tree),
-                      "override.incompatible.ret",
-                      cannotOverride(m, other),
-                      mtres, otres);
-            m.flags_field |= BAD_OVERRIDE;
+            if ((m.flags() & STATIC) != 0 && (other.flags() & STATIC) != 0) {
+                log.error(TreeInfo.diagnosticPositionFor(m, tree),
+                        Errors.OverrideIncompatibleRet(Fragments.CantHide(m, m.location(), other,
+                                        other.location()), mtres, otres));
+                m.flags_field |= BAD_OVERRIDE;
+            } else {
+                log.error(TreeInfo.diagnosticPositionFor(m, tree),
+                        "override.incompatible.ret",
+                        cannotOverride(m, other),
+                        mtres, otres);
+                m.flags_field |= BAD_OVERRIDE;
+            }
             return;
         } else if (overrideWarner.hasNonSilentLint(LintCategory.UNCHECKED)) {
             warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree),
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Nov 12 06:13:14 2015 +0530
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Nov 12 08:39:23 2015 +0530
@@ -2284,6 +2284,10 @@
     {0} in {1} cannot override {2} in {3}
 
 # 0: symbol, 1: symbol, 2: symbol, 3: symbol
+compiler.misc.cant.hide=\
+    {0} in {1} cannot hide {2} in {3}
+
+# 0: symbol, 1: symbol, 2: symbol, 3: symbol
 compiler.misc.cant.implement=\
     {0} in {1} cannot implement {2} in {3}
 
--- a/langtools/test/tools/javac/OverrideChecks/T4720359a.out	Thu Nov 12 06:13:14 2015 +0530
+++ b/langtools/test/tools/javac/OverrideChecks/T4720359a.out	Thu Nov 12 08:39:23 2015 +0530
@@ -1,2 +1,2 @@
-T4720359a.java:16:23: compiler.err.override.incompatible.ret: (compiler.misc.cant.override: m(), p1.T4720359c, m(), p1.T4720359a), int, void
+T4720359a.java:16:23: compiler.err.override.incompatible.ret: (compiler.misc.cant.hide: m(), p1.T4720359c, m(), p1.T4720359a), int, void
 1 error
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/OverrideChecks/T8139255.java	Thu Nov 12 08:39:23 2015 +0530
@@ -0,0 +1,14 @@
+/*
+ * @test    /nodynamiccopyright/
+ * @bug     8139255
+ * @summary javac emits diagnostic message as overriding instead of hiding for class methods.
+ * @compile/fail/ref=T8139255.out -XDrawDiagnostics  T8139255.java
+ */
+
+public class T8139255 {
+   static void func() { }
+}
+
+class T8139255_1  extends T8139255 {
+   static int func() { return 0; }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/OverrideChecks/T8139255.out	Thu Nov 12 08:39:23 2015 +0530
@@ -0,0 +1,2 @@
+T8139255.java:13:15: compiler.err.override.incompatible.ret: (compiler.misc.cant.hide: func(), T8139255_1, func(), T8139255), int, void
+1 error
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/HideStatic.java	Thu Nov 12 08:39:23 2015 +0530
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2015, 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.override.incompatible.ret
+// key: compiler.misc.cant.hide
+
+class Base {
+   static void func() { }
+}
+
+class HideStatic extends Base {
+   static int func() { return 0; }
+}