Search in sources :

Example 56 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory 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 57 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory 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 58 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory 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 59 with ASTNodeFactory

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

the class ObsoleteXORRatherThanDuplicateConditionsCleanUp method replaceDuplicateExpression.

private void replaceDuplicateExpression(final InfixExpression node, final Expression firstExpression, final Expression secondExpression, final AtomicBoolean isFirstExprPositive, final AtomicBoolean isSecondExprPositive) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteXORRatherThanDuplicateConditionsCleanUp_description);
    InfixExpression newInfixExpression = ast.newInfixExpression();
    newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, firstExpression));
    newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, secondExpression));
    if (isFirstExprPositive.get() == isSecondExprPositive.get()) {
        newInfixExpression.setOperator(InfixExpression.Operator.EQUALS);
    } else {
        newInfixExpression.setOperator(InfixExpression.Operator.XOR);
    }
    ASTNodes.replaceButKeepComment(rewrite, node, newInfixExpression, 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 60 with ASTNodeFactory

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

the class OptimizeRegExCleanUp method rewriteRegEx.

private void rewriteRegEx(final StringLiteral visited, final String pattern) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.OptimizeRegExCleanUp_description);
    rewrite.replace(visited, ast.newStringLiteral(pattern), 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

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