Search in sources :

Example 6 with SwitchStatement

use of com.google.devtools.j2objc.ast.SwitchStatement 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

Statement (com.google.devtools.j2objc.ast.Statement)6 SwitchStatement (com.google.devtools.j2objc.ast.SwitchStatement)6 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)5 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)4 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)4 Expression (com.google.devtools.j2objc.ast.Expression)3 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)3 Block (com.google.devtools.j2objc.ast.Block)2 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)2 ContinueStatement (com.google.devtools.j2objc.ast.ContinueStatement)2 DoStatement (com.google.devtools.j2objc.ast.DoStatement)2 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)2 ForStatement (com.google.devtools.j2objc.ast.ForStatement)2 IfStatement (com.google.devtools.j2objc.ast.IfStatement)2 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2 SwitchCase (com.google.devtools.j2objc.ast.SwitchCase)2 ThrowStatement (com.google.devtools.j2objc.ast.ThrowStatement)2 TryStatement (com.google.devtools.j2objc.ast.TryStatement)2 WhileStatement (com.google.devtools.j2objc.ast.WhileStatement)2