Mercurial > hg > release > icedtea7-forest-2.6 > langtools
changeset 1313:a2f36e3acda6
7181320: javac NullPointerException for switch labels with cast to String expressions
Reviewed-by: mcimadamore
author | sundar |
---|---|
date | Mon, 10 Sep 2012 12:08:56 +0530 |
parents | c7e82bdf46af |
children | fdbdbf39564d |
files | src/share/classes/com/sun/tools/javac/code/Types.java src/share/classes/com/sun/tools/javac/comp/Lower.java test/tools/javac/StringsInSwitch/7181320/BinOpInCaseLabel.java test/tools/javac/StringsInSwitch/7181320/CastInCaseLabel.java test/tools/javac/StringsInSwitch/7181320/CondExprInCaseLabel.java test/tools/javac/StringsInSwitch/7181320/CondExprInCaseLabel1.java test/tools/javac/StringsInSwitch/7181320/CondExprInCaseLabel2.java |
diffstat | 7 files changed, 199 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/code/Types.java Fri Sep 07 09:03:32 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Mon Sep 10 12:08:56 2012 +0530 @@ -1616,9 +1616,14 @@ * type parameters in t are deleted. */ public Type erasure(Type t) { - return erasure(t, false); + return eraseNotNeeded(t)? t : erasure(t, false); } //where + private boolean eraseNotNeeded(Type t) { + return (t.tag <= lastBaseTag) || (syms.stringType.tsym == t.tsym); + } + + //where private Type erasure(Type t, boolean recurse) { if (t.tag <= lastBaseTag) return t; /* fast special case */
--- a/src/share/classes/com/sun/tools/javac/comp/Lower.java Fri Sep 07 09:03:32 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java Mon Sep 10 12:08:56 2012 +0530 @@ -3458,7 +3458,6 @@ JCExpression expression = oneCase.getExpression(); if (expression != null) { // expression for a "default" case is null - expression = TreeInfo.skipParens(expression); String labelExpr = (String) expression.type.constValue(); Integer mapping = caseLabelToPosition.put(labelExpr, casePosition); Assert.checkNull(mapping);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/StringsInSwitch/7181320/BinOpInCaseLabel.java Mon Sep 10 12:08:56 2012 +0530 @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2012, 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. + */ + +/* + * @test + * @bug 7181320 + * @summary javac NullPointerException for switch labels with cast to String expressions + * @compile BinOpInCaseLabel.java + */ + +public class BinOpInCaseLabel { + public static void main(String [] args) { + switch (args[0]) { + case "hello" + "world": + break; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/StringsInSwitch/7181320/CastInCaseLabel.java Mon Sep 10 12:08:56 2012 +0530 @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2012, 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. + */ + +/* + * @test + * @bug 7181320 + * @summary javac NullPointerException for switch labels with cast to String expressions + * @compile CastInCaseLabel.java + */ + +public class CastInCaseLabel { + public static void main(String [] args) { + switch (args[0]) { + case (String)"hello": + break; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/StringsInSwitch/7181320/CondExprInCaseLabel.java Mon Sep 10 12:08:56 2012 +0530 @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2012, 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. + */ + +/* + * @test + * @bug 7181320 + * @summary javac NullPointerException for switch labels with cast to String expressions + * @compile CondExprInCaseLabel.java + */ + +public class CondExprInCaseLabel { + public static void main(String [] args) { + final boolean cond = true; + switch (args[0]) { + case cond ? "hello" : "world": + break; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/StringsInSwitch/7181320/CondExprInCaseLabel1.java Mon Sep 10 12:08:56 2012 +0530 @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2012, 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. + */ + +/* + * @test + * @bug 7181320 + * @summary javac NullPointerException for switch labels with cast to String expressions + * @compile CondExprInCaseLabel1.java + */ + +public class CondExprInCaseLabel1 { + public static void main(String [] args) { + final boolean cond = true; + switch (args[0]) { + case cond ? (String)"hello" : "world": + break; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/StringsInSwitch/7181320/CondExprInCaseLabel2.java Mon Sep 10 12:08:56 2012 +0530 @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2012, 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. + */ + +/* + * @test + * @bug 7181320 + * @summary javac NullPointerException for switch labels with cast to String expressions + * @compile CondExprInCaseLabel2.java + */ + +public class CondExprInCaseLabel2 { + public static void main(String [] args) { + final boolean cond = true; + switch (args[0]) { + case cond ? "hello" : (String)"world": + break; + } + } +}