Search in sources :

Example 66 with Statement

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

the class TryWithResourceRefactoring method visit.

@Override
public boolean visit(TryStatement node) {
    final List<Statement> tryStmts = asList(node.getBody());
    if (tryStmts.size() >= 1 && tryStmts.get(0).getNodeType() == TRY_STATEMENT) {
        final TryStatement innerTryStmt = as(tryStmts.get(0), TryStatement.class);
        if (innerTryStmt != null && !innerTryStmt.resources().isEmpty() && innerTryStmt.catchClauses().isEmpty()) {
            return collapseTryStatements(node, innerTryStmt);
        }
    }
    final VariableDeclarationStatement previousDeclStmt = as(getPreviousStatement(node), VariableDeclarationStatement.class);
    if (previousDeclStmt == null) {
        return VISIT_SUBTREE;
    }
    final VariableDeclarationFragment previousDeclFragment = getUniqueFragment(previousDeclStmt);
    final List<Statement> finallyStmts = asList(node.getFinally());
    if (previousDeclFragment != null && finallyStmts.size() >= 1) {
        final List<ASTNode> nodesToRemove = new ArrayList<ASTNode>();
        nodesToRemove.add(previousDeclStmt);
        final Statement finallyStmt = finallyStmts.get(0);
        nodesToRemove.add(finallyStmts.size() == 1 ? node.getFinally() : finallyStmt);
        final ExpressionStatement finallyEs = as(finallyStmt, ExpressionStatement.class);
        final IfStatement finallyIs = as(finallyStmt, IfStatement.class);
        if (finallyEs != null) {
            final MethodInvocation mi = as(finallyEs.getExpression(), MethodInvocation.class);
            if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, mi.getExpression())) {
                final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
                return refactorToTryWithResources(node, newResource, nodesToRemove);
            }
        } else if (finallyIs != null && asList(finallyIs.getThenStatement()).size() == 1 && asList(finallyIs.getElseStatement()).isEmpty()) {
            final Expression nullCheckedExpr = getNullCheckedExpression(finallyIs.getExpression());
            final Statement thenStmt = asList(finallyIs.getThenStatement()).get(0);
            final MethodInvocation mi = asExpression(thenStmt, MethodInvocation.class);
            if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, nullCheckedExpr, mi.getExpression())) {
                final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
                return refactorToTryWithResources(node, newResource, nodesToRemove);
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 67 with Statement

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

the class UpdateSetRatherThanTestingFirstRefactoring method visit.

@Override
public boolean visit(IfStatement node) {
    final Statement elseStmt = node.getElseStatement();
    final Statement thenStmt = node.getThenStatement();
    final PrefixExpression pe = as(node.getExpression(), PrefixExpression.class);
    if (hasOperator(pe, NOT)) {
        return maybeReplaceSetContains(node, pe.getOperand(), thenStmt, elseStmt, false);
    } else {
        return maybeReplaceSetContains(node, node.getExpression(), elseStmt, thenStmt, true);
    }
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 68 with Statement

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

the class SuperCallRatherThanUselessOverridingRefactoring method visit.

@Override
public boolean visit(MethodDeclaration node) {
    if (node.getBody() == null) {
        return VISIT_SUBTREE;
    }
    List<Statement> bodyStmts = statements(node.getBody());
    if (bodyStmts.size() == 1) {
        SuperMethodInvocation bodyMi = asExpression(bodyStmts.get(0), SuperMethodInvocation.class);
        if (bodyMi != null) {
            IMethodBinding bodyMethodBinding = bodyMi.resolveMethodBinding();
            IMethodBinding declMethodBinding = node.resolveBinding();
            if (declMethodBinding != null && bodyMethodBinding != null && declMethodBinding.overrides(bodyMethodBinding) && !hasSignificantAnnotations(declMethodBinding) && haveSameModifiers(bodyMethodBinding, declMethodBinding)) {
                if (Modifier.isProtected(declMethodBinding.getModifiers()) && !declaredInSamePackage(bodyMethodBinding, declMethodBinding)) {
                    // protected also means package visibility, so check if it is required
                    if (!isMethodUsedInItsPackage(declMethodBinding, node)) {
                        this.ctx.getRefactorings().remove(node);
                        return DO_NOT_VISIT_SUBTREE;
                    }
                } else {
                    this.ctx.getRefactorings().remove(node);
                    return DO_NOT_VISIT_SUBTREE;
                }
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Statement(org.eclipse.jdt.core.dom.Statement) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Aggregations

Statement (org.eclipse.jdt.core.dom.Statement)68 ForStatement (org.eclipse.jdt.core.dom.ForStatement)42 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)40 IfStatement (org.eclipse.jdt.core.dom.IfStatement)39 Block (org.eclipse.jdt.core.dom.Block)37 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)37 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)35 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)32 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)32 DoStatement (org.eclipse.jdt.core.dom.DoStatement)31 Expression (org.eclipse.jdt.core.dom.Expression)31 ASTNode (org.eclipse.jdt.core.dom.ASTNode)29 AST (org.eclipse.jdt.core.dom.AST)25 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)25 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)23 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)18 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)17 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)17 CastExpression (org.eclipse.jdt.core.dom.CastExpression)15 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)15