use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteAddAllRatherThanLoopCleanUp method replaceWithCollectionMethod.
private void replaceWithCollectionMethod(final ASTNode toReplace, final String methodName, final Expression affectedCollection, final Expression data) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteAddAllRatherThanLoopCleanUp_description);
MethodInvocation newMethod = ast.newMethodInvocation();
newMethod.setName(ast.newSimpleName(methodName));
newMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(data)));
if (affectedCollection != null) {
newMethod.setExpression(ASTNodes.createMoveTarget(rewrite, affectedCollection));
}
ASTNodes.replaceButKeepComment(rewrite, toReplace, ast.newExpressionStatement(newMethod), group);
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteAndConditionRatherThanEmbededIfCleanUp method replaceIfNoElseStatement.
private void replaceIfNoElseStatement(final IfStatement node, final IfStatement innerIf) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteAndConditionRatherThanEmbededIfCleanUp_description);
if (node.getThenStatement() instanceof Block || innerIf.getThenStatement() instanceof Block) {
InfixExpression newInfixExpression = ast.newInfixExpression();
newInfixExpression.setLeftOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, node.getExpression())));
newInfixExpression.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
newInfixExpression.setRightOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, innerIf.getExpression())));
ASTNodes.replaceButKeepComment(rewrite, innerIf.getExpression(), newInfixExpression, group);
ASTNodes.replaceButKeepComment(rewrite, node, ASTNodes.createMoveTarget(rewrite, innerIf), group);
} else {
// 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.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteBreakRatherThanPassiveIterationsCleanUp method addBreak.
private void addBreak(final IfStatement ifStatement, final List<Statement> assignments) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteBreakRatherThanPassiveIterationsCleanUp_description);
if (ifStatement.getThenStatement() instanceof Block) {
rewrite.insertAfter(ast.newBreakStatement(), assignments.get(assignments.size() - 1), group);
} else {
Block newBlock = ast.newBlock();
newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, ifStatement.getThenStatement()));
newBlock.statements().add(ast.newBreakStatement());
ASTNodes.replaceButKeepComment(rewrite, ifStatement.getThenStatement(), newBlock, group);
}
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoletePushNegationDownCleanUp method getOppositeExpression.
private Expression getOppositeExpression(final Expression negativeExpression) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
Expression operand = ASTNodes.getUnparenthesedExpression(negativeExpression);
if (operand instanceof PrefixExpression) {
PrefixExpression pe = (PrefixExpression) operand;
if (ASTNodes.hasOperator(pe, PrefixExpression.Operator.NOT)) {
return ASTNodes.createMoveTarget(rewrite, pe.getOperand());
}
} else if (operand instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression) operand;
InfixExpression.Operator reverseOp = (InfixExpression.Operator) OperatorEnum.getOperator(infixExpression).getReverseBooleanOperator();
if (reverseOp != null) {
List<Expression> allOperands = new ArrayList<>(ASTNodes.allOperands(infixExpression));
if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.AND, InfixExpression.Operator.OR)) {
for (ListIterator<Expression> it = allOperands.listIterator(); it.hasNext(); ) {
Expression anOperand = it.next();
Expression oppositeOperand = getOppositeExpression(anOperand);
it.set(oppositeOperand != null ? oppositeOperand : ast.negate(anOperand, true));
}
} else {
allOperands = ASTNodes.createMoveTarget(rewrite, allOperands);
}
return ast.newParenthesizedExpression(ast.newInfixExpression(reverseOp, allOperands));
}
} else {
Boolean constant = ASTNodes.getBooleanLiteral(operand);
if (constant != null) {
return ast.newBooleanLiteral(!constant);
}
}
return null;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ObsoleteRedundantComparatorCleanUp method removeComparator.
private void removeComparator(final MethodInvocation visitedIfRefactoringNeeded, final Expression list, final Expression comparator, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantComparatorCleanUp_description);
if (visitedIfRefactoringNeeded != null) {
String collectionsClassname = addImport(Collections.class, classesToUseWithImport, importsToAdd);
MethodInvocation sortMethod = ast.newMethodInvocation();
sortMethod.setExpression(ASTNodeFactory.newName(ast, collectionsClassname));
// $NON-NLS-1$
sortMethod.setName(ast.newSimpleName("sort"));
sortMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(list)));
ASTNodes.replaceButKeepComment(rewrite, visitedIfRefactoringNeeded, sortMethod, group);
} else {
ASTNodes.removeButKeepComment(rewrite, comparator, group);
}
}
Aggregations