Search in sources :

Example 21 with ASTRewrite

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

the class StringBuilderCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    if (isStringConcat(visited)) {
        List<Pair<ITypeBinding, Expression>> allOperands = new LinkedList<>();
        readSubExpressions(visited, allOperands, new AtomicBoolean(false));
        boolean replaceNeeded = filterOutEmptyStringsFromStringConcat(allOperands);
        if (replaceNeeded) {
            TextEditGroup group = new TextEditGroup(MultiFixMessages.StringBuilderCleanUp_description);
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            rewrite.replace(visited, createStringConcats(allOperands), group);
            return false;
        }
    }
    return true;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) LinkedList(java.util.LinkedList) Pair(org.autorefactor.util.Pair)

Example 22 with ASTRewrite

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

the class RemoveEmptyStatementCleanUp method visit.

@Override
public boolean visit(final EmptyStatement visited) {
    if (isEmptyCode(visited)) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.RemoveEmptyStatementCleanUp_description);
        if (ASTNodes.canHaveSiblings(visited) || visited.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
            rewrite.remove(visited, group);
            return false;
        }
        if (visited instanceof EmptyStatement) {
            ASTNodeFactory ast = cuRewrite.getASTBuilder();
            ASTNodes.replaceButKeepComment(rewrite, visited, ast.newBlock(), group);
            return false;
        }
    }
    return true;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 23 with ASTRewrite

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

the class SeparateAssertionsRatherThanBooleanExpressionCleanUp method maybeRefactorMethod.

private boolean maybeRefactorMethod(final ExpressionStatement visited, final MethodInvocation originalMethod, final InfixExpression.Operator operator, final int parameterIndex) {
    InfixExpression booleanExpression = ASTNodes.as((Expression) originalMethod.arguments().get(parameterIndex), InfixExpression.class);
    if (booleanExpression != null && ASTNodes.hasOperator(booleanExpression, operator)) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        ASTNodeFactory ast = cuRewrite.getASTBuilder();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.SeparateAssertionsRatherThanBooleanExpressionCleanUp_description);
        List<Expression> allOperands = ASTNodes.allOperands(booleanExpression);
        rewrite.replace(booleanExpression, ASTNodes.createMoveTarget(rewrite, allOperands.remove(0)), group);
        List<Statement> expressionStatements = new ArrayList<>(allOperands.size());
        for (Expression operand : allOperands) {
            List<Expression> newArguments = new ArrayList<>(originalMethod.arguments().size());
            for (Object argument : originalMethod.arguments()) {
                newArguments.add(rewrite.createCopyTarget(ASTNodes.getUnparenthesedExpression((Expression) argument)));
            }
            newArguments.set(parameterIndex, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(operand)));
            MethodInvocation newMethod;
            if (originalMethod.getExpression() != null) {
                MethodInvocation newMethodInvocation = ast.newMethodInvocation();
                newMethodInvocation.setExpression(rewrite.createCopyTarget(originalMethod.getExpression()));
                newMethodInvocation.setName(ast.newSimpleName(originalMethod.getName().getIdentifier()));
                newMethodInvocation.arguments().addAll(newArguments);
                newMethod = newMethodInvocation;
            } else {
                MethodInvocation newMethodInvocation = ast.newMethodInvocation();
                newMethodInvocation.setExpression(null);
                newMethodInvocation.setName(ast.newSimpleName(originalMethod.getName().getIdentifier()));
                newMethodInvocation.arguments().addAll(newArguments);
                newMethod = newMethodInvocation;
            }
            ExpressionStatement newStatement = ast.newExpressionStatement(newMethod);
            expressionStatements.add(newStatement);
        }
        if (ASTNodes.canHaveSiblings(visited)) {
            Collections.reverse(expressionStatements);
            for (Statement expressionStatement : expressionStatements) {
                rewrite.insertAfter(expressionStatement, visited, group);
            }
        } else {
            expressionStatements.add(0, ASTNodes.createMoveTarget(rewrite, visited));
            Block newBlock = ast.newBlock();
            newBlock.statements().addAll(expressionStatements);
            ASTNodes.replaceButKeepComment(rewrite, visited, newBlock, group);
        }
        return false;
    }
    return true;
}
Also used : ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 24 with ASTRewrite

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

the class StringCleanUp method replaceStringValueOfByArg0.

private void replaceStringValueOfByArg0(final Expression toReplace, final MethodInvocation methodInvocation, final ITypeBinding expectedType) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.StringCleanUp_description);
    ITypeBinding actualType = ((Expression) methodInvocation.arguments().get(0)).resolveTypeBinding();
    if (ASTNodes.hasType(actualType, String.class.getCanonicalName()) || expectedType.equals(actualType) || Bindings.getBoxedTypeBinding(expectedType, methodInvocation.getAST()).equals(actualType)) {
        rewrite.replace(toReplace, ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, (Expression) methodInvocation.arguments().get(0))), group);
    } else {
        rewrite.replace(toReplace, ast.newCastExpression(ast.type(expectedType.getQualifiedName()), ASTNodes.createMoveTarget(rewrite, (Expression) methodInvocation.arguments().get(0))), group);
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 25 with ASTRewrite

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

the class StringCleanUp method removeToString.

private void removeToString(final MethodInvocation visited) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.StringCleanUp_description);
    if (visited.getExpression() != null) {
        rewrite.replace(visited, ASTNodes.createMoveTarget(rewrite, visited.getExpression()), group);
    } else {
        rewrite.replace(visited, ast.newThisExpression(), 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