use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class PushNegationDownRefactoring method visit.
@Override
public boolean visit(PrefixExpression node) {
if (!hasOperator(node, NOT)) {
return VISIT_SUBTREE;
}
final ASTBuilder b = ctx.getASTBuilder();
final Refactorings r = ctx.getRefactorings();
final Expression operand = removeParentheses(node.getOperand());
if (operand instanceof PrefixExpression) {
final PrefixExpression pe = (PrefixExpression) operand;
if (hasOperator(pe, NOT)) {
r.replace(node, b.move(pe.getOperand()));
return DO_NOT_VISIT_SUBTREE;
}
} else if (operand instanceof InfixExpression) {
final InfixExpression ie = (InfixExpression) operand;
final Operator reverseOp = (Operator) OperatorEnum.getOperator(ie).getReverseBooleanOperator();
if (reverseOp != null) {
List<Expression> allOperands = new ArrayList<Expression>(allOperands(ie));
if (Arrays.<Operator>asList(CONDITIONAL_AND, CONDITIONAL_OR, AND, OR).contains(ie.getOperator())) {
for (ListIterator<Expression> it = allOperands.listIterator(); it.hasNext(); ) {
it.set(b.negate(it.next()));
}
} else {
allOperands = b.move(allOperands);
}
r.replace(node, b.parenthesize(b.infixExpr(reverseOp, allOperands)));
return DO_NOT_VISIT_SUBTREE;
}
} else {
final Boolean constant = getBooleanLiteral(operand);
if (constant != null) {
r.replace(node, b.boolean0(!constant));
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class ReduceVariableScopeRefactoring method getVariableDeclarationFragment.
private VariableDeclarationFragment getVariableDeclarationFragment(Expression exprToReplace, Name varName) {
if (exprToReplace instanceof Assignment) {
final Assignment a = (Assignment) exprToReplace;
if (a.getLeftHandSide() instanceof SimpleName) {
final SimpleName sn = (SimpleName) a.getLeftHandSide();
if (sn.getFullyQualifiedName().equals(varName.getFullyQualifiedName())) {
final ASTBuilder b = this.ctx.getASTBuilder();
final VariableDeclarationFragment vdf = b.getAST().newVariableDeclarationFragment();
vdf.setInitializer(b.copy(a.getRightHandSide()));
vdf.setName(b.copy(sn));
return vdf;
}
}
throw new NotImplementedException(a.getLeftHandSide());
}
throw new NotImplementedException(exprToReplace);
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class RemoveEmptyIfRefactoring method visit.
@Override
public boolean visit(IfStatement node) {
final Refactorings r = this.ctx.getRefactorings();
final Statement thenStmt = node.getThenStatement();
final Statement elseStmt = node.getElseStatement();
if (elseStmt != null && asList(elseStmt).isEmpty()) {
r.remove(elseStmt);
return DO_NOT_VISIT_SUBTREE;
} else if (thenStmt != null && asList(thenStmt).isEmpty()) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Expression condition = node.getExpression();
if (elseStmt != null) {
r.replace(node, b.if0(b.negate(condition), b.move(elseStmt)));
} else if (isPassive(condition)) {
removeBlock(node, r, b);
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class ReplaceQualifiedNamesBySimpleNamesRefactoring method maybeReplaceFqnWithSimpleName.
private boolean maybeReplaceFqnWithSimpleName(final QualifiedName node, final Set<String> localIdentifiers) {
final ASTNode ancestor = getFirstAncestorOrNull(node, PackageDeclaration.class, ImportDeclaration.class);
final QName qname = getFullyQualifiedNameOrNull(node);
if (ancestor != null || qname == null) {
return VISIT_SUBTREE;
}
if (types.canReplaceFqnWithSimpleName(node, qname, FqnType.TYPE) || (fields.canReplaceFqnWithSimpleName(node, qname, FqnType.FIELD) && !localIdentifiers.contains(qname.simpleName))) {
final ASTBuilder b = ctx.getASTBuilder();
ctx.getRefactorings().replace(node, b.copy(node.getName()));
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class SimplifyExpressionRefactoring method replace.
private boolean replace(final InfixExpression node, final boolean isTrue, final Expression exprToCopy) {
checkNoExtendedOperands(node);
if (!isPrimitive(node.getLeftOperand(), "boolean") && !isPrimitive(node.getRightOperand(), "boolean")) {
return VISIT_SUBTREE;
}
// Either:
// - Two boolean primitives: no possible NPE
// - One boolean primitive and one Boolean object, this code already run
// the risk of an NPE, so we can replace the infix expression without
// fearing we would introduce a previously non existing NPE.
final ASTBuilder b = this.ctx.getASTBuilder();
final Expression operand;
if (isTrue == hasOperator(node, EQUALS)) {
operand = b.copy(exprToCopy);
} else {
operand = b.negate(exprToCopy);
}
this.ctx.getRefactorings().replace(node, operand);
return DO_NOT_VISIT_SUBTREE;
}
Aggregations