Search in sources :

Example 26 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement 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 27 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.

the class TranslationUtil method createAnnotation.

public Expression createAnnotation(AnnotationMirror annotationMirror) {
    DeclaredType type = annotationMirror.getAnnotationType();
    TypeElement typeElem = (TypeElement) type.asElement();
    FunctionElement element = new FunctionElement("create_" + nameTable.getFullName(typeElem), type, typeElem);
    FunctionInvocation invocation = new FunctionInvocation(element, type);
    Map<? extends ExecutableElement, ? extends AnnotationValue> values = typeUtil.elementUtil().getElementValuesWithDefaults(annotationMirror);
    for (ExecutableElement member : ElementUtil.getSortedAnnotationMembers(typeElem)) {
        TypeMirror valueType = member.getReturnType();
        element.addParameters(valueType);
        invocation.addArgument(createAnnotationValue(valueType, values.get(member)));
    }
    return invocation;
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

FunctionElement (com.google.devtools.j2objc.types.FunctionElement)27 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)25 TypeMirror (javax.lang.model.type.TypeMirror)20 Expression (com.google.devtools.j2objc.ast.Expression)14 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)12 TypeElement (javax.lang.model.element.TypeElement)11 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)10 PointerType (com.google.devtools.j2objc.types.PointerType)9 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)8 ExecutableElement (javax.lang.model.element.ExecutableElement)8 VariableElement (javax.lang.model.element.VariableElement)8 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)7 SimpleName (com.google.devtools.j2objc.ast.SimpleName)7 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)6 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)6 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)5 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)3 Assignment (com.google.devtools.j2objc.ast.Assignment)2 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2