7071246: Enclosing string literal in parenthesis in switch-case crashes javac

Thu, 04 Aug 2011 11:15:37 -0700

author
darcy
date
Thu, 04 Aug 2011 11:15:37 -0700
changeset 1063
64b9b7ae3366
parent 1062
0d6d41563040
child 1064
c0d5f93af048

7071246: Enclosing string literal in parenthesis in switch-case crashes javac
Reviewed-by: mcimadamore

src/share/classes/com/sun/tools/javac/comp/Lower.java file | annotate | diff | comparison | revisions
test/tools/javac/StringsInSwitch/StringSwitches.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Lower.java	Wed Jul 27 11:53:17 2011 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java	Thu Aug 04 11:15:37 2011 -0700
     1.3 @@ -3450,6 +3450,7 @@
     1.4                  JCExpression expression = oneCase.getExpression();
     1.5  
     1.6                  if (expression != null) { // expression for a "default" case is null
     1.7 +                    expression = TreeInfo.skipParens(expression);
     1.8                      String labelExpr = (String) expression.type.constValue();
     1.9                      Integer mapping = caseLabelToPosition.put(labelExpr, casePosition);
    1.10                      Assert.checkNull(mapping);
    1.11 @@ -3555,8 +3556,8 @@
    1.12                  if (isDefault)
    1.13                      caseExpr = null;
    1.14                  else {
    1.15 -                    caseExpr = make.Literal(caseLabelToPosition.get((String)oneCase.
    1.16 -                                                                    getExpression().
    1.17 +                    caseExpr = make.Literal(caseLabelToPosition.get((String)TreeInfo.skipParens(oneCase.
    1.18 +                                                                                                getExpression()).
    1.19                                                                      type.constValue()));
    1.20                  }
    1.21  
     2.1 --- a/test/tools/javac/StringsInSwitch/StringSwitches.java	Wed Jul 27 11:53:17 2011 -0700
     2.2 +++ b/test/tools/javac/StringsInSwitch/StringSwitches.java	Thu Aug 04 11:15:37 2011 -0700
     2.3 @@ -1,5 +1,5 @@
     2.4  /*
     2.5 - * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
     2.6 + * Copyright (c) 2009, 2011 Oracle and/or its affiliates. All rights reserved.
     2.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.8   *
     2.9   * This code is free software; you can redistribute it and/or modify it
    2.10 @@ -23,7 +23,7 @@
    2.11  
    2.12  /*
    2.13   * @test
    2.14 - * @bug 6827009
    2.15 + * @bug 6827009 7071246
    2.16   * @summary Positive tests for strings in switch.
    2.17   * @author  Joseph D. Darcy
    2.18   */
    2.19 @@ -36,6 +36,7 @@
    2.20          failures += testPileup();
    2.21          failures += testSwitchingTwoWays();
    2.22          failures += testNamedBreak();
    2.23 +        failures += testExtraParens();
    2.24  
    2.25          if (failures > 0) {
    2.26              throw new RuntimeException();
    2.27 @@ -260,4 +261,19 @@
    2.28          result |= (1<<5);
    2.29          return result;
    2.30      }
    2.31 +
    2.32 +    private static int testExtraParens() {
    2.33 +        int failures = 1;
    2.34 +        String s = "first";
    2.35 +
    2.36 +        switch(s) {
    2.37 +        case (("first")):
    2.38 +            failures = 0;
    2.39 +            break;
    2.40 +        case ("second"):
    2.41 +            throw new RuntimeException("Should not be reached.");
    2.42 +        }
    2.43 +
    2.44 +        return failures;
    2.45 +    }
    2.46  }

mercurial