Search in sources :

Example 71 with ASTRewrite

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

the class ObsoleteOppositeConditionRatherThanDuplicateConditionCleanUp method refactorCondition.

private void refactorCondition(final IfStatement node, final Expression duplicateExpression, final Expression notDuplicateExpression, final Statement positiveStatement, final Statement negativeStatement) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteOppositeConditionRatherThanDuplicateConditionCleanUp_description);
    Statement negativeStmtCopy;
    if (negativeStatement instanceof IfStatement) {
        Block newBlock = ast.newBlock();
        newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, negativeStatement));
        negativeStmtCopy = newBlock;
    } else {
        negativeStmtCopy = ASTNodes.createMoveTarget(rewrite, negativeStatement);
    }
    Expression secondCond;
    Statement secondStmtCopy;
    Statement thirdStmtCopy;
    PrefixExpression negativeCond = ASTNodes.as(notDuplicateExpression, PrefixExpression.class);
    if (negativeCond != null && ASTNodes.hasOperator(negativeCond, PrefixExpression.Operator.NOT)) {
        secondCond = negativeCond.getOperand();
        secondStmtCopy = ASTNodes.createMoveTarget(rewrite, positiveStatement);
        thirdStmtCopy = ASTNodes.createMoveTarget(rewrite, node.getThenStatement());
    } else {
        secondCond = notDuplicateExpression;
        secondStmtCopy = ASTNodes.createMoveTarget(rewrite, node.getThenStatement());
        thirdStmtCopy = ASTNodes.createMoveTarget(rewrite, positiveStatement);
    }
    ASTNodes.replaceButKeepComment(rewrite, node.getExpression(), ast.negate(duplicateExpression, true), group);
    ASTNodes.replaceButKeepComment(rewrite, node.getThenStatement(), negativeStmtCopy, group);
    IfStatement newIfStatement = ast.newIfStatement();
    newIfStatement.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(secondCond)));
    newIfStatement.setThenStatement(secondStmtCopy);
    newIfStatement.setElseStatement(thirdStmtCopy);
    ASTNodes.replaceButKeepComment(rewrite, node.getElseStatement(), newIfStatement, group);
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 72 with ASTRewrite

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

the class ObsoleteParsingRatherThanValueOfCleanUp method replaceByParsing.

private void replaceByParsing(final MethodInvocation visited, final ITypeBinding typeBinding, final String methodName, final Expression arg0) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteParsingRatherThanValueOfCleanUp_description);
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setExpression(ASTNodeFactory.newName(ast, typeBinding.getName()));
    methodInvocation.setName(ast.newSimpleName(methodName));
    methodInvocation.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(arg0)));
    MethodInvocation newMethodInvocation = methodInvocation;
    ASTNodes.replaceButKeepComment(rewrite, visited, newMethodInvocation, 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 73 with ASTRewrite

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

the class ObsoleteOneConditionRatherThanUnreachableBlockCleanUp method refactorCondition.

private void refactorCondition(final IfStatement secondIf) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteOneConditionRatherThanUnreachableBlockCleanUp_description);
    if (secondIf.getElseStatement() == null) {
        rewrite.remove(secondIf, group);
    } else {
        ASTNodes.replaceButKeepComment(rewrite, secondIf, ASTNodes.createMoveTarget(rewrite, secondIf.getElseStatement()), group);
    }
}
Also used : ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 74 with ASTRewrite

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

the class ObsoleteOperandFactorizationCleanUp method replaceDuplicateExpression.

private void replaceDuplicateExpression(final InfixExpression visited, final InfixExpression firstCondition, final Expression firstExpression, final Expression secondExpression, final Expression secondOppositeExpression) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteOperandFactorizationCleanUp_description);
    InfixExpression newInnerInfixExpression = ast.newInfixExpression();
    newInnerInfixExpression.setOperator(visited.getOperator());
    newInnerInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, secondExpression));
    newInnerInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, secondOppositeExpression));
    InfixExpression newMainInfixExpression = ast.newInfixExpression();
    newMainInfixExpression.setOperator(firstCondition.getOperator());
    newMainInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, firstExpression));
    newMainInfixExpression.setRightOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, newInnerInfixExpression));
    ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodeFactory.parenthesizeIfNeeded(ast, newMainInfixExpression), 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 75 with ASTRewrite

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

the class ObsoleteRedundantTruthCleanUp method removeBooleanConstant.

private void removeBooleanConstant(final ASTNode visited, final Expression expressionToCopy, final boolean isTrue, final boolean isEquals) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantTruthCleanUp_description);
    Expression operand;
    if (isTrue == isEquals) {
        operand = ASTNodes.createMoveTarget(rewrite, expressionToCopy);
    } else {
        operand = ast.negate(expressionToCopy, true);
    }
    rewrite.replace(visited, ASTNodeFactory.parenthesizeIfNeeded(ast, operand), group);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) 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

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