use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class VectorOldToNewAPIRefactoring method replaceWithSpecial.
private void replaceWithSpecial(final MethodInvocation node, final String newMethodName) {
final List<Expression> args = arguments(node);
assertSize(args, 1);
final Expression arg0 = args.get(0);
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
r.set(node, NAME_PROPERTY, b.simpleName(newMethodName));
if (hasType(arg0, "int", "short", "byte")) {
r.replace(arg0, b.cast(b.type("Object"), b.move(arg0)));
}
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class DoWhileRatherThanWhileRefactoring method visit.
@Override
public boolean visit(WhileStatement node) {
final Object constantCondition = node.getExpression().resolveConstantExpressionValue();
if (Boolean.TRUE.equals(constantCondition)) {
ASTBuilder b = this.ctx.getASTBuilder();
this.ctx.getRefactorings().replace(node, b.doWhile(b.copy(node.getExpression()), b.copy(node.getBody())));
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class EnumSetRatherThanHashSetRefactoring method replace.
/**
* Refactoring is not correct if argument for HashSet constructor is a Collection, but other
* than EnumSet. <br>
* In case of empty collection <code>EnumSet.copyOf</code> will throw an
* <code>IllegalArgumentException</code>, <br>
* and HashSet(Collection) will not. <br>
* <br>
* Other constructors can be replaced with <code>EnumSet.noneOf(Class)</code> method. <br>
* <br>
*
* @see {@link java.util.EnumSet#copyOf(Collection)}
* @see {@link java.util.EnumSet#copyOf(EnumSet)}
* @see {@link java.util.EnumSet#noneOf(Class)} <br>
* @param cic
* - class instance creation node to be replaced
* @param type
* - type argument of the declaration
*/
@Override
boolean replace(ClassInstanceCreation cic, Type... types) {
if (types == null || types.length < 1) {
return VISIT_SUBTREE;
}
Type type = types[0];
ASTBuilder b = ctx.getASTBuilder();
List<Expression> arguments = arguments(cic);
final MethodInvocation invocation;
if (!arguments.isEmpty() && instanceOf(arguments.get(0), "java.util.Collection")) {
Expression typeArg = arguments.get(0);
if (!instanceOf(typeArg, "java.util.EnumSet")) {
return VISIT_SUBTREE;
}
invocation = b.invoke(b.name("java", "util", "EnumSet"), "copyOf", b.copy(typeArg));
} else {
TypeLiteral newTypeLiteral = ctx.getAST().newTypeLiteral();
newTypeLiteral.setType(b.copy(type));
invocation = b.invoke(b.name("java", "util", "EnumSet"), "noneOf", newTypeLiteral);
}
ctx.getRefactorings().replace(cic, invocation);
return DO_NOT_VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class RemoveUnnecessaryCastRefactoring method visit.
@Override
public boolean visit(CastExpression node) {
if (canRemoveCast(node)) {
final ASTBuilder b = ctx.getASTBuilder();
ctx.getRefactorings().replace(node, b.move(node.getExpression()));
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class CollapseIfStatementRefactoring method replaceIfNoElseStatement.
private boolean replaceIfNoElseStatement(IfStatement outerIf, IfStatement innerIf) {
if (innerIf.getElseStatement() != null) {
return VISIT_SUBTREE;
}
final ASTBuilder b = this.ctx.getASTBuilder();
final InfixExpression ie = b.infixExpr(parenthesizeOrExpr(b, outerIf.getExpression()), CONDITIONAL_AND, parenthesizeOrExpr(b, innerIf.getExpression()));
this.ctx.getRefactorings().replace(outerIf.getExpression(), ie);
this.ctx.getRefactorings().replace(outerIf.getThenStatement(), b.copy(innerIf.getThenStatement()));
return DO_NOT_VISIT_SUBTREE;
}
Aggregations