Search in sources :

Example 61 with ASTRewrite

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

the class ObsoleteComparisonCleanUp method refactorComparingToZero.

private void refactorComparingToZero(final InfixExpression visited, final MethodInvocation comparisonMethod, final InfixExpression.Operator operator) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteComparisonCleanUp_description);
    InfixExpression newInfixExpression = ast.newInfixExpression();
    newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, comparisonMethod));
    newInfixExpression.setOperator(operator);
    // $NON-NLS-1$
    newInfixExpression.setRightOperand(ast.newNumberLiteral("0"));
    ASTNodes.replaceButKeepComment(rewrite, visited, newInfixExpression, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 62 with ASTRewrite

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

the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method refactor.

private void refactor(final Expression visited, final ITypeBinding typeArgument, final Set<String> classesToUseWithImport, final SimpleName name1, final AtomicReference<Expression> criteria, final AtomicBoolean isForward, final Boolean isNullFirst) {
    String comparatorClassName = addImport(Comparator.class, classesToUseWithImport, new HashSet<>());
    Expression lambda;
    if (criteria.get() instanceof MethodInvocation) {
        lambda = buildMethod(typeArgument, (MethodInvocation) criteria.get());
    } else {
        lambda = buildField(visited, typeArgument, isForward.get(), isNullFirst, (QualifiedName) criteria.get(), name1);
    }
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaExpressionRatherThanComparatorCleanUp_description);
    MethodInvocation comparingMethod = ast.newMethodInvocation();
    comparingMethod.setExpression(ASTNodeFactory.newName(ast, comparatorClassName));
    // $NON-NLS-1$
    comparingMethod.setName(ast.newSimpleName("comparing"));
    comparingMethod.arguments().add(lambda);
    if (!isForward.get()) {
        MethodInvocation reversedMethod = ast.newMethodInvocation();
        reversedMethod.setExpression(comparingMethod);
        // $NON-NLS-1$
        reversedMethod.setName(ast.newSimpleName("reversed"));
        comparingMethod = reversedMethod;
    }
    if (isNullFirst != null) {
        if (isNullFirst) {
            MethodInvocation nullsFirstMethod = ast.newMethodInvocation();
            nullsFirstMethod.setExpression(ASTNodeFactory.newName(ast, comparatorClassName));
            // $NON-NLS-1$
            nullsFirstMethod.setName(ast.newSimpleName("nullsFirst"));
            nullsFirstMethod.arguments().add(comparingMethod);
            comparingMethod = nullsFirstMethod;
        } else {
            MethodInvocation nullsLastMethod = ast.newMethodInvocation();
            nullsLastMethod.setExpression(ASTNodeFactory.newName(ast, comparatorClassName));
            // $NON-NLS-1$
            nullsLastMethod.setName(ast.newSimpleName("nullsLast"));
            nullsLastMethod.arguments().add(comparingMethod);
            comparingMethod = nullsLastMethod;
        }
    }
    ASTNodes.replaceButKeepComment(rewrite, visited, comparingMethod, group);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) 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 63 with ASTRewrite

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

the class ObsoleteMergeConditionalBlocksCleanUp method addOneMoreIf.

private boolean addOneMoreIf(final List<IfStatement> duplicateIfBlocks, final List<Boolean> isThenStatement, final AtomicInteger operandCount) {
    IfStatement lastBlock = duplicateIfBlocks.get(duplicateIfBlocks.size() - 1);
    Statement previousStatement = isThenStatement.get(isThenStatement.size() - 1) ? lastBlock.getThenStatement() : lastBlock.getElseStatement();
    Statement nextStatement = isThenStatement.get(isThenStatement.size() - 1) ? lastBlock.getElseStatement() : lastBlock.getThenStatement();
    if (nextStatement != null) {
        IfStatement nextElse = ASTNodes.as(nextStatement, IfStatement.class);
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        if (nextElse != null && !rewrite.hasBeenRefactored(nextElse) && operandCount.get() + ASTNodes.getNbOperands(nextElse.getExpression()) < ASTNodes.EXCESSIVE_OPERAND_NUMBER) {
            if (ASTNodes.match(previousStatement, nextElse.getThenStatement())) {
                operandCount.addAndGet(ASTNodes.getNbOperands(nextElse.getExpression()));
                duplicateIfBlocks.add(nextElse);
                isThenStatement.add(Boolean.TRUE);
                return true;
            }
            if (nextElse.getElseStatement() != null && ASTNodes.match(previousStatement, nextElse.getElseStatement())) {
                operandCount.addAndGet(ASTNodes.getNbOperands(nextElse.getExpression()));
                duplicateIfBlocks.add(nextElse);
                isThenStatement.add(Boolean.FALSE);
                return true;
            }
        }
    }
    return false;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite)

Example 64 with ASTRewrite

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

the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method replaceEquals.

private void replaceEquals(final IfStatement node, final Expression firstField, final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final Expression secondField, final ReturnStatement returnStatement) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp_description);
    String classnameText = addImport(Objects.class, classesToUseWithImport, importsToAdd);
    ReturnStatement copyOfReturnStatement = ASTNodes.createMoveTarget(rewrite, returnStatement);
    MethodInvocation newMethodInvocation = ast.newMethodInvocation();
    newMethodInvocation.setExpression(ASTNodeFactory.newName(ast, classnameText));
    newMethodInvocation.setName(ast.newSimpleName(EQUALS_METHOD));
    newMethodInvocation.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(firstField)));
    newMethodInvocation.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(secondField)));
    ASTNodes.replaceButKeepComment(rewrite, node.getExpression(), ast.not(newMethodInvocation), group);
    if (node.getThenStatement() instanceof Block) {
        ASTNodes.replaceButKeepComment(rewrite, (ASTNode) ((Block) node.getThenStatement()).statements().get(0), copyOfReturnStatement, group);
    } else {
        ASTNodes.replaceButKeepComment(rewrite, node.getThenStatement(), copyOfReturnStatement, group);
    }
    if (node.getElseStatement() != null) {
        rewrite.remove(node.getElseStatement(), group);
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 65 with ASTRewrite

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

the class RemoveUncheckedThrowsClausesCleanUp method visit.

@Override
public boolean visit(final MethodDeclaration visited) {
    Collection<ASTNode> nodesToRemove = getUncheckedExceptions(visited);
    if (!nodesToRemove.isEmpty()) {
        for (ASTNode node : nodesToRemove) {
            TextEditGroup group = new TextEditGroup(MultiFixMessages.RemoveUncheckedThrowsClausesCleanUp_description);
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            ASTNodes.replaceButKeepComment(rewrite, node, null, group);
        }
        return false;
    }
    return true;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) 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