Search in sources :

Example 76 with ASTRewrite

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

the class ObsoletePrimitiveComparisonRatherThanWrapperComparisonCleanUp method refactor.

private void refactor(final MethodInvocation visited, final Expression primitiveValue, final Class<?> wrapperClass) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoletePrimitiveComparisonRatherThanWrapperComparisonCleanUp_description);
    MethodInvocation compareMethod = ast.newMethodInvocation();
    compareMethod.setExpression(ast.newSimpleName(wrapperClass.getSimpleName()));
    // $NON-NLS-1$
    compareMethod.setName(ast.newSimpleName("compare"));
    compareMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(primitiveValue)));
    compareMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression((Expression) visited.arguments().get(0))));
    ASTNodes.replaceButKeepComment(rewrite, visited, compareMethod, group);
}
Also used : 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 77 with ASTRewrite

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

the class ObsoleteReduceIndentationCleanUp method moveElseStatement.

private void moveElseStatement(final IfStatement visited) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteReduceIndentationCleanUp_description);
    List<Statement> statementsToMove = ASTNodes.asList(visited.getElseStatement());
    if (ASTNodes.canHaveSiblings(visited)) {
        for (int i = statementsToMove.size() - 1; i >= 0; i--) {
            rewrite.insertAfter(ASTNodes.createMoveTarget(rewrite, statementsToMove.get(i)), visited, group);
        }
        rewrite.remove(visited.getElseStatement(), group);
    } else {
        List<Statement> copyOfStatements = new ArrayList<>(statementsToMove.size() + 1);
        for (Statement statement : statementsToMove) {
            copyOfStatements.add(ASTNodes.createMoveTarget(rewrite, statement));
        }
        rewrite.remove(visited.getElseStatement(), group);
        copyOfStatements.add(0, ASTNodes.createMoveTarget(rewrite, visited));
        Block newBlock = ast.newBlock();
        newBlock.statements().addAll(copyOfStatements);
        ASTNodes.replaceButKeepComment(rewrite, visited, newBlock, group);
    }
}
Also used : ForStatement(org.eclipse.jdt.core.dom.ForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ArrayList(java.util.ArrayList) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 78 with ASTRewrite

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

the class ObsoleteRedundantModifiersCleanUp method removeFinalModifier.

private boolean removeFinalModifier(final List<IExtendedModifier> modifiers) {
    boolean result = true;
    for (Modifier modifier : getModifiersOnly(modifiers)) {
        if (modifier.isFinal()) {
            TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantModifiersCleanUp_description);
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            rewrite.remove(modifier, group);
            result = false;
        }
    }
    return result;
}
Also used : ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 79 with ASTRewrite

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

the class ObsoleteRedundantModifiersCleanUp method removeProtectedModifier.

private boolean removeProtectedModifier(final BodyDeclaration visited) {
    List<IExtendedModifier> modifiers = visited.modifiers();
    for (Modifier modifier : getModifiersOnly(modifiers)) {
        if (modifier.isProtected()) {
            TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantModifiersCleanUp_description);
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            rewrite.remove(modifier, group);
            return false;
        }
    }
    return true;
}
Also used : ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) TextEditGroup(org.eclipse.text.edits.TextEditGroup) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 80 with ASTRewrite

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

the class ObsoleteRemoveEmptySuperConstrInvocationCleanUp method visit.

@Override
public boolean visit(final SuperConstructorInvocation node) {
    if (node.arguments().isEmpty()) {
        TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRemoveEmptySuperConstrInvocationCleanUp_description);
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        ASTNodes.removeButKeepComment(rewrite, node, group);
        return false;
    }
    return true;
}
Also used : 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