Search in sources :

Example 86 with Block

use of org.eclipse.jdt.core.dom.Block in project processing by processing.

the class CompletionGenerator method findClosestNode.

protected static ASTNode findClosestNode(int lineNumber, ASTNode node) {
    log("findClosestNode to line " + lineNumber);
    ASTNode parent = findClosestParentNode(lineNumber, node);
    log("findClosestParentNode returned " + getNodeAsString(parent));
    if (parent == null)
        return null;
    if (getLineNumber(parent) == lineNumber) {
        log(parent + "|PNode " + getLineNumber(parent) + ", lfor " + lineNumber);
        return parent;
    }
    List<ASTNode> nodes;
    if (parent instanceof TypeDeclaration) {
        nodes = ((TypeDeclaration) parent).bodyDeclarations();
    } else if (parent instanceof Block) {
        nodes = ((Block) parent).statements();
    } else {
        log("findClosestNode() found " + getNodeAsString(parent));
        return null;
    }
    if (nodes.size() > 0) {
        ASTNode retNode = parent;
        for (ASTNode cNode : nodes) {
            log(cNode + "|cNode " + getLineNumber(cNode) + ", lfor " + lineNumber);
            if (getLineNumber(cNode) <= lineNumber)
                retNode = cNode;
        }
        return retNode;
    }
    return parent;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 87 with Block

use of org.eclipse.jdt.core.dom.Block in project AutoRefactor by JnRouvignac.

the class AddBracketsToControlStatementRefactoring method setBlock.

private boolean setBlock(Statement statement) {
    if (statement == null) {
        return VISIT_SUBTREE;
    }
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Block block = b.block(b.copy(statement));
    block.accept(this);
    this.ctx.getRefactorings().replace(statement, block);
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Block(org.eclipse.jdt.core.dom.Block) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 88 with Block

use of org.eclipse.jdt.core.dom.Block in project AutoRefactor by JnRouvignac.

the class CommonCodeInIfElseStatementRefactoring method visit.

// TODO handle switch statements
// TODO also handle ternary operator, ConditionalExpression
@Override
public boolean visit(IfStatement node) {
    if (node.getElseStatement() == null) {
        return VISIT_SUBTREE;
    }
    if (!(node.getParent() instanceof Block)) {
        // when not inside curly braces
        return VISIT_SUBTREE;
    }
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    final List<List<Statement>> allCasesStmts = new ArrayList<List<Statement>>();
    final List<List<ASTNode>> removedCaseStmts = new LinkedList<List<ASTNode>>();
    // collect all the if / else if / else if / ... / else cases
    if (collectAllCases(allCasesStmts, node)) {
        // initialize removedCaseStmts list
        for (int i = 0; i < allCasesStmts.size(); i++) {
            removedCaseStmts.add(new LinkedList<ASTNode>());
        }
        // if all cases exist
        final ASTMatcher matcher = new ASTMatcherSameVariablesAndMethods();
        final int minSize = minSize(allCasesStmts);
        final List<Statement> caseStmts = allCasesStmts.get(0);
        boolean result = VISIT_SUBTREE;
        // identify matching statements starting from the beginning of each case
        for (int stmtIndex = 0; stmtIndex < minSize; stmtIndex++) {
            if (!match(matcher, allCasesStmts, true, stmtIndex, 0, allCasesStmts.size())) {
                break;
            }
            r.insertBefore(b.copy(caseStmts.get(stmtIndex)), node);
            removeStmts(allCasesStmts, true, stmtIndex, removedCaseStmts);
            result = DO_NOT_VISIT_SUBTREE;
        }
        // identify matching statements starting from the end of each case
        for (int stmtIndex = 1; 0 <= minSize - stmtIndex; stmtIndex++) {
            if (!match(matcher, allCasesStmts, false, stmtIndex, 0, allCasesStmts.size()) || anyContains(removedCaseStmts, allCasesStmts, stmtIndex)) {
                break;
            }
            r.insertAfter(b.copy(caseStmts.get(caseStmts.size() - stmtIndex)), node);
            removeStmts(allCasesStmts, false, stmtIndex, removedCaseStmts);
            result = DO_NOT_VISIT_SUBTREE;
        }
        // remove the nodes common to all cases
        final List<Boolean> areCasesEmpty = new ArrayList<Boolean>(allCasesStmts.size());
        for (int i = 0; i < allCasesStmts.size(); i++) {
            areCasesEmpty.add(Boolean.FALSE);
        }
        removeStmtsFromCases(allCasesStmts, removedCaseStmts, areCasesEmpty);
        if (allEmpty(areCasesEmpty)) {
            // TODO JNR keep comments
            r.remove(node);
            return DO_NOT_VISIT_SUBTREE;
        }
        // remove empty cases
        if (areCasesEmpty.get(0)) {
            if (areCasesEmpty.size() == 2 && !areCasesEmpty.get(1)) {
                // then clause is empty and there is only one else clause
                // => revert if statement
                r.replace(node, b.if0(b.not(b.parenthesizeIfNeeded(b.move(node.getExpression()))), b.move(node.getElseStatement())));
            } else {
                r.replace(node.getThenStatement(), b.block());
            }
            result = DO_NOT_VISIT_SUBTREE;
        }
        for (int i = 1; i < areCasesEmpty.size(); i++) {
            if (areCasesEmpty.get(i)) {
                final Statement firstStmt = allCasesStmts.get(i).get(0);
                r.remove(findNodeToRemove(firstStmt, firstStmt.getParent()));
                result = DO_NOT_VISIT_SUBTREE;
            }
        }
        return result;
    }
    return VISIT_SUBTREE;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Refactorings(org.autorefactor.refactoring.Refactorings) ArrayList(java.util.ArrayList) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) LinkedList(java.util.LinkedList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ASTMatcherSameVariablesAndMethods(org.autorefactor.refactoring.ASTMatcherSameVariablesAndMethods) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 89 with Block

use of org.eclipse.jdt.core.dom.Block in project AutoRefactor by JnRouvignac.

the class IfElseIfRefactoring method visit.

// TODO JNR
// UseIfElseIfRefactoring
// if (b) {
// return i;
// }
// if (c) {
// return j;
// }
// if (d) {
// return k;
// }
// return l;
@Override
public boolean visit(IfStatement node) {
    final Statement elseStmt = node.getElseStatement();
    if (elseStmt instanceof Block) {
        List<Statement> elseStmts = statements((Block) elseStmt);
        if (elseStmts.size() == 1 && elseStmts.get(0) instanceof IfStatement) {
            final ASTBuilder b = this.ctx.getASTBuilder();
            this.ctx.getRefactorings().set(node, ELSE_STATEMENT_PROPERTY, b.copy(elseStmts.get(0)));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Block(org.eclipse.jdt.core.dom.Block) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

Block (org.eclipse.jdt.core.dom.Block)89 ASTNode (org.eclipse.jdt.core.dom.ASTNode)53 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)43 AST (org.eclipse.jdt.core.dom.AST)41 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)38 Statement (org.eclipse.jdt.core.dom.Statement)37 Expression (org.eclipse.jdt.core.dom.Expression)36 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)33 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)33 ForStatement (org.eclipse.jdt.core.dom.ForStatement)32 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)31 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)27 IfStatement (org.eclipse.jdt.core.dom.IfStatement)26 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)25 DoStatement (org.eclipse.jdt.core.dom.DoStatement)24 Type (org.eclipse.jdt.core.dom.Type)24 SimpleName (org.eclipse.jdt.core.dom.SimpleName)20 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)19 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)19 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)17