use of com.google.devtools.j2objc.ast.SwitchCase in project j2objc by google.
the class SwitchRewriter method endVisit.
@Override
public void endVisit(SwitchStatement node) {
fixVariableDeclarations(node);
fixStringValue(node);
fixEnumValue(node);
List<Statement> stmts = node.getStatements();
Statement lastStmt = stmts.get(stmts.size() - 1);
if (!stmts.isEmpty() && lastStmt instanceof SwitchCase) {
// Last switch case doesn't have an associated statement, so add an empty one
// with the same line number as the switch case to keep line numbers synced.
EmptyStatement emptyStmt = new EmptyStatement();
emptyStmt.setLineNumber(lastStmt.getLineNumber());
stmts.add(emptyStmt);
}
}
use of com.google.devtools.j2objc.ast.SwitchCase in project j2objc by google.
the class SwitchRewriter method fixStringValue.
private void fixStringValue(SwitchStatement node) {
Expression expr = node.getExpression();
TypeMirror type = expr.getTypeMirror();
if (!typeUtil.isString(type)) {
return;
}
ArrayType arrayType = typeUtil.getArrayType(type);
ArrayInitializer arrayInit = new ArrayInitializer(arrayType);
int idx = 0;
for (Statement stmt : node.getStatements()) {
if (stmt instanceof SwitchCase) {
SwitchCase caseStmt = (SwitchCase) stmt;
if (!caseStmt.isDefault()) {
arrayInit.addExpression(TreeUtil.remove(caseStmt.getExpression()));
caseStmt.setExpression(NumberLiteral.newIntLiteral(idx++, typeUtil));
}
}
}
TypeMirror intType = typeUtil.getInt();
FunctionElement indexOfFunc = new FunctionElement("JreIndexOfStr", intType, null).addParameters(type, arrayType, intType);
FunctionInvocation invocation = new FunctionInvocation(indexOfFunc, intType);
invocation.addArgument(TreeUtil.remove(expr)).addArgument(arrayInit).addArgument(NumberLiteral.newIntLiteral(idx, typeUtil));
node.setExpression(invocation);
}
Aggregations