src/share/classes/com/sun/tools/javac/comp/Lower.java

Print this page




3433              * particular set of case labels, could potentially be
3434              * used instead of String.hashCode.
3435              */
3436 
3437             ListBuffer<JCStatement> stmtList = new ListBuffer<JCStatement>();
3438 
3439             // Map from String case labels to their original position in
3440             // the list of case labels.
3441             Map<String, Integer> caseLabelToPosition =
3442                 new LinkedHashMap<String, Integer>(alternatives + 1, 1.0f);
3443 
3444             // Map of hash codes to the string case labels having that hashCode.
3445             Map<Integer, Set<String>> hashToString =
3446                 new LinkedHashMap<Integer, Set<String>>(alternatives + 1, 1.0f);
3447 
3448             int casePosition = 0;
3449             for(JCCase oneCase : caseList) {
3450                 JCExpression expression = oneCase.getExpression();
3451 
3452                 if (expression != null) { // expression for a "default" case is null

3453                     String labelExpr = (String) expression.type.constValue();
3454                     Integer mapping = caseLabelToPosition.put(labelExpr, casePosition);
3455                     Assert.checkNull(mapping);
3456                     int hashCode = labelExpr.hashCode();
3457 
3458                     Set<String> stringSet = hashToString.get(hashCode);
3459                     if (stringSet == null) {
3460                         stringSet = new LinkedHashSet<String>(1, 1.0f);
3461                         stringSet.add(labelExpr);
3462                         hashToString.put(hashCode, stringSet);
3463                     } else {
3464                         boolean added = stringSet.add(labelExpr);
3465                         Assert.check(added);
3466                     }
3467                 }
3468                 casePosition++;
3469             }
3470 
3471             // Synthesize a switch statement that has the effect of
3472             // mapping from a string to the integer position of that


3538 
3539             switch1.cases = caseBuffer.toList();
3540             stmtList.append(switch1);
3541 
3542             // Make isomorphic switch tree replacing string labels
3543             // with corresponding integer ones from the label to
3544             // position map.
3545 
3546             ListBuffer<JCCase> lb = ListBuffer.lb();
3547             JCSwitch switch2 = make.Switch(make.Ident(dollar_tmp), lb.toList());
3548             for(JCCase oneCase : caseList ) {
3549                 // Rewire up old unlabeled break statements to the
3550                 // replacement switch being created.
3551                 patchTargets(oneCase, tree, switch2);
3552 
3553                 boolean isDefault = (oneCase.getExpression() == null);
3554                 JCExpression caseExpr;
3555                 if (isDefault)
3556                     caseExpr = null;
3557                 else {
3558                     caseExpr = make.Literal(caseLabelToPosition.get((String)oneCase.
3559                                                                     getExpression().
3560                                                                     type.constValue()));
3561                 }
3562 
3563                 lb.append(make.Case(caseExpr,
3564                                     oneCase.getStatements()));
3565             }
3566 
3567             switch2.cases = lb.toList();
3568             stmtList.append(switch2);
3569 
3570             return make.Block(0L, stmtList.toList());
3571         }
3572     }
3573 
3574     public void visitNewArray(JCNewArray tree) {
3575         tree.elemtype = translate(tree.elemtype);
3576         for (List<JCExpression> t = tree.dims; t.tail != null; t = t.tail)
3577             if (t.head != null) t.head = translate(t.head, syms.intType);
3578         tree.elems = translate(tree.elems, types.elemtype(tree.type));
3579         result = tree;




3433              * particular set of case labels, could potentially be
3434              * used instead of String.hashCode.
3435              */
3436 
3437             ListBuffer<JCStatement> stmtList = new ListBuffer<JCStatement>();
3438 
3439             // Map from String case labels to their original position in
3440             // the list of case labels.
3441             Map<String, Integer> caseLabelToPosition =
3442                 new LinkedHashMap<String, Integer>(alternatives + 1, 1.0f);
3443 
3444             // Map of hash codes to the string case labels having that hashCode.
3445             Map<Integer, Set<String>> hashToString =
3446                 new LinkedHashMap<Integer, Set<String>>(alternatives + 1, 1.0f);
3447 
3448             int casePosition = 0;
3449             for(JCCase oneCase : caseList) {
3450                 JCExpression expression = oneCase.getExpression();
3451 
3452                 if (expression != null) { // expression for a "default" case is null
3453                     expression = TreeInfo.skipParens(expression);
3454                     String labelExpr = (String) expression.type.constValue();
3455                     Integer mapping = caseLabelToPosition.put(labelExpr, casePosition);
3456                     Assert.checkNull(mapping);
3457                     int hashCode = labelExpr.hashCode();
3458 
3459                     Set<String> stringSet = hashToString.get(hashCode);
3460                     if (stringSet == null) {
3461                         stringSet = new LinkedHashSet<String>(1, 1.0f);
3462                         stringSet.add(labelExpr);
3463                         hashToString.put(hashCode, stringSet);
3464                     } else {
3465                         boolean added = stringSet.add(labelExpr);
3466                         Assert.check(added);
3467                     }
3468                 }
3469                 casePosition++;
3470             }
3471 
3472             // Synthesize a switch statement that has the effect of
3473             // mapping from a string to the integer position of that


3539 
3540             switch1.cases = caseBuffer.toList();
3541             stmtList.append(switch1);
3542 
3543             // Make isomorphic switch tree replacing string labels
3544             // with corresponding integer ones from the label to
3545             // position map.
3546 
3547             ListBuffer<JCCase> lb = ListBuffer.lb();
3548             JCSwitch switch2 = make.Switch(make.Ident(dollar_tmp), lb.toList());
3549             for(JCCase oneCase : caseList ) {
3550                 // Rewire up old unlabeled break statements to the
3551                 // replacement switch being created.
3552                 patchTargets(oneCase, tree, switch2);
3553 
3554                 boolean isDefault = (oneCase.getExpression() == null);
3555                 JCExpression caseExpr;
3556                 if (isDefault)
3557                     caseExpr = null;
3558                 else {
3559                     caseExpr = make.Literal(caseLabelToPosition.get((String)TreeInfo.skipParens(oneCase.
3560                                                                                                 getExpression()).
3561                                                                     type.constValue()));
3562                 }
3563 
3564                 lb.append(make.Case(caseExpr,
3565                                     oneCase.getStatements()));
3566             }
3567 
3568             switch2.cases = lb.toList();
3569             stmtList.append(switch2);
3570 
3571             return make.Block(0L, stmtList.toList());
3572         }
3573     }
3574 
3575     public void visitNewArray(JCNewArray tree) {
3576         tree.elemtype = translate(tree.elemtype);
3577         for (List<JCExpression> t = tree.dims; t.tail != null; t = t.tail)
3578             if (t.head != null) t.head = translate(t.head, syms.intType);
3579         tree.elems = translate(tree.elems, types.elemtype(tree.type));
3580         result = tree;