Search in sources :

Example 81 with ASTNodeFactory

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

the class ObsoleteIfRatherThanWhileAndFallsThroughCleanUp method replaceByIf.

private void replaceByIf(final WhileStatement visited, final BreakVisitor breakVisitor) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteIfRatherThanWhileAndFallsThroughCleanUp_description);
    for (BreakStatement breakStatement : breakVisitor.getBreaks()) {
        if (ASTNodes.canHaveSiblings(breakStatement) || breakStatement.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
            rewrite.remove(breakStatement, group);
        } else {
            ASTNodes.replaceButKeepComment(rewrite, breakStatement, ast.newBlock(), group);
        }
    }
    IfStatement newIfStatement = ast.newIfStatement();
    newIfStatement.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getExpression())));
    newIfStatement.setThenStatement(ASTNodes.createMoveTarget(rewrite, visited.getBody()));
    ASTNodes.replaceButKeepComment(rewrite, visited, newIfStatement, group);
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 82 with ASTNodeFactory

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

the class ObsoleteInstanceofRatherThanIsInstanceCleanUp method replace.

private void replace(final MethodInvocation node, final TypeLiteral klass) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteInstanceofRatherThanIsInstanceCleanUp_description);
    InstanceofExpression newInstanceofExpression = ast.newInstanceofExpression();
    newInstanceofExpression.setLeftOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, (Expression) node.arguments().get(0))));
    newInstanceofExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, klass.getType()));
    ASTNodes.replaceButKeepComment(rewrite, node, ASTNodeFactory.parenthesizeIfNeeded(ast, newInstanceofExpression), group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 83 with ASTNodeFactory

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

the class ObsoleteInvertEqualsCleanUp method invertEqualsInvocation.

private void invertEqualsInvocation(final MethodInvocation visited, final Expression expression, final Expression arg0) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteInvertEqualsCleanUp_description);
    ASTNodes.replaceButKeepComment(rewrite, visited.getExpression(), ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, arg0)), group);
    ASTNodes.replaceButKeepComment(rewrite, arg0, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(expression)), 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 84 with ASTNodeFactory

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

the class ObsoleteRemoveUnnecessaryCastCleanUp method createPrimitive.

private void createPrimitive(final CastExpression visited, final PrefixExpression.Operator prefixOperator, final NumberLiteral literal, final char postfix) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRemoveUnnecessaryCastCleanUp_description);
    NumberLiteral numberLiteral = ast.newNumberLiteral(literal.getToken() + postfix);
    if (prefixOperator != null) {
        PrefixExpression prefixExpression = ast.newPrefixExpression();
        prefixExpression.setOperator(prefixOperator);
        prefixExpression.setOperand(numberLiteral);
        ASTNodes.replaceButKeepComment(rewrite, visited, prefixExpression, group);
    } else {
        ASTNodes.replaceButKeepComment(rewrite, visited, numberLiteral, group);
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 85 with ASTNodeFactory

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

the class ObsoleteTernaryOperatorRatherThanDuplicateConditionsCleanUp method replaceDuplicateExpression.

private void replaceDuplicateExpression(final InfixExpression node, final Expression oneCondition, final Expression oneExpression, final Expression oppositeExpression, final List<Expression> previousOperands, final List<Expression> nextOperands) {
    AtomicBoolean isFirstExprPositive = new AtomicBoolean();
    Expression basicExpression = getBasisExpression(oneCondition, isFirstExprPositive);
    Expression thenExpression;
    Expression elseExpression;
    if (isFirstExprPositive.get()) {
        thenExpression = oneExpression;
        elseExpression = oppositeExpression;
    } else {
        thenExpression = oppositeExpression;
        elseExpression = oneExpression;
    }
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteTernaryOperatorRatherThanDuplicateConditionsCleanUp_description);
    ParenthesizedExpression newConditionalExpression = ast.newParenthesizedExpression(ast.newConditionalExpression(ASTNodes.createMoveTarget(rewrite, basicExpression), ASTNodes.createMoveTarget(rewrite, thenExpression), ASTNodes.createMoveTarget(rewrite, elseExpression)));
    if (previousOperands.isEmpty() && nextOperands.isEmpty()) {
        ASTNodes.replaceButKeepComment(rewrite, node, newConditionalExpression, group);
    } else {
        List<Expression> operands = ASTNodes.createMoveTarget(rewrite, previousOperands);
        operands.add(newConditionalExpression);
        operands.addAll(ASTNodes.createMoveTarget(rewrite, nextOperands));
        ASTNodes.replaceButKeepComment(rewrite, node, ast.newInfixExpression(node.getOperator(), operands), group);
    }
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) 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