Mon, 15 Apr 2013 14:17:30 +0100
8011392: Missing checkcast when casting to intersection type
Summary: javac should emit a checkcast for each additional target type specified in an intersection type cast
Reviewed-by: jjg
src/share/classes/com/sun/tools/javac/comp/TransTypes.java | file | annotate | diff | comparison | revisions | |
test/tools/javac/lambda/Intersection03.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/TransTypes.java Mon Apr 15 14:16:05 2013 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/TransTypes.java Mon Apr 15 14:17:30 2013 +0100 1.3 @@ -710,8 +710,18 @@ 1.4 1.5 public void visitTypeCast(JCTypeCast tree) { 1.6 tree.clazz = translate(tree.clazz, null); 1.7 + Type originalTarget = tree.type; 1.8 tree.type = erasure(tree.type); 1.9 tree.expr = translate(tree.expr, tree.type); 1.10 + if (originalTarget.isCompound()) { 1.11 + Type.IntersectionClassType ict = (Type.IntersectionClassType)originalTarget; 1.12 + for (Type c : ict.getExplicitComponents()) { 1.13 + Type ec = erasure(c); 1.14 + if (!types.isSameType(ec, tree.type)) { 1.15 + tree.expr = coerce(tree.expr, ec); 1.16 + } 1.17 + } 1.18 + } 1.19 result = tree; 1.20 } 1.21
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/lambda/Intersection03.java Mon Apr 15 14:17:30 2013 +0100 2.3 @@ -0,0 +1,49 @@ 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. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 8011392 2.30 + * @summary Missing checkcast when casting to intersection type 2.31 + */ 2.32 +import java.util.*; 2.33 + 2.34 +public class Intersection03 { 2.35 + 2.36 + static int assertionCount = 0; 2.37 + 2.38 + static void assertTrue(boolean cond) { 2.39 + assertionCount++; 2.40 + if (!cond) throw new AssertionError(); 2.41 + } 2.42 + 2.43 + public static void main(String[] args) { 2.44 + try { 2.45 + Runnable r = (List<?> & Runnable)new ArrayList<String>(); 2.46 + assertTrue(false); 2.47 + } catch (ClassCastException cce) { 2.48 + assertTrue(true); 2.49 + } 2.50 + assertTrue(assertionCount == 1); 2.51 + } 2.52 +}