Example usage for org.eclipse.jdt.core.dom Block STATEMENTS_PROPERTY

List of usage examples for org.eclipse.jdt.core.dom Block STATEMENTS_PROPERTY

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Block STATEMENTS_PROPERTY.

Prototype

ChildListPropertyDescriptor STATEMENTS_PROPERTY

To view the source code for org.eclipse.jdt.core.dom Block STATEMENTS_PROPERTY.

Click Source Link

Document

The "statements" structural property of this node type (element type: Statement ).

Usage

From source file:org.moe.natjgen.ConstructorEditor.java

License:Apache License

public void setBodyAsSuperInitWithFieldSetters(String[] setters, String[] getters) {
    if (setters == null || getters == null || setters.length != getters.length) {
        throw new IllegalArgumentException();
    }/* w  w w  . j  av a 2s .  c  o  m*/

    Block block = getAST().newBlock();
    getRewrite().set(methodDecl, MethodDeclaration.BODY_PROPERTY, block, getEditGroup());
    ListRewrite block_stmts = getRewrite().getListRewrite(block, Block.STATEMENTS_PROPERTY);
    SuperConstructorInvocation invoke = getAST().newSuperConstructorInvocation();
    block_stmts.insertLast(invoke, getEditGroup());
    ListRewrite invoke_args = getRewrite().getListRewrite(invoke,
            SuperConstructorInvocation.ARGUMENTS_PROPERTY);
    TypeLiteral literal = getAST().newTypeLiteral();
    invoke_args.insertLast(literal, getEditGroup());
    getRewrite().set(literal, TypeLiteral.TYPE_PROPERTY,
            getAST().newSimpleType(getAST().newName(getManager().getUnitName())), getEditGroup());

    for (int i = 0; i < setters.length; i++) {
        MethodInvocation setfieldinvoke = getAST().newMethodInvocation();
        Statement stmt = getAST().newExpressionStatement(setfieldinvoke);
        block_stmts.insertLast(stmt, getEditGroup());
        getRewrite().set(setfieldinvoke, MethodInvocation.NAME_PROPERTY, getAST().newSimpleName(setters[i]),
                getEditGroup());
        ListRewrite arglrw = getRewrite().getListRewrite(setfieldinvoke, MethodInvocation.ARGUMENTS_PROPERTY);
        arglrw.insertLast(getAST().newSimpleName(getters[i]), getEditGroup());
    }
}

From source file:org.moe.natjgen.InitializerEditor.java

License:Apache License

@SuppressWarnings("unchecked")
public boolean hasNatJRegister() {
    Block block = (Block) getRewrite().get(decl, Initializer.BODY_PROPERTY);

    if (block == null) {
        return false;
    }//www  . java  2  s  .  c o m

    ListRewrite block_stmts = getRewrite().getListRewrite(block, Block.STATEMENTS_PROPERTY);
    for (Statement stmt : (List<Statement>) block_stmts.getRewrittenList()) {
        if (stmt instanceof ExpressionStatement) {
            Expression expr = (Expression) getRewrite().get(stmt, ExpressionStatement.EXPRESSION_PROPERTY);
            if (expr instanceof MethodInvocation) {
                Expression cls = (Expression) getRewrite().get(expr, MethodInvocation.EXPRESSION_PROPERTY);
                if (!(cls instanceof SimpleName)) {
                    continue;
                }

                String identifier = (String) getRewrite().get(cls, SimpleName.IDENTIFIER_PROPERTY);
                if (!identifier.equals(Constants.NatJ) && !identifier.equals(Constants.NatJFQ)) {
                    continue;
                }

                SimpleName method = (SimpleName) getRewrite().get(expr, MethodInvocation.NAME_PROPERTY);
                String methodId = (String) getRewrite().get(method, SimpleName.IDENTIFIER_PROPERTY);
                if (methodId.equals(Constants.NatJRegisterMethod)) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:org.moe.natjgen.InitializerEditor.java

License:Apache License

public void insertNatJRegister() throws GeneratorException {
    editLock();//from www .  ja  va2s  .co m

    Block block = (Block) getRewrite().get(decl, Initializer.BODY_PROPERTY);

    if (block == null) {
        block = getAST().newBlock();
        getRewrite().set(decl, Initializer.BODY_PROPERTY, block, getEditGroup());
    }

    ListRewrite block_stmts = getRewrite().getListRewrite(block, Block.STATEMENTS_PROPERTY);
    MethodInvocation invoke = getAST().newMethodInvocation();
    ExpressionStatement expr_stmt = getAST().newExpressionStatement(invoke);
    block_stmts.insertFirst(expr_stmt, getEditGroup());
    getRewrite().set(invoke, MethodInvocation.EXPRESSION_PROPERTY,
            getAST().newSimpleName(getManager().addImport(Constants.NatJFQ)), getEditGroup());
    getRewrite().set(invoke, MethodInvocation.NAME_PROPERTY,
            getAST().newSimpleName(Constants.NatJRegisterMethod), getEditGroup());
}

From source file:org.moe.natjgen.MethodEditor.java

License:Apache License

@SuppressWarnings("unchecked")
public void setBodyAsMethodCaller(String jmethod, Type rettype, ArrayList<CalleeArgument> arguments) {
    Block code = (Block) getRewrite().get(methodDecl, MethodDeclaration.BODY_PROPERTY);
    if (code == null) {
        code = getAST().newBlock();/*from ww w .j a  v  a 2s  . c o m*/
        getRewrite().set(methodDecl, MethodDeclaration.BODY_PROPERTY, code, getEditGroup());
    }
    ListRewrite statements = getRewrite().getListRewrite(code, Block.STATEMENTS_PROPERTY);
    for (ASTNode object : (List<ASTNode>) statements.getRewrittenList()) {
        statements.remove(object, getEditGroup());
    }

    MethodInvocation invoke = getAST().newMethodInvocation();
    getRewrite().set(invoke, MethodInvocation.NAME_PROPERTY, getAST().newSimpleName(jmethod), getEditGroup());

    if (rettype.isVoid()) {
        ExpressionStatement expr_stmt = getAST().newExpressionStatement(invoke);
        statements.insertLast(expr_stmt, getEditGroup());
    } else {
        ReturnStatement return_stmt = getAST().newReturnStatement();
        statements.insertLast(return_stmt, getEditGroup());
        getRewrite().set(return_stmt, ReturnStatement.EXPRESSION_PROPERTY, invoke, getEditGroup());
    }

    if (arguments.size() > 0) {
        ListRewrite args = getRewrite().getListRewrite(invoke, MethodInvocation.ARGUMENTS_PROPERTY);
        for (CalleeArgument arg : arguments) {
            args.insertLast(getAST().newSimpleName(arg.getName()), getEditGroup());
        }
    }
}

From source file:org.moe.natjgen.MethodEditor.java

License:Apache License

@SuppressWarnings("unchecked")
public void setBodyAsProtocolDefaultMethod(Type rettype) {
    Block code = (Block) getRewrite().get(methodDecl, MethodDeclaration.BODY_PROPERTY);
    if (code == null) {
        code = getAST().newBlock();/*from   w  w w.j a v  a  2  s . c o  m*/
        getRewrite().set(methodDecl, MethodDeclaration.BODY_PROPERTY, code, getEditGroup());
    }
    ListRewrite statements = getRewrite().getListRewrite(code, Block.STATEMENTS_PROPERTY);
    for (ASTNode object : (List<ASTNode>) statements.getRewrittenList()) {
        statements.remove(object, getEditGroup());
    }

    Name uoe = getAST().newName("java.lang.UnsupportedOperationException");
    SimpleType uoet = getAST().newSimpleType(uoe);
    ClassInstanceCreation cic = getAST().newClassInstanceCreation();
    getRewrite().set(cic, ClassInstanceCreation.TYPE_PROPERTY, uoet, getEditGroup());
    ThrowStatement th = getAST().newThrowStatement();
    getRewrite().set(th, ThrowStatement.EXPRESSION_PROPERTY, cic, getEditGroup());
    statements.insertLast(th, getEditGroup());
}

From source file:org.moe.natjgen.MethodEditor.java

License:Apache License

@SuppressWarnings("unchecked")
public void updateSafePropertyBinding(String getter, String setter, CalleeArgument calleeArgument) {
    Block block = (Block) getRewrite().get(methodDecl, MethodDeclaration.BODY_PROPERTY);
    if (block == null) {
        block = getAST().newBlock();//from  ww w . j a v a 2  s.  c o m
        getRewrite().set(methodDecl, MethodDeclaration.BODY_PROPERTY, block, getEditGroup());
    }
    ListRewrite block_stmts = getRewrite().getListRewrite(block, Block.STATEMENTS_PROPERTY);
    for (ASTNode object : (List<ASTNode>) block_stmts.getRewrittenList()) {
        block_stmts.remove(object, getEditGroup());
    }

    /**
     * <ObjCRuntimeFQ>
     */
    Name objc_runtime = getAST().newName(Constants.ObjCRuntimeFQ);

    /**
     * Object __old = <getter>()
     */
    SimpleType objcobj_type = getAST().newSimpleType(getAST().newName("Object"));
    MethodInvocation getter_inv = getAST().newMethodInvocation();
    getRewrite().set(getter_inv, MethodInvocation.NAME_PROPERTY, getAST().newName(getter), getEditGroup());

    Name old_obj = getAST().newName("__old");
    VariableDeclarationFragment vdf = getAST().newVariableDeclarationFragment();
    getRewrite().set(vdf, VariableDeclarationFragment.NAME_PROPERTY, old_obj, getEditGroup());
    getRewrite().set(vdf, VariableDeclarationFragment.INITIALIZER_PROPERTY, getter_inv, getEditGroup());
    VariableDeclarationExpression vde = getAST().newVariableDeclarationExpression(vdf);
    getRewrite().set(vde, VariableDeclarationExpression.TYPE_PROPERTY,
            ASTNode.copySubtree(getAST(), objcobj_type), getEditGroup());
    insertStatement(block_stmts, vde);

    /**
     * <ObjCRuntimeFQ>.associateObjCObject(this, <argument>)
     */
    MethodInvocation assoc_inv = getAST().newMethodInvocation();
    getRewrite().set(assoc_inv, MethodInvocation.EXPRESSION_PROPERTY, objc_runtime, getEditGroup());
    getRewrite().set(assoc_inv, MethodInvocation.NAME_PROPERTY, getAST().newName("associateObjCObject"),
            getEditGroup());
    ListRewrite assoc_inv_args = getRewrite().getListRewrite(assoc_inv, MethodInvocation.ARGUMENTS_PROPERTY);
    assoc_inv_args.insertLast(getAST().newThisExpression(), getEditGroup());
    assoc_inv_args.insertLast(getAST().newSimpleName(calleeArgument.getName()), getEditGroup());

    /**
     * if (<arg> != null) { ObjCRuntime.associateObjCObject(this, <arg>); }
     */
    {
        Block con_block = getAST().newBlock();
        ListRewrite con_block_stmt = getRewrite().getListRewrite(con_block, Block.STATEMENTS_PROPERTY);
        insertStatement(con_block_stmt, assoc_inv);

        InfixExpression con_obj_nonnull = getAST().newInfixExpression();
        getRewrite().set(con_obj_nonnull, InfixExpression.OPERATOR_PROPERTY,
                InfixExpression.Operator.NOT_EQUALS, getEditGroup());
        getRewrite().set(con_obj_nonnull, InfixExpression.LEFT_OPERAND_PROPERTY,
                getAST().newSimpleName(calleeArgument.getName()), getEditGroup());
        getRewrite().set(con_obj_nonnull, InfixExpression.RIGHT_OPERAND_PROPERTY, getAST().newNullLiteral(),
                getEditGroup());

        IfStatement if_obj_nonnull = getAST().newIfStatement();
        getRewrite().set(if_obj_nonnull, IfStatement.EXPRESSION_PROPERTY, con_obj_nonnull, getEditGroup());
        getRewrite().set(if_obj_nonnull, IfStatement.THEN_STATEMENT_PROPERTY, con_block, getEditGroup());
        block_stmts.insertLast(if_obj_nonnull, getEditGroup());
    }

    /**
     * <setter>(...)
     */
    MethodInvocation setter_inv = getAST().newMethodInvocation();
    getRewrite().set(setter_inv, MethodInvocation.NAME_PROPERTY, getAST().newName(setter), getEditGroup());
    ListRewrite setter_inv_args = getRewrite().getListRewrite(setter_inv, MethodInvocation.ARGUMENTS_PROPERTY);
    setter_inv_args.insertLast(getAST().newSimpleName(calleeArgument.getName()), getEditGroup());
    insertStatement(block_stmts, setter_inv);

    /**
     * <ObjCRuntimeFQ>.dissociateObjCObject(this, old)
     */
    MethodInvocation unassoc_inv = getAST().newMethodInvocation();
    getRewrite().set(unassoc_inv, MethodInvocation.EXPRESSION_PROPERTY,
            ASTNode.copySubtree(getAST(), objc_runtime), getEditGroup());
    getRewrite().set(unassoc_inv, MethodInvocation.NAME_PROPERTY, getAST().newName("dissociateObjCObject"),
            getEditGroup());
    ListRewrite unassoc_inv_args = getRewrite().getListRewrite(unassoc_inv,
            MethodInvocation.ARGUMENTS_PROPERTY);
    unassoc_inv_args.insertLast(getAST().newThisExpression(), getEditGroup());
    unassoc_inv_args.insertLast(ASTNode.copySubtree(getAST(), old_obj), getEditGroup());

    /**
     * if (__old != null) { ObjCRuntime.dissociateObjCObject(this, __old); }
     */
    {
        Block con_block = getAST().newBlock();
        ListRewrite con_block_stmt = getRewrite().getListRewrite(con_block, Block.STATEMENTS_PROPERTY);
        insertStatement(con_block_stmt, unassoc_inv);

        InfixExpression con_obj_nonnull = getAST().newInfixExpression();
        getRewrite().set(con_obj_nonnull, InfixExpression.OPERATOR_PROPERTY,
                InfixExpression.Operator.NOT_EQUALS, getEditGroup());
        getRewrite().set(con_obj_nonnull, InfixExpression.LEFT_OPERAND_PROPERTY,
                ASTNode.copySubtree(getAST(), old_obj), getEditGroup());
        getRewrite().set(con_obj_nonnull, InfixExpression.RIGHT_OPERAND_PROPERTY, getAST().newNullLiteral(),
                getEditGroup());

        IfStatement if_obj_nonnull = getAST().newIfStatement();
        getRewrite().set(if_obj_nonnull, IfStatement.EXPRESSION_PROPERTY, con_obj_nonnull, getEditGroup());
        getRewrite().set(if_obj_nonnull, IfStatement.THEN_STATEMENT_PROPERTY, con_block, getEditGroup());
        block_stmts.insertLast(if_obj_nonnull, getEditGroup());
    }
}