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);
}
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);
}
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);
}
}
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);
}
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);
}
Aggregations