Search in sources :

Example 51 with ASTRewrite

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

the class ObsoleteAddAllRatherThanLoopCleanUp method replaceWithCollectionsAddAll.

private void replaceWithCollectionsAddAll(final Statement node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final Expression iterable, final MethodInvocation addMethod) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteAddAllRatherThanLoopCleanUp_description);
    String classname = addImport(Collections.class, classesToUseWithImport, importsToAdd);
    MethodInvocation addAllMethod = ast.newMethodInvocation();
    addAllMethod.setExpression(ASTNodeFactory.newName(ast, classname));
    // $NON-NLS-1$
    addAllMethod.setName(ast.newSimpleName("addAll"));
    addAllMethod.arguments().add(addMethod.getExpression() != null ? ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(addMethod.getExpression())) : ast.newThisExpression());
    addAllMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(iterable)));
    ASTNodes.replaceButKeepComment(rewrite, node, ast.newExpressionStatement(addAllMethod), 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 52 with ASTRewrite

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

the class ObsoleteCharacterParameterRatherThanStringCleanUp method refactorWithCharacter.

private void refactorWithCharacter(final StringLiteral stringLiteral, final String value) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteCharacterParameterRatherThanStringCleanUp_description);
    CharacterLiteral replacement = ast.newCharacterLiteral();
    replacement.setCharValue(value.charAt(0));
    rewrite.replace(stringLiteral, replacement, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 53 with ASTRewrite

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

the class ObsoleteCommonCodeInIfElseStatementCleanUp method insertIdenticalCode.

private void insertIdenticalCode(final IfStatement node, final List<Statement> stmtsToRemove) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteCommonCodeInIfElseStatementCleanUp_description);
    for (Statement stmtToRemove : stmtsToRemove) {
        rewrite.insertAfter(ASTNodes.createMoveTarget(rewrite, stmtToRemove), node, group);
    }
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 54 with ASTRewrite

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

the class EqualsIgnoreCaseRatherThanCaseShiftCleanUp method refactorEqualsIgnoreCase.

private void refactorEqualsIgnoreCase(final MethodInvocation visited, final Expression leftExpressionIfChanged, final Expression rightExpressionIfChanged) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.EqualsIgnoreCaseRatherThanCaseShiftCleanUp_description);
    if (leftExpressionIfChanged != null) {
        rewrite.replace(visited.getExpression(), ASTNodes.createMoveTarget(rewrite, leftExpressionIfChanged), group);
    }
    if (rightExpressionIfChanged != null) {
        rewrite.replace((Expression) visited.arguments().get(0), ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(rightExpressionIfChanged)), group);
    }
}
Also used : ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 55 with ASTRewrite

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

the class EqualsIgnoreCaseRatherThanCaseShiftCleanUp method refactorEquals.

private void refactorEquals(final MethodInvocation visited, final MethodInvocation leftInvocation, final MethodInvocation rightInvocation) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.EqualsIgnoreCaseRatherThanCaseShiftCleanUp_description);
    rewrite.replace(leftInvocation, ASTNodes.createMoveTarget(rewrite, leftInvocation.getExpression()), group);
    // $NON-NLS-1$
    rewrite.replace(visited.getName(), ast.newSimpleName("equalsIgnoreCase"), group);
    rewrite.replace(rightInvocation, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(rightInvocation.getExpression())), group);
}
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