@Override public boolean enterCaseNode(final CaseNode caseNode) { enterDefault(caseNode); type("SwitchCase"); comma(); final Node test = caseNode.getTest(); property("test"); if (test != null) { test.accept(this); } else { nullValue(); } comma(); array("consequent", caseNode.getBody().getStatements()); return leave(); }
@Override public boolean enterSwitchNode(final SwitchNode switchNode) { final List<CaseNode> caseNodes = switchNode.getCases(); final List<CaseTreeImpl> caseTrees = new ArrayList<>(caseNodes.size()); for (final CaseNode caseNode : caseNodes) { final Block body = caseNode.getBody(); caseTrees.add( new CaseTreeImpl(caseNode, translateExpr(caseNode.getTest()), translateStats(body != null? body.getStatements() : null))); } curStat = new SwitchTreeImpl(switchNode, translateExpr(switchNode.getExpression()), caseTrees); return false; }
@Override public boolean enterSwitchNode(final SwitchNode switchNode) { switchNode.toString(sb); sb.append(" {"); final List<CaseNode> cases = switchNode.getCases(); for (final CaseNode caseNode : cases) { sb.append(EOLN); indent(); caseNode.toString(sb); indent += TABWIDTH; caseNode.getBody().accept(this); indent -= TABWIDTH; sb.append(EOLN); } sb.append(EOLN); indent(); sb.append("}"); return false; }
@Override public Node leaveSwitchNode(final SwitchNode switchNode) { final boolean allInteger = switchNode.getTag().getSymbolType().isInteger(); if (allInteger) { return switchNode; } final Expression expression = switchNode.getExpression(); final List<CaseNode> cases = switchNode.getCases(); final List<CaseNode> newCases = new ArrayList<>(); for (final CaseNode caseNode : cases) { final Expression test = caseNode.getTest(); newCases.add(test != null ? caseNode.setTest(convert(test, Type.OBJECT)) : caseNode); } return switchNode. setExpression(lc, convert(expression, Type.OBJECT)). setCases(lc, newCases); }
@Override public boolean enterSwitchNode(final SwitchNode switchNode) { switchNode.toString(sb, printTypes); sb.append(" {"); final List<CaseNode> cases = switchNode.getCases(); for (final CaseNode caseNode : cases) { sb.append(EOLN); indent(); caseNode.toString(sb, printTypes); printLocalVariableConversion(caseNode); indent += TABWIDTH; caseNode.getBody().accept(this); indent -= TABWIDTH; sb.append(EOLN); } if(switchNode.getLocalVariableConversion() != null) { sb.append(EOLN); indent(); sb.append("default: "); printLocalVariableConversion(switchNode); sb.append("{}"); } sb.append(EOLN); indent(); sb.append("}"); return false; }
private static boolean isUniqueIntegerSwitchNode(final SwitchNode switchNode) { final Set<Integer> alreadySeen = new HashSet<>(); for (final CaseNode caseNode : switchNode.getCases()) { final Expression test = caseNode.getTest(); if (test != null && !isUniqueIntegerLiteral(test, alreadySeen)) { return false; } } return true; }
@Override public Node leaveCaseNode(final CaseNode caseNode) { // Try to represent the case test as an integer final Node test = caseNode.getTest(); if (test instanceof LiteralNode) { final LiteralNode<?> lit = (LiteralNode<?>)test; if (lit.isNumeric() && !(lit.getValue() instanceof Integer)) { if (JSType.isRepresentableAsInt(lit.getNumber())) { return caseNode.setTest((Expression)LiteralNode.newInstance(lit, lit.getInt32()).accept(this)); } } } return caseNode; }
public CaseTreeImpl(final CaseNode node, final ExpressionTree expression, final List<? extends StatementTree> statements) { super(node); this.expression = expression; this.statements = statements; }