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

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

Introduction

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

Prototype

ASTNode.NodeList statements

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

Click Source Link

Document

The list of statements (element type: Statement ).

Usage

From source file:org.autorefactor.refactoring.ASTHelper.java

License:Open Source License

/**
 * Generecized version of the equivalent JDT method.
 *
 * @param node the node on which to call the equivalent JDT method
 * @return a List of expressions//from w  w w.j  a  v  a  2 s  .c  o m
 * @see Block#statements()
 */
@SuppressWarnings("unchecked")
public static List<Statement> statements(Block node) {
    return node.statements();
}

From source file:org.autorefactor.refactoring.ASTSemanticMatcher.java

License:Open Source License

private boolean match0(final Block node, final Statement other) {
    if ((node.getParent() instanceof IfStatement || node.getParent() instanceof ForStatement
            || node.getParent() instanceof EnhancedForStatement || node.getParent() instanceof WhileStatement
            || node.getParent() instanceof DoStatement) && node.statements().size() == 1) {
        return safeSubtreeMatch(node.statements().get(0), other) || super.match(node, other);
    }/*ww  w  .  ja v a  2  s . c o m*/

    return super.match(node, other);
}

From source file:org.autorefactor.refactoring.rules.RemoveEmptyStatementRefactoring.java

License:Open Source License

private boolean isEmptyCode(final Statement emptyCode) {
    if (emptyCode instanceof EmptyStatement) {
        return true;
    } else if (emptyCode instanceof Block) {
        final Block block = (Block) emptyCode;
        return block.statements() == null || block.statements().isEmpty();
    }//  w  w w .j  a  v a 2  s  . c o m
    return false;
}

From source file:org.autorefactor.refactoring.rules.ViewHolderRefactoring.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration node) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    IMethodBinding methodBinding = node.resolveBinding();

    if (methodBinding != null && isMethod(methodBinding, "android.widget.Adapter", "getView", "int",
            "android.view.View", "android.view.ViewGroup")) {
        GetViewVisitor visitor = new GetViewVisitor();
        Block body = node.getBody();/*  w  w w .  ja  va2  s.c o m*/
        if (body != null) {
            body.accept(visitor);
            if (!visitor.usesConvertView && visitor.viewVariable != null && !visitor.isInflateInsideIf()) {
                // Transform tree

                //Create If statement
                IfStatement ifStatement = b.getAST().newIfStatement();
                // test-clause
                InfixExpression infixExpression = b.getAST().newInfixExpression();
                infixExpression.setOperator(InfixExpression.Operator.EQUALS);
                infixExpression.setLeftOperand(b.simpleName("convertView"));
                infixExpression.setRightOperand(b.getAST().newNullLiteral());
                ifStatement.setExpression(infixExpression);
                //then
                Assignment assignment = b.assign(b.simpleName("convertView"), Assignment.Operator.ASSIGN,
                        b.copy(visitor.getInflateExpression()));
                Block thenBlock = b.block(b.getAST().newExpressionStatement(assignment));
                ifStatement.setThenStatement(thenBlock);
                r.insertBefore(ifStatement, visitor.viewAssignmentStatement);

                // assign to local view variable when necessary
                if (!"convertView".equals(visitor.viewVariable.getIdentifier())) {
                    Statement assignConvertViewToView = null;
                    if (visitor.viewVariableDeclarationFragment != null) {
                        assignConvertViewToView = b.declare(visitor.viewVariable.resolveTypeBinding().getName(),
                                b.copy(visitor.viewVariable), b.simpleName("convertView"));
                    } else if (visitor.viewVariableAssignment != null) {
                        assignConvertViewToView = b.getAST()
                                .newExpressionStatement(b.assign(b.copy(visitor.viewVariable),
                                        Assignment.Operator.ASSIGN, b.simpleName("convertView")));
                    }
                    if (assignConvertViewToView != null) {
                        r.insertBefore(assignConvertViewToView, visitor.viewAssignmentStatement);
                    }
                }

                // make sure method returns the view to be reused DELETEME
                if (visitor.returnStatement != null) {
                    r.insertAfter(b.return0(b.copy(visitor.viewVariable)), visitor.returnStatement);
                    r.remove(visitor.returnStatement);
                }

                //Optimize findViewById calls
                FindViewByIdVisitor findViewByIdVisitor = new FindViewByIdVisitor();
                body.accept(findViewByIdVisitor);
                if (findViewByIdVisitor.items.size() > 0) {
                    //create ViewHolderItem class
                    TypeDeclaration viewHolderItemDeclaration = b.getAST().newTypeDeclaration();
                    viewHolderItemDeclaration.setName(b.simpleName("ViewHolderItem"));
                    List<ASTNode> viewItemsDeclarations = viewHolderItemDeclaration.bodyDeclarations();
                    for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {

                        VariableDeclarationFragment declarationFragment = b.getAST()
                                .newVariableDeclarationFragment();
                        SimpleName simpleName = b.simpleName(item.variable.getIdentifier());
                        declarationFragment.setName(simpleName);
                        FieldDeclaration fieldDeclaration = b.getAST().newFieldDeclaration(declarationFragment);
                        fieldDeclaration.setType(b.getAST()
                                .newSimpleType(b.simpleName(item.variable.resolveTypeBinding().getName())));
                        viewItemsDeclarations.add(fieldDeclaration);
                    }
                    viewHolderItemDeclaration.modifiers()
                            .add(b.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD));
                    r.insertBefore(viewHolderItemDeclaration, node);
                    // create viewhHolderItem object
                    VariableDeclarationStatement viewHolderItemVariableDeclaration = b.declare("ViewHolderItem",
                            b.simpleName("viewHolderItem"), null);
                    r.insertAt(viewHolderItemVariableDeclaration, 0, Block.STATEMENTS_PROPERTY, body);
                    //initialize viewHolderItem
                    Assignment viewHolderItemInitialization = b.assign(b.simpleName("viewHolderItem"),
                            Assignment.Operator.ASSIGN, b.new0("ViewHolderItem"));
                    thenBlock.statements().add(b.getAST().newExpressionStatement(viewHolderItemInitialization));
                    //  Assign findViewById to ViewHolderItem
                    for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
                        //ensure we are accessing to convertView object
                        QualifiedName qualifiedName = b.getAST().newQualifiedName(b.name("viewHolderItem"),
                                b.simpleName(item.variable.getIdentifier()));
                        item.findViewByIdInvocation.setExpression(b.simpleName("convertView"));
                        Assignment itemAssignment = b.assign(qualifiedName, Assignment.Operator.ASSIGN,
                                (Expression) ASTNode.copySubtree(b.getAST(), item.findViewByIdExpression));

                        thenBlock.statements().add(b.getAST().newExpressionStatement(itemAssignment));

                        //replace previous fidnviewbyid with accesses to viewHolderItem
                        QualifiedName viewHolderItemFieldAccessQualifiedName = b.getAST().newQualifiedName(
                                b.name("viewHolderItem"), b.simpleName(item.variable.getIdentifier()));
                        r.replace(item.findViewByIdExpression, b.copy(qualifiedName));
                    }
                    //store viewHolderItem in convertView
                    MethodInvocation setTagInvocation = b.invoke("convertView", "setTag",
                            b.simpleName("viewHolderItem"));
                    thenBlock.statements().add(b.getAST().newExpressionStatement(setTagInvocation));

                    //retrieve viewHolderItem from convertView
                    ifStatement
                            .setElseStatement(b.block(b.getAST()
                                    .newExpressionStatement(b.assign(b.simpleName("viewHolderItem"),
                                            Assignment.Operator.ASSIGN,
                                            b.cast("ViewHolderItem", b.invoke("convertView", "getTag"))))));

                }
                r.remove(visitor.viewAssignmentStatement);
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}

From source file:org.decojer.cavaj.transformers.TrLineNumberAnalysis.java

License:Open Source License

private static void analyzeLines(final Block block, final Element declaration) {
    assert declaration != null;

    for (final Statement statement : (List<Statement>) block.statements()) {
        final Op op = getOp(statement);
        if (op == null) {
            // LOGGER.warning("Op is null for: '" + statement);
            // TODO can happen for while(true) {...}

            // TODO what is with sub nodes? really decend here? simply fill?
            continue;
        }/*from   w w  w .  j av  a 2 s.  c o  m*/
        // LOGGER.info("LINE: " + op.getLine() + " / " + bd);
    }
}

From source file:org.dyno.visual.swing.parser.listener.AbstractClassModel.java

License:Open Source License

private boolean createEventMethodForWidget(TypeDeclaration type, WidgetAdapter adapter, EventSetDescriptor esd,
        MethodDescriptor mListener, MethodDeclaration md) {
    Block body = md.getBody();
    List statements = body.statements();
    if (!adapter.isRoot() && !statements.isEmpty()) {
        Object firstStatement = statements.get(0);
        if (firstStatement instanceof IfStatement) {
            IfStatement ifstatement = (IfStatement) firstStatement;
            Statement thenstatement = ifstatement.getThenStatement();
            if (thenstatement instanceof Block) {
                statements = ((Block) thenstatement).statements();
            }//from  w  w  w  .ja  va2 s . c o  m
        }
    }
    boolean success = false;
    for (Object stmt : statements) {
        Statement statement = (Statement) stmt;
        if (statement instanceof ExpressionStatement) {
            if (processWidgetCreationStatement(type, adapter, esd, mListener, statement))
                success = true;
        }
    }
    return success;
}

From source file:org.dyno.visual.swing.parser.listener.AnonymousInnerModel.java

License:Open Source License

protected IEventMethod getDelegatingContent(WidgetAdapter adapter, EventSetDescriptor eventSet,
        MethodDescriptor methodDesc, Block body, SingleVariableDeclaration var) {
    List statements = body.statements();
    StringBuilder builder = new StringBuilder();
    for (Object stmt : statements) {
        builder.append(stmt.toString());
    }/*from w  w w.ja  v  a  2  s  .c o  m*/
    return new CodeSnippet(adapter, eventSet, methodDesc, builder.toString());
}

From source file:org.dyno.visual.swing.parser.listener.DelegationModel.java

License:Open Source License

protected IEventMethod getDelegatingContent(WidgetAdapter adapter, EventSetDescriptor eventSet,
        MethodDescriptor methodDesc, Block body, SingleVariableDeclaration var) {
    List statements = body.statements();
    if (statements.size() == 1) {
        Object stmt = statements.get(0);
        if (stmt instanceof ExpressionStatement) {
            ExpressionStatement es = (ExpressionStatement) stmt;
            Expression expression = es.getExpression();
            if (expression instanceof MethodInvocation) {
                MethodInvocation mi = (MethodInvocation) expression;
                Expression optional = mi.getExpression();
                if (optional == null || optional instanceof ThisExpression) {
                    List list = mi.arguments();
                    if (list.size() != 1) {
                        return new CodeSnippet(adapter, eventSet, methodDesc, es.toString());
                    }/*from  w  w  w  .  j  a  va2s .  c om*/
                    Expression exp = (Expression) list.get(0);
                    if (exp instanceof SimpleName) {
                        SimpleName sn = (SimpleName) exp;
                        SimpleName varn = var.getName();
                        if (sn.getFullyQualifiedName().equals(varn.getFullyQualifiedName()))
                            return new EventDelegation(methodDesc, mi.getName().getFullyQualifiedName());
                    }
                    return new CodeSnippet(adapter, eventSet, methodDesc, es.toString());
                } else {
                    return new CodeSnippet(adapter, eventSet, methodDesc, es.toString());
                }
            } else
                return new CodeSnippet(adapter, eventSet, methodDesc, es.toString());
        } else
            return new CodeSnippet(adapter, eventSet, methodDesc, stmt.toString());
    } else {
        StringBuilder builder = new StringBuilder();
        for (Object stmt : statements) {
            builder.append(stmt.toString());
        }
        return new CodeSnippet(adapter, eventSet, methodDesc, builder.toString());
    }
}

From source file:org.ebayopensource.dsf.javatojs.translate.OtherTranslator.java

License:Open Source License

public void processBlock(final Block astBlock, final JstBlock block) {

    StatementTranslator stmtTranslator = getStmtTranslator();

    for (Object o : astBlock.statements()) {
        if (o instanceof Statement) {
            IStmt s = stmtTranslator.processStatement((Statement) o, block);
            if (s != null) {
                block.addStmt(s);//from  ww w  .  j  a  va 2s  . c o  m
            }
        } else {
            getLogger().logUnhandledNode(this, (ASTNode) o, block);
        }
    }
}

From source file:org.ebayopensource.dsf.javatojs.translate.StatementTranslator.java

License:Open Source License

private ForStmt toForStmt(final ForStatement astStmt, final BaseJstNode jstBlock) {

    ExpressionTranslator exprTranslator = getExprTranslator();

    ForStmt forStmt = new ForStmt();
    jstBlock.addChild(forStmt);//from   w ww .  j  a v  a2  s  . c  o m
    JstInitializer jstInitializers = null;
    for (Object o : astStmt.initializers()) {
        if (o instanceof VariableDeclarationExpression) {
            VariableDeclarationExpression vde = (VariableDeclarationExpression) o;
            forStmt.setInitializer(exprTranslator.toJstVars(vde.getType(), vde.fragments(), jstBlock));
        } else if (o instanceof Assignment) {
            if (jstInitializers == null) {
                jstInitializers = new JstInitializer();
                forStmt.setInitializer(jstInitializers);
            }
            jstInitializers.addAssignment(exprTranslator.toAssignExpr((Assignment) o, jstBlock));
        } else {
            getLogger().logUnhandledNode(this, (ASTNode) o, jstBlock);
        }
    }
    Expression astCond = astStmt.getExpression();
    if (astCond != null && (astCond instanceof InfixExpression || astCond instanceof PrefixExpression)) {
        forStmt.setCondition(getExprTranslator().toBoolExpr(astCond, jstBlock));
    } else if (astCond instanceof MethodInvocation) {
        IExpr e = exprTranslator.processExpression(astCond, jstBlock);
        forStmt.setCondition(new BoolExpr(e));
    }
    for (Object o : astStmt.updaters()) {
        if (o instanceof Expression) {
            forStmt.addUpdater(getExprTranslator().processExpression((Expression) o, jstBlock));
        } else {
            getLogger().logUnhandledNode(this, (ASTNode) o, jstBlock);
        }
    }
    Statement body = astStmt.getBody();
    if (body instanceof Block) {
        Block block = (Block) body;
        for (Object o : block.statements()) {
            if (o instanceof Statement) {
                forStmt.addStmt(processStatement((Statement) o, forStmt.getBody()));
            } else {
                getLogger().logUnhandledNode(this, (ASTNode) o, jstBlock);
            }
        }
    } else if (body instanceof Statement) {
        forStmt.addStmt(processStatement((Statement) body, forStmt.getBody()));
    } else {
        getLogger().logUnhandledNode(this, (ASTNode) body, jstBlock);
    }

    return forStmt;
}