Tue, 11 Aug 2009 01:14:31 +0100
6199153: Generic throws and overriding
Summary: javac incorrectly rejects an uchecked overriding
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java Tue Aug 11 01:14:06 2009 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java Tue Aug 11 01:14:31 2009 +0100 1.3 @@ -1043,7 +1043,7 @@ 1.4 * @param thrown The list of thrown exceptions. 1.5 * @param handled The list of handled exceptions. 1.6 */ 1.7 - List<Type> unHandled(List<Type> thrown, List<Type> handled) { 1.8 + List<Type> unhandled(List<Type> thrown, List<Type> handled) { 1.9 List<Type> unhandled = List.nil(); 1.10 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) 1.11 if (!isHandled(l.head, handled)) unhandled = unhandled.prepend(l.head); 1.12 @@ -1200,29 +1200,36 @@ 1.13 m.owner.isSubClass(other.owner, types)) { 1.14 // allow limited interoperability with covariant returns 1.15 } else { 1.16 - typeError(TreeInfo.diagnosticPositionFor(m, tree), 1.17 - diags.fragment("override.incompatible.ret", 1.18 - cannotOverride(m, other)), 1.19 + log.error(TreeInfo.diagnosticPositionFor(m, tree), 1.20 + "override.incompatible.ret", 1.21 + cannotOverride(m, other), 1.22 mtres, otres); 1.23 return; 1.24 } 1.25 } else if (overrideWarner.warned) { 1.26 warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), 1.27 - "prob.found.req", 1.28 - diags.fragment("override.unchecked.ret", 1.29 - uncheckedOverrides(m, other)), 1.30 - mtres, otres); 1.31 + "override.unchecked.ret", 1.32 + uncheckedOverrides(m, other), 1.33 + mtres, otres); 1.34 } 1.35 1.36 // Error if overriding method throws an exception not reported 1.37 // by overridden method. 1.38 List<Type> otthrown = types.subst(ot.getThrownTypes(), otvars, mtvars); 1.39 - List<Type> unhandled = unHandled(mt.getThrownTypes(), otthrown); 1.40 - if (unhandled.nonEmpty()) { 1.41 + List<Type> unhandledErased = unhandled(mt.getThrownTypes(), types.erasure(otthrown)); 1.42 + List<Type> unhandledUnerased = unhandled(mt.getThrownTypes(), otthrown); 1.43 + if (unhandledErased.nonEmpty()) { 1.44 log.error(TreeInfo.diagnosticPositionFor(m, tree), 1.45 "override.meth.doesnt.throw", 1.46 cannotOverride(m, other), 1.47 - unhandled.head); 1.48 + unhandledUnerased.head); 1.49 + return; 1.50 + } 1.51 + else if (unhandledUnerased.nonEmpty()) { 1.52 + warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), 1.53 + "override.unchecked.thrown", 1.54 + cannotOverride(m, other), 1.55 + unhandledUnerased.head); 1.56 return; 1.57 } 1.58
2.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties Tue Aug 11 01:14:06 2009 +0100 2.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties Tue Aug 11 01:14:31 2009 +0100 2.3 @@ -1093,23 +1093,33 @@ 2.4 no arguments 2.5 2.6 compiler.err.override.static=\ 2.7 - {0}; overriding method is static 2.8 + {0}\n\ 2.9 + overriding method is static 2.10 compiler.err.override.meth=\ 2.11 - {0}; overridden method is {1} 2.12 + {0}\n\ 2.13 + overridden method is {1} 2.14 2.15 compiler.err.override.meth.doesnt.throw=\ 2.16 - {0}; overridden method does not throw {1} 2.17 + {0}\n\ 2.18 + overridden method does not throw {1} 2.19 2.20 # In the following string {1} is a space separated list of Java Keywords, as 2.21 # they would have been declared in the source code 2.22 compiler.err.override.weaker.access=\ 2.23 - {0}; attempting to assign weaker access privileges; was {1} 2.24 + {0}\n\ 2.25 + attempting to assign weaker access privileges; was {1} 2.26 2.27 -compiler.misc.override.incompatible.ret=\ 2.28 - {0}; attempting to use incompatible return type 2.29 +compiler.err.override.incompatible.ret=\ 2.30 + {0}\n\ 2.31 + return type {1} is not compatible with {2} 2.32 2.33 -compiler.misc.override.unchecked.ret=\ 2.34 - {0}; return type requires unchecked conversion 2.35 +compiler.warn.override.unchecked.ret=\ 2.36 + [unchecked] {0}\n\ 2.37 + return type requires unchecked conversion from {1} to {2} 2.38 + 2.39 +compiler.warn.override.unchecked.thrown=\ 2.40 + [unchecked] {0}\n\ 2.41 + overridden method does not throw {1} 2.42 2.43 ## The following are all possible strings for the first argument ({0}) of the 2.44 ## above strings.
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/OverrideChecks/6199153/T6199153.java Tue Aug 11 01:14:31 2009 +0100 3.3 @@ -0,0 +1,44 @@ 3.4 +/* 3.5 + * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 3.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 3.24 + * have any questions. 3.25 + */ 3.26 + 3.27 +/** 3.28 + * @test 3.29 + * @bug 6199153 3.30 + * @summary Generic throws and overriding 3.31 + * @author mcimadamore 3.32 + * @compile/fail/ref=T6199153.out -Xlint -Werror -XDrawDiagnostics T6199153.java 3.33 + */ 3.34 + 3.35 +import java.io.IOException; 3.36 + 3.37 +class T6199153 { 3.38 + 3.39 + static class A { 3.40 + public <T extends IOException> void m() throws T {} 3.41 + } 3.42 + 3.43 + static class B extends A { 3.44 + public void m() throws IOException {} 3.45 + } 3.46 +} 3.47 +
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/OverrideChecks/6199153/T6199153.out Tue Aug 11 01:14:31 2009 +0100 4.3 @@ -0,0 +1,4 @@ 4.4 +T6199153.java:41:21: compiler.warn.override.unchecked.thrown: (compiler.misc.cant.override: m(), T6199153.B, <T>m(), T6199153.A), java.io.IOException 4.5 +- compiler.err.warnings.and.werror 4.6 +1 error 4.7 +1 warning