use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteComparisonRatherThanEqualsCleanUp method replaceEquals.
private void replaceEquals(final boolean isPositive, final Expression visited, final MethodInvocation methodInvocation) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteComparisonRatherThanEqualsCleanUp_description);
if (isInStringAppend(methodInvocation.getParent())) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
ASTNodes.replaceButKeepComment(rewrite, visited, ast.newParenthesizedExpression(getCompareToNode(isPositive, methodInvocation)), group);
} else {
ASTNodes.replaceButKeepComment(rewrite, visited, getCompareToNode(isPositive, methodInvocation), group);
}
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteComparisonRatherThanEqualsCleanUp method getCompareToNode.
private InfixExpression getCompareToNode(final boolean isPositive, final MethodInvocation visited) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
MethodInvocation compareToMethod = ast.newMethodInvocation();
compareToMethod.setExpression(ASTNodes.createMoveTarget(rewrite, visited.getExpression()));
// $NON-NLS-1$
compareToMethod.setName(ast.newSimpleName("compareTo"));
compareToMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, (Expression) visited.arguments().get(0)));
InfixExpression newInfixExpression = ast.newInfixExpression();
newInfixExpression.setLeftOperand(compareToMethod);
newInfixExpression.setOperator(isPositive ? InfixExpression.Operator.EQUALS : InfixExpression.Operator.NOT_EQUALS);
newInfixExpression.setRightOperand(ast.newNumberLiteral(0));
return newInfixExpression;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteDoubleNegationCleanUp method removeDoubleNegation.
private void removeDoubleNegation(final InfixExpression visited, final Expression leftExpression, final Expression rightExpression, final Expression leftNegatedExpression, final Expression rightNegatedExpression) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteDoubleNegationCleanUp_description);
InfixExpression newInfixExpression = ast.newInfixExpression();
if (leftNegatedExpression != null) {
newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, leftNegatedExpression));
if (rightNegatedExpression != null) {
newInfixExpression.setOperator(getAppropriateOperator(visited));
newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, rightNegatedExpression));
} else {
newInfixExpression.setOperator(getNegatedOperator(visited));
newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, rightExpression));
}
} else {
newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, leftExpression));
newInfixExpression.setOperator(getNegatedOperator(visited));
newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, rightNegatedExpression));
}
rewrite.replace(visited, newInfixExpression, group);
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteDuplicateAlternativeCleanUp method replaceIfNoElseStatement.
private void replaceIfNoElseStatement(final IfStatement visited, final IfStatement innerIf, final Statement innerMainStatement, final boolean isInnerMainFirst) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteDuplicateAlternativeCleanUp_description);
if (visited.getThenStatement() instanceof Block || innerMainStatement instanceof Block) {
InfixExpression newInfixExpression = ast.newInfixExpression();
Expression outerCondition;
if (isInnerMainFirst) {
outerCondition = ASTNodes.createMoveTarget(rewrite, visited.getExpression());
} else {
outerCondition = ast.negate(visited.getExpression(), true);
}
newInfixExpression.setLeftOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, outerCondition));
newInfixExpression.setOperator(isInnerMainFirst ? InfixExpression.Operator.CONDITIONAL_AND : InfixExpression.Operator.CONDITIONAL_OR);
newInfixExpression.setRightOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, innerIf.getExpression())));
ASTNodes.replaceButKeepComment(rewrite, innerIf.getExpression(), newInfixExpression, group);
ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, innerIf), group);
} else {
// Workaround: Do not do the cleanup
// Prepare the code for the next pass
Block newBlock = ast.newBlock();
newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, innerIf));
ASTNodes.replaceButKeepComment(rewrite, innerIf, newBlock, group);
}
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class ObsoleteFillRatherThanLoopCleanUp method replaceWithArraysFill.
private void replaceWithArraysFill(final ForStatement node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final Assignment assignment, final ArrayAccess arrayAccess) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteFillRatherThanLoopCleanUp_description);
String classname = addImport(Arrays.class, classesToUseWithImport, importsToAdd);
MethodInvocation fillMethod = ast.newMethodInvocation();
fillMethod.setExpression(ASTNodeFactory.newName(ast, classname));
// $NON-NLS-1$
fillMethod.setName(ast.newSimpleName("fill"));
fillMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(arrayAccess.getArray())));
fillMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(assignment.getRightHandSide())));
ASTNodes.replaceButKeepComment(rewrite, node, ast.newExpressionStatement(fillMethod), group);
}
Aggregations