Search in sources :

Example 86 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteAddAllRatherThanLoopCleanUp method replaceWithCollectionMethod.

private void replaceWithCollectionMethod(final ASTNode toReplace, final String methodName, final Expression affectedCollection, final Expression data) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteAddAllRatherThanLoopCleanUp_description);
    MethodInvocation newMethod = ast.newMethodInvocation();
    newMethod.setName(ast.newSimpleName(methodName));
    newMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(data)));
    if (affectedCollection != null) {
        newMethod.setExpression(ASTNodes.createMoveTarget(rewrite, affectedCollection));
    }
    ASTNodes.replaceButKeepComment(rewrite, toReplace, ast.newExpressionStatement(newMethod), 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 87 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteAndConditionRatherThanEmbededIfCleanUp method replaceIfNoElseStatement.

private void replaceIfNoElseStatement(final IfStatement node, final IfStatement innerIf) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteAndConditionRatherThanEmbededIfCleanUp_description);
    if (node.getThenStatement() instanceof Block || innerIf.getThenStatement() instanceof Block) {
        InfixExpression newInfixExpression = ast.newInfixExpression();
        newInfixExpression.setLeftOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, node.getExpression())));
        newInfixExpression.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
        newInfixExpression.setRightOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, innerIf.getExpression())));
        ASTNodes.replaceButKeepComment(rewrite, innerIf.getExpression(), newInfixExpression, group);
        ASTNodes.replaceButKeepComment(rewrite, node, ASTNodes.createMoveTarget(rewrite, innerIf), group);
    } else {
        // Do not do the cleanup
        // Prepare the code for the next pass
        Block newBlock = ast.newBlock();
        newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, innerIf));
        ASTNodes.replaceButKeepComment(rewrite, innerIf, newBlock, 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) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 88 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteBreakRatherThanPassiveIterationsCleanUp method addBreak.

private void addBreak(final IfStatement ifStatement, final List<Statement> assignments) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteBreakRatherThanPassiveIterationsCleanUp_description);
    if (ifStatement.getThenStatement() instanceof Block) {
        rewrite.insertAfter(ast.newBreakStatement(), assignments.get(assignments.size() - 1), group);
    } else {
        Block newBlock = ast.newBlock();
        newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, ifStatement.getThenStatement()));
        newBlock.statements().add(ast.newBreakStatement());
        ASTNodes.replaceButKeepComment(rewrite, ifStatement.getThenStatement(), newBlock, group);
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 89 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoletePushNegationDownCleanUp method getOppositeExpression.

private Expression getOppositeExpression(final Expression negativeExpression) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    Expression operand = ASTNodes.getUnparenthesedExpression(negativeExpression);
    if (operand instanceof PrefixExpression) {
        PrefixExpression pe = (PrefixExpression) operand;
        if (ASTNodes.hasOperator(pe, PrefixExpression.Operator.NOT)) {
            return ASTNodes.createMoveTarget(rewrite, pe.getOperand());
        }
    } else if (operand instanceof InfixExpression) {
        InfixExpression infixExpression = (InfixExpression) operand;
        InfixExpression.Operator reverseOp = (InfixExpression.Operator) OperatorEnum.getOperator(infixExpression).getReverseBooleanOperator();
        if (reverseOp != null) {
            List<Expression> allOperands = new ArrayList<>(ASTNodes.allOperands(infixExpression));
            if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.AND, InfixExpression.Operator.OR)) {
                for (ListIterator<Expression> it = allOperands.listIterator(); it.hasNext(); ) {
                    Expression anOperand = it.next();
                    Expression oppositeOperand = getOppositeExpression(anOperand);
                    it.set(oppositeOperand != null ? oppositeOperand : ast.negate(anOperand, true));
                }
            } else {
                allOperands = ASTNodes.createMoveTarget(rewrite, allOperands);
            }
            return ast.newParenthesizedExpression(ast.newInfixExpression(reverseOp, allOperands));
        }
    } else {
        Boolean constant = ASTNodes.getBooleanLiteral(operand);
        if (constant != null) {
            return ast.newBooleanLiteral(!constant);
        }
    }
    return null;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) List(java.util.List) ArrayList(java.util.ArrayList) ListIterator(java.util.ListIterator)

Example 90 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class ObsoleteRedundantComparatorCleanUp method removeComparator.

private void removeComparator(final MethodInvocation visitedIfRefactoringNeeded, final Expression list, final Expression comparator, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantComparatorCleanUp_description);
    if (visitedIfRefactoringNeeded != null) {
        String collectionsClassname = addImport(Collections.class, classesToUseWithImport, importsToAdd);
        MethodInvocation sortMethod = ast.newMethodInvocation();
        sortMethod.setExpression(ASTNodeFactory.newName(ast, collectionsClassname));
        // $NON-NLS-1$
        sortMethod.setName(ast.newSimpleName("sort"));
        sortMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(list)));
        ASTNodes.replaceButKeepComment(rewrite, visitedIfRefactoringNeeded, sortMethod, group);
    } else {
        ASTNodes.removeButKeepComment(rewrite, comparator, 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)

Aggregations

ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)152 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)133 TextEditGroup (org.eclipse.text.edits.TextEditGroup)115 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 Expression (org.eclipse.jdt.core.dom.Expression)44 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)37 ArrayList (java.util.ArrayList)19 Block (org.eclipse.jdt.core.dom.Block)17 Statement (org.eclipse.jdt.core.dom.Statement)17 IfStatement (org.eclipse.jdt.core.dom.IfStatement)16 Type (org.eclipse.jdt.core.dom.Type)14 TypeNameDecider (org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)10 List (java.util.List)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)8 Name (org.eclipse.jdt.core.dom.Name)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)6