Search in sources :

Example 16 with ASTRewrite

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

the class AbstractUnitTestCleanUp method maybeRefactorComparison.

private boolean maybeRefactorComparison(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final ASTNode nodeToReplace, final MethodInvocation originalMethod, final InfixExpression infixExpression, final boolean isAssertEquals, final Expression failureMessage) {
    Pair<Expression, Expression> actualAndExpected = getActualAndExpected(infixExpression.getLeftOperand(), infixExpression.getRightOperand());
    if (isComparingObjects(infixExpression) && !ASTNodes.is(infixExpression.getLeftOperand(), NullLiteral.class) && !ASTNodes.is(infixExpression.getRightOperand(), NullLiteral.class)) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        // $NON-NLS-1$
        TextEditGroup group = new TextEditGroup("");
        MethodInvocation newAssert = invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, // $NON-NLS-1$
        getAssertName(isAssertEquals, "Same"), // $NON-NLS-1$
        ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(actualAndExpected.getFirst())), // $NON-NLS-1$
        ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(actualAndExpected.getSecond())), null, failureMessage);
        ASTNodes.replaceButKeepComment(rewrite, nodeToReplace, invokeMethodOrStatement(nodeToReplace, newAssert), group);
        return false;
    }
    return maybeRefactorToEquality(classesToUseWithImport, importsToAdd, nodeToReplace, originalMethod, isAssertEquals, actualAndExpected.getFirst(), actualAndExpected.getSecond(), failureMessage, true);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 17 with ASTRewrite

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

the class SingleDeclarationsRatherThanMultiDeclarationCleanUp method refactorMultiDeclaration.

private void refactorMultiDeclaration(final ASTNode visited, final List<IExtendedModifier> modifiers, final Type type, final List<VariableDeclarationFragment> fragments, final Javadoc docComment) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.SingleDeclarationsRatherThanMultiDeclarationCleanUp_description);
    for (int i = fragments.size() - 1; 0 <= i; i--) {
        VariableDeclarationFragment fragment = fragments.get(i);
        SimpleName copyOfFragment = ASTNodes.createMoveTarget(rewrite, fragment.getName());
        Type copyOfType = ast.createCopyTarget(type);
        Expression copyOfInitializer;
        if (fragment.getInitializer() != null) {
            copyOfInitializer = ASTNodes.createMoveTarget(rewrite, fragment.getInitializer());
        } else {
            copyOfInitializer = null;
        }
        VariableDeclarationFragment newFragment = ast.newVariableDeclarationFragment(copyOfFragment, copyOfInitializer);
        newFragment.extraDimensions().addAll(ASTNodes.createMoveTarget(rewrite, fragment.extraDimensions()));
        ASTNode newNode;
        if (visited instanceof VariableDeclarationStatement) {
            VariableDeclarationStatement newStatement = ast.newVariableDeclarationStatement(copyOfType, newFragment);
            updateModifiers(modifiers, newStatement.modifiers());
            newNode = newStatement;
        } else {
            FieldDeclaration newField = ast.newFieldDeclaration(copyOfType, newFragment);
            if (docComment != null) {
                newField.setJavadoc(ast.createCopyTarget(docComment));
            }
            updateModifiers(modifiers, newField.modifiers());
            newNode = newField;
        }
        if (i > 0) {
            rewrite.insertAfter(newNode, visited, group);
        } else {
            ASTNodes.replaceButKeepComment(rewrite, visited, newNode, group);
        }
    }
}
Also used : Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) TextEditGroup(org.eclipse.text.edits.TextEditGroup) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 18 with ASTRewrite

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

the class StandardMethodRatherThanLibraryMethodCleanUp method copyArguments.

private List<Expression> copyArguments(final MethodInvocation visited) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    List<Expression> copyOfArgs = new ArrayList<>(visited.arguments().size());
    for (Object expression : visited.arguments()) {
        copyOfArgs.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression((Expression) expression)));
    }
    return copyOfArgs;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ArrayList(java.util.ArrayList) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite)

Example 19 with ASTRewrite

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

the class StringBuilderCleanUp method replaceWithString.

private void replaceWithString(final Type type, final Expression initializer, final List<Pair<ITypeBinding, Expression>> allAppendedStrings, final List<Statement> statementsToRemove, final List<MethodInvocation> toStringToRefactor) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.StringBuilderCleanUp_description);
    rewrite.replace(initializer, createStringConcats(allAppendedStrings), group);
    ASTNodes.replaceButKeepComment(rewrite, type, ast.type(String.class.getSimpleName()), group);
    for (Statement statementToRemove : statementsToRemove) {
        ASTNodes.removeButKeepComment(rewrite, statementToRemove, group);
    }
    for (MethodInvocation readToRefactor : toStringToRefactor) {
        rewrite.replace(readToRefactor, ASTNodes.createMoveTarget(rewrite, readToRefactor.getExpression()), group);
    }
}
Also used : ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) 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 20 with ASTRewrite

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

the class StringBuilderCleanUp method maybeReplaceWithNewStringAppends.

/**
 * Rewrite the successive calls to append()
 *
 * @param visited                     The node to replace.
 * @param allAppendedStrings          All appended strings.
 * @param lastExpression              The expression on which the methods are
 *                                    called.
 * @param isInstanceCreationToRewrite
 * @return
 */
private boolean maybeReplaceWithNewStringAppends(final Expression visited, final List<Pair<ITypeBinding, Expression>> allAppendedStrings, final Expression lastExpression, final boolean isInstanceCreationToRewrite) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.StringBuilderCleanUp_description);
    Expression result = null;
    List<Expression> tempStringLiterals = new ArrayList<>();
    List<Expression> finalStrings = new ArrayList<>();
    AtomicBoolean isFirst = new AtomicBoolean(true);
    for (Pair<ITypeBinding, Expression> appendedString : allAppendedStrings) {
        if (appendedString == null) {
            return true;
        }
        if (isValuedStringLiteralOrConstant(appendedString.getSecond())) {
            tempStringLiterals.add(ASTNodes.createMoveTarget(rewrite, appendedString.getSecond()));
        } else {
            result = handleTempStringLiterals(lastExpression, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
            if (isFirst.get()) {
                isFirst.lazySet(false);
                if (!isInstanceCreationToRewrite) {
                    result = ASTNodes.createMoveTarget(rewrite, lastExpression);
                    finalStrings.add(getTypedExpression(appendedString));
                } else if (appendedString.getFirst() != null ? ASTNodes.hasType(appendedString.getFirst(), String.class.getCanonicalName()) : ASTNodes.hasType(appendedString.getSecond(), String.class.getCanonicalName())) {
                    result = ast.newClassInstanceCreation(ASTNodes.createMoveTarget(rewrite, ((ClassInstanceCreation) lastExpression).getType()), getTypedExpression(appendedString));
                } else {
                    result = ast.newClassInstanceCreation(ASTNodes.createMoveTarget(rewrite, ((ClassInstanceCreation) lastExpression).getType()));
                    finalStrings.add(getTypedExpression(appendedString));
                }
            } else {
                finalStrings.add(getTypedExpression(appendedString));
            }
        }
    }
    result = handleTempStringLiterals(lastExpression, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
    for (Expression finalString : finalStrings) {
        if (result == null) {
            result = finalString;
        } else {
            MethodInvocation appendMethod = ast.newMethodInvocation();
            appendMethod.setExpression(result);
            // $NON-NLS-1$
            appendMethod.setName(ast.newSimpleName("append"));
            appendMethod.arguments().add(finalString);
            result = appendMethod;
        }
    }
    rewrite.replace(visited, result, group);
    return false;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) 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