7071246: Enclosing string literal in parenthesis in switch-case crashes javac
authordarcy
Thu, 04 Aug 2011 11:15:37 -0700
changeset 10201 52da97a48f81
parent 10200 56cc93e7b6ef
child 10202 f32c7f2c2219
7071246: Enclosing string literal in parenthesis in switch-case crashes javac Reviewed-by: mcimadamore
langtools/src/share/classes/com/sun/tools/javac/comp/Lower.java
langtools/test/tools/javac/StringsInSwitch/StringSwitches.java
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Lower.java	Wed Jul 27 11:53:17 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Lower.java	Thu Aug 04 11:15:37 2011 -0700
@@ -3450,6 +3450,7 @@
                 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);
@@ -3555,8 +3556,8 @@
                 if (isDefault)
                     caseExpr = null;
                 else {
-                    caseExpr = make.Literal(caseLabelToPosition.get((String)oneCase.
-                                                                    getExpression().
+                    caseExpr = make.Literal(caseLabelToPosition.get((String)TreeInfo.skipParens(oneCase.
+                                                                                                getExpression()).
                                                                     type.constValue()));
                 }
 
--- a/langtools/test/tools/javac/StringsInSwitch/StringSwitches.java	Wed Jul 27 11:53:17 2011 -0700
+++ b/langtools/test/tools/javac/StringsInSwitch/StringSwitches.java	Thu Aug 04 11:15:37 2011 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2011 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
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 6827009
+ * @bug 6827009 7071246
  * @summary Positive tests for strings in switch.
  * @author  Joseph D. Darcy
  */
@@ -36,6 +36,7 @@
         failures += testPileup();
         failures += testSwitchingTwoWays();
         failures += testNamedBreak();
+        failures += testExtraParens();
 
         if (failures > 0) {
             throw new RuntimeException();
@@ -260,4 +261,19 @@
         result |= (1<<5);
         return result;
     }
+
+    private static int testExtraParens() {
+        int failures = 1;
+        String s = "first";
+
+        switch(s) {
+        case (("first")):
+            failures = 0;
+            break;
+        case ("second"):
+            throw new RuntimeException("Should not be reached.");
+        }
+
+        return failures;
+    }
 }