Example usage for org.eclipse.jdt.core.dom Statement equals

List of usage examples for org.eclipse.jdt.core.dom Statement equals

Introduction

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

Prototype

@Override
public final boolean equals(Object obj) 

Source Link

Document

The ASTNode implementation of this Object method uses object identity (==).

Usage

From source file:com.motorolamobility.preflighting.samplechecker.findviewbyid.quickfix.FindViewByIdMarkerResolution.java

License:Apache License

private int getLoopStatementIndex(ASTNode loopNode, List<Statement> statements) {
    int i = 0;//from   ww  w .  j  a v a2  s .c  o m
    for (i = 0; i < statements.size(); i++) {
        Statement statement = statements.get(i);
        if (statement.equals(loopNode)) {
            break;
        }
    }
    return i;
}

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

License:Open Source License

private boolean maybeReplaceWithCollectionContains(Statement forNode, Expression iterable,
        Expression loopElement, IfStatement is) {
    if (is != null && is.getElseStatement() == null && instanceOf(iterable, "java.util.Collection")) {
        MethodInvocation cond = as(is.getExpression(), MethodInvocation.class);
        List<Statement> thenStmts = asList(is.getThenStatement());
        if (!thenStmts.isEmpty() && isMethod(cond, "java.lang.Object", "equals", "java.lang.Object")) {
            Expression toFind = getExpressionToFind(cond, loopElement);
            if (toFind != null) {
                if (thenStmts.size() == 1) {
                    Statement forNextStmt = getNextStatement(forNode);
                    Statement thenStmt = thenStmts.get(0);
                    Boolean negate = negateCollectionContains(thenStmt, forNextStmt);
                    if (negate != null) {
                        ASTBuilder b = ctx.getASTBuilder();
                        ctx.getRefactorings().replace(forNode,
                                b.return0(collectionContains(iterable, toFind, negate, b)));
                        if (forNextStmt.equals(getNextSibling(forNode))) {
                            ctx.getRefactorings().remove(forNextStmt);
                        }/*from w  w w.j  a va 2  s  .  c  om*/
                        return DO_NOT_VISIT_SUBTREE;
                    }
                    return maybeReplaceWithCollectionContains0(forNode, iterable, thenStmt, toFind);
                } else if (thenStmts.size() == 2) {
                    BreakStatement bs = as(thenStmts.get(1), BreakStatement.class);
                    if (bs != null && bs.getLabel() == null) {
                        return maybeReplaceWithCollectionContains0(forNode, iterable, thenStmts.get(0), toFind);
                    }
                }
            }
        }
    }
    return VISIT_SUBTREE;
}

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

License:Open Source License

private boolean maybeReplaceWithCollectionContains0(Statement forNode, Expression iterable,
        Statement uniqueThenStmt, Expression toFind) {
    Statement previousStmt = getPreviousStatement(forNode);
    if (previousStmt == null) {
        return VISIT_SUBTREE;
    }// ww  w . j a v a 2 s  .  c om
    boolean previousStmtIsPreviousSibling = previousStmt.equals(getPreviousSibling(forNode));
    Assignment as = asExpression(uniqueThenStmt, Assignment.class);
    Pair<Name, Expression> innerInit = decomposeInitializer(as);
    Name initName = innerInit.getFirst();
    Expression init2 = innerInit.getSecond();
    Pair<Name, Expression> outerInit = getInitializer(previousStmt);
    if (isSameVariable(outerInit.getFirst(), initName)) {
        Boolean negate2 = negateCollectionContains((BooleanLiteral) init2,
                (BooleanLiteral) outerInit.getSecond());
        if (negate2 != null) {
            ASTBuilder b = ctx.getASTBuilder();
            Statement replacement;
            if (previousStmtIsPreviousSibling && previousStmt instanceof VariableDeclarationStatement) {
                replacement = b.declare("boolean", b.copy((SimpleName) initName),
                        collectionContains(iterable, toFind, negate2, b));
            } else if (!previousStmtIsPreviousSibling || previousStmt instanceof ExpressionStatement) {
                replacement = b.toStmt(
                        b.assign(b.copy(initName), ASSIGN, collectionContains(iterable, toFind, negate2, b)));
            } else {
                throw new NotImplementedException(forNode);
            }
            ctx.getRefactorings().replace(forNode, replacement);
            if (previousStmtIsPreviousSibling) {
                ctx.getRefactorings().remove(previousStmt);
            }
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}

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

License:Open Source License

private void replace(VariableAccess varDecl, VariableAccess varAccess) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final AST ast = b.getAST();
    final ASTNode scope = varAccess.getScope();
    final Name varName = varAccess.getVariableName();
    final Type varType = getType(varDecl.getVariableName().getParent());
    if (scope instanceof Block) {
        final List<Statement> stmts = statements((Block) scope);
        for (int i = 0; i < stmts.size(); i++) {
            final Statement stmt = stmts.get(i);
            final Expression parentExpr = getAncestor(varName, Expression.class); // FIXME i=0
            final Statement parentStmt = getAncestor(parentExpr, Statement.class); // FIXME i=0
            if (stmt.equals(parentStmt)) {
                final VariableDeclarationFragment vdf = getVariableDeclarationFragment(parentExpr, varName);
                final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
                vds.setType(varType);/*from  ww w. ja va 2 s.  c o m*/
                this.ctx.getRefactorings().replace(stmt, vds);
                break;
            }
        }
    } else if (scope instanceof EnhancedForStatement) {
        final EnhancedForStatement efs = (EnhancedForStatement) scope;
        final EnhancedForStatement newEfs = b.copy(efs);
        newEfs.setParameter(b.copy(efs.getParameter()));
        newEfs.setExpression(b.copy(efs.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(efs.getBody(), parentStmt)) {
            newEfs.setBody(copy(efs.getBody(), varName));
        }
        this.ctx.getRefactorings().replace(efs, newEfs);
    } else if (scope instanceof ForStatement) {
        final ForStatement fs = (ForStatement) scope;
        final ForStatement newFs = b.copy(fs);
        final List<Expression> initializers = initializers(newFs);
        if (initializers.size() == 1) {
            final Expression init = initializers.remove(0);
            final VariableDeclarationFragment vdf = getVariableDeclarationFragment(init, varName);
            final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(vdf);
            vde.setType(varType);
            initializers.add(vde);
            this.ctx.getRefactorings().replace(fs, newFs);
            // TODO JNR
            // if (equalNotNull(fs.getBody(), parentStmt)) {
            // newFs.setBody(copy(fs.getBody()));
            // }
        } else {
            throw new NotImplementedException(scope, "for more than one initializer in for loop.");
        }
    } else if (scope instanceof WhileStatement) {
        final WhileStatement ws = (WhileStatement) scope;
        final WhileStatement newWs = ast.newWhileStatement();
        newWs.setExpression(b.copy(ws.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(ws.getBody(), parentStmt)) {
            newWs.setBody(copy(ws.getBody(), varName));
        }
        this.ctx.getRefactorings().replace(ws, newWs);
    } else if (scope instanceof IfStatement) {
        final IfStatement is = (IfStatement) scope;
        final IfStatement newIs = ast.newIfStatement();
        newIs.setExpression(b.copy(is.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(is.getThenStatement(), parentStmt)) {
            newIs.setThenStatement(copy(is.getThenStatement(), varName));
            if (is.getElseStatement() != null) {
                newIs.setElseStatement(b.copy(is.getElseStatement()));
            }
            this.ctx.getRefactorings().replace(is, newIs);
        } else if (equalNotNull(is.getElseStatement(), parentStmt)) {
            if (is.getThenStatement() != null) {
                newIs.setThenStatement(b.copy(is.getThenStatement()));
            }
            newIs.setElseStatement(copy(is.getElseStatement(), varName));
            this.ctx.getRefactorings().replace(is, newIs);
        } else {
            throw new IllegalStateException(is,
                    "Parent statement should be inside the then or else statement of this if statement: " + is);
        }
    } else {
        throw new NotImplementedException(scope);
    }
}

From source file:org.eclipse.wb.internal.rcp.nebula.ctabletree.CTableTreeInfo.java

License:Open Source License

/**
 * Ensures that all children are added before association {@link Statement}.
 *//*from w ww  .ja  v a  2  s . c om*/
private void addBroadcastListener_forTarget() {
    addBroadcastListener(new JavaEventListener() {
        @Override
        public void target_isTerminalStatement(JavaInfo parent, JavaInfo child, Statement statement,
                boolean[] terminal) {
            if (parent == m_this) {
                if (child instanceof CContainerColumnInfo) {
                    // locate first CTableTreeItem 
                    List<CTableTreeItemInfo> items = getChildren(CTableTreeItemInfo.class);
                    for (CTableTreeItemInfo item : items) {
                        List<ASTNode> itemNodes = item.getRelatedNodes();
                        for (ASTNode node : itemNodes) {
                            ASTNode current = node;
                            do {
                                if (statement.equals(current)) {
                                    terminal[0] = true;
                                    return;
                                }
                                current = current.getParent();
                            } while (current != null);
                        }
                    }
                }
            }
            super.target_isTerminalStatement(parent, child, statement, terminal);
        }
    });
}

From source file:org.eclipse.wb.internal.rcp.nebula.grid.GridInfo.java

License:Open Source License

/**
 * Ensures that all children are added before association {@link Statement}.
 *///from   w w w.ja  v  a2s  .c  o m
private void addBroadcastListener_forTarget() {
    addBroadcastListener(new JavaEventListener() {
        @Override
        public void target_isTerminalStatement(JavaInfo parent, JavaInfo child, Statement statement,
                boolean[] terminal) {
            if (parent == m_this && (child instanceof GridColumnInfo || child instanceof GridColumnGroupInfo)) {
                List<GridItemInfo> items = getChildren(GridItemInfo.class);
                for (GridItemInfo item : items) {
                    List<ASTNode> itemNodes = item.getRelatedNodes();
                    for (ASTNode node : itemNodes) {
                        ASTNode current = node;
                        do {
                            if (statement.equals(current)) {
                                terminal[0] = true;
                                return;
                            }
                            current = current.getParent();
                        } while (current != null);
                    }
                }
            }
        }
    });
}