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