Search in sources :

Example 1 with SwitchCase

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);
    }
}
Also used : SwitchCase(com.google.devtools.j2objc.ast.SwitchCase) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement)

Example 2 with SwitchCase

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);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) SwitchCase(com.google.devtools.j2objc.ast.SwitchCase) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Aggregations

EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)2 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2 Statement (com.google.devtools.j2objc.ast.Statement)2 SwitchCase (com.google.devtools.j2objc.ast.SwitchCase)2 SwitchStatement (com.google.devtools.j2objc.ast.SwitchStatement)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)1 Expression (com.google.devtools.j2objc.ast.Expression)1 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)1 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)1 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)1 ArrayType (javax.lang.model.type.ArrayType)1 TypeMirror (javax.lang.model.type.TypeMirror)1