Thu, 22 Aug 2013 10:22:44 +0100
8022316: Generic throws, overriding and method reference
Reviewed-by: jjg, mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Wed Aug 21 20:41:42 2013 -0400 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Thu Aug 22 10:22:44 2013 +0100 1.3 @@ -505,12 +505,27 @@ 1.4 1.5 //merge thrown types - form the intersection of all the thrown types in 1.6 //all the signatures in the list 1.7 + boolean toErase = !bestSoFar.type.hasTag(FORALL); 1.8 List<Type> thrown = null; 1.9 - for (Symbol msym1 : methodSyms) { 1.10 - Type mt1 = memberType(origin.type, msym1); 1.11 + Type mt1 = memberType(origin.type, bestSoFar); 1.12 + for (Symbol msym2 : methodSyms) { 1.13 + Type mt2 = memberType(origin.type, msym2); 1.14 + List<Type> thrown_mt2 = mt2.getThrownTypes(); 1.15 + if (toErase) { 1.16 + thrown_mt2 = erasure(thrown_mt2); 1.17 + } else { 1.18 + /* If bestSoFar is generic then all the methods are generic. 1.19 + * The opposite is not true: a non generic method can override 1.20 + * a generic method (raw override) so it's safe to cast mt1 and 1.21 + * mt2 to ForAll. 1.22 + */ 1.23 + ForAll fa1 = (ForAll)mt1; 1.24 + ForAll fa2 = (ForAll)mt2; 1.25 + thrown_mt2 = subst(thrown_mt2, fa2.tvars, fa1.tvars); 1.26 + } 1.27 thrown = (thrown == null) ? 1.28 - mt1.getThrownTypes() : 1.29 - chk.intersect(mt1.getThrownTypes(), thrown); 1.30 + thrown_mt2 : 1.31 + chk.intersect(thrown_mt2, thrown); 1.32 } 1.33 1.34 final List<Type> thrown1 = thrown;
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/T8022316/CompilerErrorGenericThrowPlusMethodRefTest.java Thu Aug 22 10:22:44 2013 +0100 2.3 @@ -0,0 +1,78 @@ 2.4 +/* 2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. Oracle designates this 2.11 + * particular file as subject to the "Classpath" exception as provided 2.12 + * by Oracle in the LICENSE file that accompanied this code. 2.13 + * 2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.17 + * version 2 for more details (a copy is included in the LICENSE file that 2.18 + * accompanied this code). 2.19 + * 2.20 + * You should have received a copy of the GNU General Public License version 2.21 + * 2 along with this work; if not, write to the Free Software Foundation, 2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.23 + * 2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.25 + * or visit www.oracle.com if you need additional information or have any 2.26 + * questions. 2.27 + */ 2.28 + 2.29 +/* 2.30 + * @test 2.31 + * @bug 8022316 2.32 + * @summary Generic throws, overriding and method reference 2.33 + * @compile/fail/ref=CompilerErrorGenericThrowPlusMethodRefTest.out -XDrawDiagnostics CompilerErrorGenericThrowPlusMethodRefTest.java 2.34 + */ 2.35 + 2.36 +@SuppressWarnings("unchecked") 2.37 +public class CompilerErrorGenericThrowPlusMethodRefTest { 2.38 + interface SAM11 { 2.39 + public <E extends Throwable> void foo() throws E ; 2.40 + } 2.41 + 2.42 + interface SAM12 extends SAM11{ 2.43 + @Override 2.44 + public void foo() throws Throwable; 2.45 + } 2.46 + 2.47 + public void boo() throws RuntimeException {} 2.48 + 2.49 + static void test1() { 2.50 + try { 2.51 + SAM12 s2 = new CompilerErrorGenericThrowPlusMethodRefTest()::boo; 2.52 + s2.foo(); 2.53 + } catch(Throwable ex) {} 2.54 + } 2.55 + 2.56 + static void test2() { 2.57 + SAM11 s1 = null; 2.58 + s1.<Exception>foo(); 2.59 + s1.<RuntimeException>foo(); 2.60 + } 2.61 + 2.62 + interface SAM21 { 2.63 + <E extends Exception> void m(E arg) throws E; 2.64 + } 2.65 + 2.66 + interface SAM22 { 2.67 + <F extends Exception> void m(F arg) throws F; 2.68 + } 2.69 + 2.70 + interface SAM23 extends SAM21, SAM22 {} 2.71 + 2.72 + public <E extends Exception> void bar(E e) throws E {} 2.73 + 2.74 + static <E extends Exception> void test3(E e) { 2.75 + try { 2.76 + SAM23 s2 = new CompilerErrorGenericThrowPlusMethodRefTest()::bar; 2.77 + s2.m(e); 2.78 + } catch(Exception ex) {} 2.79 + } 2.80 + 2.81 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/T8022316/CompilerErrorGenericThrowPlusMethodRefTest.out Thu Aug 22 10:22:44 2013 +0100 3.3 @@ -0,0 +1,2 @@ 3.4 +CompilerErrorGenericThrowPlusMethodRefTest.java:55:26: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Exception 3.5 +1 error