Search in sources :

Example 36 with ASTRewrite

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

the class MatchingStreamRatherThanCountCleanUp method replaceMethod.

private void replaceMethod(final InfixExpression visited, final MethodInvocation filterMethod, final boolean hasElement) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.MatchingStreamRatherThanCountCleanUp_description);
    rewrite.replace(visited, filterMethod, group);
    rewrite.replace(filterMethod.getName(), ast.newSimpleName(hasElement ? ANYMATCH_METHOD : NONEMATCH_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 37 with ASTRewrite

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

the class NamedMethodRatherThanLogLevelParameterCleanUp method replaceLevelByMethodName.

private void replaceLevelByMethodName(final MethodInvocation visited, final String methodName) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.NamedMethodRatherThanLogLevelParameterCleanUp_description);
    rewrite.replace(visited.getName(), ast.newSimpleName(methodName), group);
    rewrite.remove((Expression) visited.arguments().get(0), 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 38 with ASTRewrite

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

the class NoLoopIterationRatherThanEmptyCheckCleanUp method removeCondition.

private void removeCondition(final InfixExpression condition, final List<Expression> operands) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.NoLoopIterationRatherThanEmptyCheckCleanUp_description);
    if (operands.size() == 2) {
        rewrite.replace(condition, ASTNodes.createMoveTarget(rewrite, operands.get(0)), group);
    } else {
        operands.remove(operands.size() - 1);
        InfixExpression newCondition = ast.newInfixExpression(condition.getOperator(), ASTNodes.createMoveTarget(rewrite, operands));
        rewrite.replace(condition, newCondition, group);
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 39 with ASTRewrite

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

the class NoLoopIterationRatherThanEmptyCheckCleanUp method visit.

@Override
public boolean visit(final IfStatement visited) {
    if (visited.getElseStatement() == null) {
        List<Statement> statements = ASTNodes.asList(visited.getThenStatement());
        if (statements != null && statements.size() == 1) {
            Expression container = getContainer(statements);
            if (ASTNodes.isArray(container) && ASTNodes.isPassive(container)) {
                InfixExpression condition = ASTNodes.as(visited.getExpression(), InfixExpression.class);
                if (isConditionValid(condition, container)) {
                    ASTRewrite rewrite = cuRewrite.getASTRewrite();
                    TextEditGroup group = new TextEditGroup(MultiFixMessages.NoLoopIterationRatherThanEmptyCheckCleanUp_description);
                    ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, statements.get(0)), group);
                    return false;
                }
                if (ASTNodes.hasOperator(condition, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND)) {
                    List<Expression> operands = ASTNodes.allOperands(condition);
                    Expression operand = ASTNodes.as(operands.get(operands.size() - 1), InfixExpression.class);
                    if (isConditionValid(operand, container)) {
                        removeCondition(condition, operands);
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ForStatement(org.eclipse.jdt.core.dom.ForStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 40 with ASTRewrite

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

the class ObsoleteIfRatherThanTwoSwitchCasesCleanUp method replaceBySwitch.

private void replaceBySwitch(final SwitchStatement visited, final List<Pair<List<Expression>, List<Statement>>> switchStructure, final int caseIndexWithDefault, final List<BreakStatement> overBreaks) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteIfRatherThanTwoSwitchCasesCleanUp_description);
    List<Block> newBlocks = prepareNewBlocks(rewrite, ast, switchStructure);
    for (BreakStatement breakStatement : overBreaks) {
        rewrite.remove(breakStatement, group);
    }
    int localCaseIndexWithDefault = caseIndexWithDefault;
    Expression discriminant = visited.getExpression();
    Statement currentBlock = null;
    for (int i = switchStructure.size() - 1; i >= 0; i--) {
        Pair<List<Expression>, List<Statement>> caseStructure = switchStructure.get(i);
        Expression newCondition = buildNewCondition(rewrite, ast, discriminant, caseStructure);
        if (currentBlock != null) {
            IfStatement newIfStatement = ast.newIfStatement();
            newIfStatement.setExpression(newCondition);
            newIfStatement.setThenStatement(newBlocks.get(i));
            newIfStatement.setElseStatement(currentBlock);
            currentBlock = newIfStatement;
        } else if (caseStructure.getSecond().isEmpty()) {
            localCaseIndexWithDefault = -1;
        } else if (localCaseIndexWithDefault == -1) {
            IfStatement newIfStatement = ast.newIfStatement();
            newIfStatement.setExpression(newCondition);
            newIfStatement.setThenStatement(newBlocks.get(i));
            currentBlock = newIfStatement;
        } else {
            currentBlock = newBlocks.get(i);
        }
    }
    ASTNodes.replaceButKeepComment(rewrite, visited, currentBlock, group);
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) ArrayList(java.util.ArrayList) List(java.util.List) 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