Search in sources :

Example 31 with Block

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

the class Functionizer method setFunctionCaller.

/**
   *  Replace method block statements with single statement that invokes function.
   */
private void setFunctionCaller(MethodDeclaration method, ExecutableElement methodElement) {
    TypeMirror returnType = methodElement.getReturnType();
    TypeElement declaringClass = ElementUtil.getDeclaringClass(methodElement);
    Block body = new Block();
    method.setBody(body);
    method.removeModifiers(Modifier.NATIVE);
    List<Statement> stmts = body.getStatements();
    FunctionInvocation invocation = new FunctionInvocation(newFunctionElement(methodElement), returnType);
    List<Expression> args = invocation.getArguments();
    if (!ElementUtil.isStatic(methodElement)) {
        args.add(new ThisExpression(declaringClass.asType()));
    }
    for (SingleVariableDeclaration param : method.getParameters()) {
        args.add(new SimpleName(param.getVariableElement()));
    }
    if (TypeUtil.isVoid(returnType)) {
        stmts.add(new ExpressionStatement(invocation));
        if (ElementUtil.isConstructor(methodElement)) {
            stmts.add(new ReturnStatement(new ThisExpression(declaringClass.asType())));
        }
    } else {
        stmts.add(new ReturnStatement(invocation));
    }
}
Also used : FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeMirror(javax.lang.model.type.TypeMirror) Expression(com.google.devtools.j2objc.ast.Expression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 32 with Block

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

the class MetadataWriter method visitType.

private void visitType(AbstractTypeDeclaration node) {
    TypeElement type = node.getTypeElement();
    if (!translationUtil.needsReflection(type)) {
        return;
    }
    ExecutableElement metadataElement = GeneratedExecutableElement.newMethodWithSelector("__metadata", CLASS_INFO_TYPE, type).addModifiers(Modifier.STATIC, Modifier.PRIVATE);
    MethodDeclaration metadataDecl = new MethodDeclaration(metadataElement);
    metadataDecl.setHasDeclaration(false);
    Block body = new Block();
    metadataDecl.setBody(body);
    new MetadataGenerator(node, body.getStatements()).generateClassMetadata();
    node.addBodyDeclaration(metadataDecl);
}
Also used : GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Block(com.google.devtools.j2objc.ast.Block)

Example 33 with Block

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

the class NilCheckResolver method visit.

@Override
public boolean visit(MethodDeclaration node) {
    Block body = node.getBody();
    if (body != null) {
        pushFirstScope();
        body.accept(this);
        popLastScope();
    }
    return false;
}
Also used : Block(com.google.devtools.j2objc.ast.Block)

Example 34 with Block

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

the class NilCheckResolver method visit.

@Override
public boolean visit(TryStatement node) {
    pushTryScope();
    for (VariableDeclarationExpression resource : node.getResources()) {
        resource.accept(this);
    }
    node.getBody().accept(this);
    popAndMerge();
    pushScope();
    for (CatchClause catchClause : node.getCatchClauses()) {
        scope.mergeDownAndReset();
        catchClause.accept(this);
    }
    popAndMerge();
    Block finallyBlock = node.getFinally();
    if (finallyBlock != null) {
        finallyBlock.accept(this);
    }
    return false;
}
Also used : VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) Block(com.google.devtools.j2objc.ast.Block) CatchClause(com.google.devtools.j2objc.ast.CatchClause)

Example 35 with Block

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

the class EnhancedForRewriter method makeBlock.

private Block makeBlock(Statement stmt) {
    if (stmt instanceof Block) {
        return (Block) stmt;
    }
    Block block = new Block();
    if (stmt.getParent() != null) {
        stmt.replaceWith(block);
    }
    block.addStatement(stmt);
    return block;
}
Also used : Block(com.google.devtools.j2objc.ast.Block)

Aggregations

Block (com.google.devtools.j2objc.ast.Block)38 Statement (com.google.devtools.j2objc.ast.Statement)18 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)13 ExecutableElement (javax.lang.model.element.ExecutableElement)13 TypeElement (javax.lang.model.element.TypeElement)12 SimpleName (com.google.devtools.j2objc.ast.SimpleName)11 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)11 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)10 VariableElement (javax.lang.model.element.VariableElement)10 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)9 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)9 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)8 ForStatement (com.google.devtools.j2objc.ast.ForStatement)8 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)8 TypeMirror (javax.lang.model.type.TypeMirror)8 Expression (com.google.devtools.j2objc.ast.Expression)7 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)7 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)6 IfStatement (com.google.devtools.j2objc.ast.IfStatement)6 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)6