Search in sources :

Example 6 with ArrayInitializer

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

Example 7 with ArrayInitializer

use of com.google.devtools.j2objc.ast.ArrayInitializer in project j2objc by google.

the class TranslationUtil method createObjectArray.

public Expression createObjectArray(List<Expression> expressions, ArrayType arrayType) {
    if (expressions.isEmpty()) {
        return new ArrayCreation(arrayType, typeUtil, 0);
    }
    ArrayCreation creation = new ArrayCreation(arrayType, typeUtil);
    ArrayInitializer initializer = new ArrayInitializer(arrayType);
    initializer.getExpressions().addAll(expressions);
    creation.setInitializer(initializer);
    return creation;
}
Also used : ArrayCreation(com.google.devtools.j2objc.ast.ArrayCreation) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Example 8 with ArrayInitializer

use of com.google.devtools.j2objc.ast.ArrayInitializer in project j2objc by google.

the class VarargsRewriter method rewriteVarargs.

private void rewriteVarargs(ExecutableElement method, TypeMirror varargsType, List<Expression> args) {
    if (varargsType == null) {
        return;
    }
    varargsType = typeUtil.erasure(varargsType);
    int numRegularParams = method.getParameters().size() - 1;
    List<Expression> varargs = args.subList(numRegularParams, args.size());
    List<Expression> varargsCopy = Lists.newArrayList(varargs);
    varargs.clear();
    if (varargsCopy.isEmpty()) {
        args.add(new ArrayCreation(typeUtil.getArrayType(varargsType), typeUtil, 0));
    } else {
        ArrayInitializer newInit = new ArrayInitializer(typeUtil.getArrayType(varargsType));
        newInit.getExpressions().addAll(varargsCopy);
        args.add(new ArrayCreation(newInit));
    }
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) ArrayCreation(com.google.devtools.j2objc.ast.ArrayCreation) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Aggregations

ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)8 Expression (com.google.devtools.j2objc.ast.Expression)7 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)5 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)5 ArrayCreation (com.google.devtools.j2objc.ast.ArrayCreation)3 TypeMirror (javax.lang.model.type.TypeMirror)3 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)2 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)2 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)2 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)2 SimpleName (com.google.devtools.j2objc.ast.SimpleName)2 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)2 TypeLiteral (com.google.devtools.j2objc.ast.TypeLiteral)2 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)2 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)2 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)2