Search in sources :

Example 26 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class SuperCallRatherThanUselessOverridingCleanUp method visit.

@Override
public boolean visit(final MethodDeclaration visited) {
    if (visited.getBody() == null) {
        return true;
    }
    List<Statement> bodyStatements = visited.getBody().statements();
    if (bodyStatements.size() == 1) {
        SuperMethodInvocation bodyMi = ASTNodes.asExpression(bodyStatements.get(0), SuperMethodInvocation.class);
        if (bodyMi != null) {
            IMethodBinding bodyMethodBinding = bodyMi.resolveMethodBinding();
            IMethodBinding declMethodBinding = visited.resolveBinding();
            if (declMethodBinding != null && bodyMethodBinding != null && declMethodBinding.overrides(bodyMethodBinding) && !hasSignificantAnnotations(declMethodBinding) && haveSameModifiers(bodyMethodBinding, declMethodBinding) && haveSameParameters(visited, bodyMi)) {
                if (!Modifier.isProtected(declMethodBinding.getModifiers()) || declaredInSamePackage(bodyMethodBinding, declMethodBinding) || // protected also means package visibility, so check if it is required
                !isMethodUsedInItsPackage(declMethodBinding, visited)) {
                    TextEditGroup group = new TextEditGroup(MultiFixMessages.SuperCallRatherThanUselessOverridingCleanUp_description);
                    ASTRewrite rewrite = cuRewrite.getASTRewrite();
                    rewrite.remove(visited, group);
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Statement(org.eclipse.jdt.core.dom.Statement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 27 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class TruncatingAppendingRatherThanSubCharactersCleanUp method replaceWithAppendSubstring.

private void replaceWithAppendSubstring(final MethodInvocation visited, final MethodInvocation truncatingMethod) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.TruncatingAppendingRatherThanSubCharactersCleanUp_description);
    Expression builder = ASTNodes.createMoveTarget(rewrite, visited.getExpression());
    Expression stringVar = ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(truncatingMethod.getExpression()));
    List<Expression> args = truncatingMethod.arguments();
    Expression arg0 = ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(args.get(0)));
    Expression arg1 = ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(args.get(1)));
    MethodInvocation appendMethod = ast.newMethodInvocation();
    appendMethod.setExpression(builder);
    // $NON-NLS-1$
    appendMethod.setName(ast.newSimpleName("append"));
    appendMethod.arguments().add(stringVar);
    appendMethod.arguments().add(arg0);
    appendMethod.arguments().add(arg1);
    rewrite.replace(visited, appendMethod, group);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 28 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class UpdateSetRatherThanTestingFirstCleanUp method maybeReplaceSetContains.

private boolean maybeReplaceSetContains(final IfStatement ifStmtToReplace, final Expression ifExpression, final Statement statement, final Statement oppositeStatement, final boolean negate, final String methodName) {
    List<Statement> statements = ASTNodes.asList(statement);
    MethodInvocation miContains = ASTNodes.as(ifExpression, MethodInvocation.class);
    if (!statements.isEmpty() && ASTNodes.usesGivenSignature(miContains, Set.class.getCanonicalName(), "contains", Object.class.getCanonicalName())) {
        // $NON-NLS-1$
        Statement firstStatement = statements.get(0);
        MethodInvocation miAddOrRemove = ASTNodes.asExpression(firstStatement, MethodInvocation.class);
        if (ASTNodes.usesGivenSignature(miAddOrRemove, Set.class.getCanonicalName(), methodName, Object.class.getCanonicalName()) && ASTNodes.match(miContains.getExpression(), miAddOrRemove.getExpression()) && ASTNodes.match(((List<Expression>) miContains.arguments()).get(0), ((List<Expression>) miAddOrRemove.arguments()).get(0))) {
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            ASTNodeFactory ast = cuRewrite.getASTBuilder();
            TextEditGroup group = new TextEditGroup(MultiFixMessages.UpdateSetRatherThanTestingFirstCleanUp_description);
            if (statements.size() == 1 && ASTNodes.asList(oppositeStatement).isEmpty()) {
                // Only one statement: replace if statement with col.add() (or col.remove())
                ASTNodes.replaceButKeepComment(rewrite, ifStmtToReplace, ASTNodes.createMoveTarget(rewrite, firstStatement), group);
            } else {
                // There are other statements, replace the if condition with col.add() (or
                // col.remove())
                rewrite.replace(ifStmtToReplace.getExpression(), negate ? ast.negate(miAddOrRemove, true) : ASTNodes.createMoveTarget(rewrite, miAddOrRemove), group);
                rewrite.remove(firstStatement, group);
            }
            return false;
        }
    }
    return true;
}
Also used : Set(java.util.Set) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) List(java.util.List) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 29 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class UseStringContainsCleanUp method replaceWithStringContains.

private void replaceWithStringContains(final InfixExpression visited, final MethodInvocation method, final boolean isPositive) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.UseStringContainsCleanUp_description);
    // $NON-NLS-1$
    rewrite.set(method, MethodInvocation.NAME_PROPERTY, ast.newSimpleName("contains"), group);
    if (isPositive) {
        rewrite.replace(visited, ASTNodes.createMoveTarget(rewrite, method), group);
    } else {
        rewrite.replace(visited, ast.not(ASTNodes.createMoveTarget(rewrite, method)), group);
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 30 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class DoWhileRatherThanWhileCleanUp method visit.

@Override
public boolean visit(final WhileStatement visited) {
    if (ASTNodes.isPassiveWithoutFallingThrough(visited.getExpression()) && Boolean.TRUE.equals(peremptoryValue(visited, visited.getExpression()))) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        ASTNodeFactory ast = cuRewrite.getASTBuilder();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.DoWhileRatherThanWhileCleanUp_description);
        ASTNodes.replaceButKeepComment(rewrite, visited, ast.newDoStatement(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getExpression())), ASTNodes.createMoveTarget(rewrite, visited.getBody())), group);
        return false;
    }
    return true;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)195 TextEditGroup (org.eclipse.text.edits.TextEditGroup)167 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)133 Expression (org.eclipse.jdt.core.dom.Expression)56 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)48 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)42 Statement (org.eclipse.jdt.core.dom.Statement)24 ArrayList (java.util.ArrayList)22 IfStatement (org.eclipse.jdt.core.dom.IfStatement)20 Block (org.eclipse.jdt.core.dom.Block)15 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)11 Modifier (org.eclipse.jdt.core.dom.Modifier)11 Type (org.eclipse.jdt.core.dom.Type)11 List (java.util.List)10 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)7 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)7