Mon, 20 Aug 2012 21:24:10 +0530
7181320: javac NullPointerException for switch labels with cast to String expressions
Reviewed-by: mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Fri Aug 17 17:30:03 2012 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Mon Aug 20 21:24:10 2012 +0530 1.3 @@ -1589,9 +1589,16 @@ 1.4 * type parameters in t are deleted. 1.5 */ 1.6 public Type erasure(Type t) { 1.7 - return erasure(t, false); 1.8 + return eraseNotNeeded(t)? t : erasure(t, false); 1.9 } 1.10 //where 1.11 + private boolean eraseNotNeeded(Type t) { 1.12 + // We don't want to erase primitive types and String type as that 1.13 + // operation is idempotent. Also, erasing these could result in loss 1.14 + // of information such as constant values attached to such types. 1.15 + return (t.tag <= lastBaseTag) || (syms.stringType.tsym == t.tsym); 1.16 + } 1.17 + 1.18 private Type erasure(Type t, boolean recurse) { 1.19 if (t.tag <= lastBaseTag) 1.20 return t; /* fast special case */
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Lower.java Fri Aug 17 17:30:03 2012 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java Mon Aug 20 21:24:10 2012 +0530 2.3 @@ -3499,7 +3499,6 @@ 2.4 JCExpression expression = oneCase.getExpression(); 2.5 2.6 if (expression != null) { // expression for a "default" case is null 2.7 - expression = TreeInfo.skipParens(expression); 2.8 String labelExpr = (String) expression.type.constValue(); 2.9 Integer mapping = caseLabelToPosition.put(labelExpr, casePosition); 2.10 Assert.checkNull(mapping);
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/StringsInSwitch/7181320/BinOpInCaseLabel.java Mon Aug 20 21:24:10 2012 +0530 3.3 @@ -0,0 +1,38 @@ 3.4 +/* 3.5 + * Copyright (c) 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test 3.29 + * @bug 7181320 3.30 + * @summary javac NullPointerException for switch labels with cast to String expressions 3.31 + * @compile BinOpInCaseLabel.java 3.32 + */ 3.33 + 3.34 +public class BinOpInCaseLabel { 3.35 + public static void main(String [] args) { 3.36 + switch (args[0]) { 3.37 + case "hello" + "world": 3.38 + break; 3.39 + } 3.40 + } 3.41 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/StringsInSwitch/7181320/CastInCaseLabel.java Mon Aug 20 21:24:10 2012 +0530 4.3 @@ -0,0 +1,38 @@ 4.4 +/* 4.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test 4.29 + * @bug 7181320 4.30 + * @summary javac NullPointerException for switch labels with cast to String expressions 4.31 + * @compile CastInCaseLabel.java 4.32 + */ 4.33 + 4.34 +public class CastInCaseLabel { 4.35 + public static void main(String [] args) { 4.36 + switch (args[0]) { 4.37 + case (String)"hello": 4.38 + break; 4.39 + } 4.40 + } 4.41 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/StringsInSwitch/7181320/CondExprInCaseLabel.java Mon Aug 20 21:24:10 2012 +0530 5.3 @@ -0,0 +1,39 @@ 5.4 +/* 5.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. 5.11 + * 5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 + * version 2 for more details (a copy is included in the LICENSE file that 5.16 + * accompanied this code). 5.17 + * 5.18 + * You should have received a copy of the GNU General Public License version 5.19 + * 2 along with this work; if not, write to the Free Software Foundation, 5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 + * 5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.23 + * or visit www.oracle.com if you need additional information or have any 5.24 + * questions. 5.25 + */ 5.26 + 5.27 +/* 5.28 + * @test 5.29 + * @bug 7181320 5.30 + * @summary javac NullPointerException for switch labels with cast to String expressions 5.31 + * @compile CondExprInCaseLabel.java 5.32 + */ 5.33 + 5.34 +public class CondExprInCaseLabel { 5.35 + public static void main(String [] args) { 5.36 + final boolean cond = true; 5.37 + switch (args[0]) { 5.38 + case cond ? "hello" : "world": 5.39 + break; 5.40 + } 5.41 + } 5.42 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/StringsInSwitch/7181320/CondExprInCaseLabel1.java Mon Aug 20 21:24:10 2012 +0530 6.3 @@ -0,0 +1,39 @@ 6.4 +/* 6.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. 6.11 + * 6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.15 + * version 2 for more details (a copy is included in the LICENSE file that 6.16 + * accompanied this code). 6.17 + * 6.18 + * You should have received a copy of the GNU General Public License version 6.19 + * 2 along with this work; if not, write to the Free Software Foundation, 6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.21 + * 6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.23 + * or visit www.oracle.com if you need additional information or have any 6.24 + * questions. 6.25 + */ 6.26 + 6.27 +/* 6.28 + * @test 6.29 + * @bug 7181320 6.30 + * @summary javac NullPointerException for switch labels with cast to String expressions 6.31 + * @compile CondExprInCaseLabel1.java 6.32 + */ 6.33 + 6.34 +public class CondExprInCaseLabel1 { 6.35 + public static void main(String [] args) { 6.36 + final boolean cond = true; 6.37 + switch (args[0]) { 6.38 + case cond ? (String)"hello" : "world": 6.39 + break; 6.40 + } 6.41 + } 6.42 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/StringsInSwitch/7181320/CondExprInCaseLabel2.java Mon Aug 20 21:24:10 2012 +0530 7.3 @@ -0,0 +1,39 @@ 7.4 +/* 7.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 + * 7.8 + * This code is free software; you can redistribute it and/or modify it 7.9 + * under the terms of the GNU General Public License version 2 only, as 7.10 + * published by the Free Software Foundation. 7.11 + * 7.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 7.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.15 + * version 2 for more details (a copy is included in the LICENSE file that 7.16 + * accompanied this code). 7.17 + * 7.18 + * You should have received a copy of the GNU General Public License version 7.19 + * 2 along with this work; if not, write to the Free Software Foundation, 7.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.21 + * 7.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.23 + * or visit www.oracle.com if you need additional information or have any 7.24 + * questions. 7.25 + */ 7.26 + 7.27 +/* 7.28 + * @test 7.29 + * @bug 7181320 7.30 + * @summary javac NullPointerException for switch labels with cast to String expressions 7.31 + * @compile CondExprInCaseLabel2.java 7.32 + */ 7.33 + 7.34 +public class CondExprInCaseLabel2 { 7.35 + public static void main(String [] args) { 7.36 + final boolean cond = true; 7.37 + switch (args[0]) { 7.38 + case cond ? "hello" : (String)"world": 7.39 + break; 7.40 + } 7.41 + } 7.42 +}