use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class SimplifyExpressionRefactoring method replaceWithNewInfixExpr.
private boolean replaceWithNewInfixExpr(InfixExpression node, final List<Expression> remainingOperands) {
if (remainingOperands.size() == 1) {
replaceBy(node, remainingOperands.get(0));
} else {
final ASTBuilder b = ctx.getASTBuilder();
ctx.getRefactorings().replace(node, b.infixExpr(node.getOperator(), b.move(remainingOperands)));
}
return DO_NOT_VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class StringBuilderMethodRatherThanReassignationRefactoring method visit.
@Override
public boolean visit(Assignment node) {
final Expression targetVar = node.getLeftHandSide();
Expression var = node.getRightHandSide();
if (ASSIGN.equals(node.getOperator()) && hasType(targetVar, "java.lang.StringBuffer", "java.lang.StringBuilder") && var instanceof MethodInvocation) {
var = getVar(var);
if (isSameVariable(targetVar, var)) {
final ASTBuilder b = this.ctx.getASTBuilder();
ctx.getRefactorings().replace(node, b.copy(node.getRightHandSide()));
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class StringBuilderRefactoring method createStringConcats.
private Expression createStringConcats(final List<Pair<ITypeBinding, Expression>> appendedStrings) {
final ASTBuilder b = this.ctx.getASTBuilder();
switch(appendedStrings.size()) {
case 0:
return b.string("");
case 1:
final Pair<ITypeBinding, Expression> expr = appendedStrings.get(0);
if (hasType(expr.getSecond(), "java.lang.String")) {
return b.copy(expr.getSecond());
}
return b.invoke("String", "valueOf", getTypedExpression(b, expr));
default:
// >= 2
boolean isFirstAndNotAString = isFirstAndNotAString(appendedStrings);
List<Expression> concatenateStrings = new ArrayList<Expression>(appendedStrings.size());
for (final Pair<ITypeBinding, Expression> typeAndValue : appendedStrings) {
if (isFirstAndNotAString) {
concatenateStrings.add(b.invoke("String", "valueOf", getTypedExpression(b, typeAndValue)));
isFirstAndNotAString = false;
} else {
concatenateStrings.add(b.parenthesizeIfNeeded(getTypedExpression(b, typeAndValue)));
}
}
return b.infixExpr(Operator.PLUS, concatenateStrings);
}
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class StringBuilderRefactoring method replaceWithAppendSubstring.
private void replaceWithAppendSubstring(final MethodInvocation node, final MethodInvocation embeddedMI) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Expression stringVar = b.copy(embeddedMI.getExpression());
final List<Expression> args = arguments(embeddedMI);
final Expression arg0 = b.copy(args.get(0));
final Expression arg1 = b.copy(args.get(1));
final Expression lastExpr = b.copy(node.getExpression());
MethodInvocation newAppendSubstring = null;
if (arg1 == null) {
newAppendSubstring = b.invoke(lastExpr, "append", stringVar, arg0);
} else {
newAppendSubstring = b.invoke(lastExpr, "append", stringVar, arg0, arg1);
}
this.ctx.getRefactorings().replace(node, newAppendSubstring);
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class StringBuilderRefactoring method replaceWithNewStringAppends.
/**
* Rewrite the successive calls to append()
*
* @param node The node to replace.
* @param allAppendedStrings All appended strings.
* @param lastExpr The expression on which the methods are called.
* @param isInstanceCreationToRewrite
*/
private void replaceWithNewStringAppends(final Expression node, final LinkedList<Pair<ITypeBinding, Expression>> allAppendedStrings, final Expression lastExpr, final boolean isInstanceCreationToRewrite) {
final ASTBuilder b = this.ctx.getASTBuilder();
Expression result = null;
final List<Expression> tempStringLiterals = new ArrayList<Expression>();
final List<Expression> finalStrings = new ArrayList<Expression>();
final AtomicBoolean isFirst = new AtomicBoolean(true);
for (final Pair<ITypeBinding, Expression> appendedString : allAppendedStrings) {
if (appendedString.getSecond() instanceof StringLiteral) {
tempStringLiterals.add(b.copy(appendedString.getSecond()));
} else {
result = handleTempStringLiterals(b, lastExpr, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
if (isFirst.get()) {
isFirst.set(false);
if (!isInstanceCreationToRewrite) {
result = b.copy(lastExpr);
finalStrings.add(getTypedExpression(b, appendedString));
} else if ((appendedString.getFirst() != null) ? hasType(appendedString.getFirst(), "java.lang.String") : hasType(appendedString.getSecond(), "java.lang.String")) {
result = b.new0(b.copy(((ClassInstanceCreation) lastExpr).getType()), getTypedExpression(b, appendedString));
} else {
result = b.new0(b.copy(((ClassInstanceCreation) lastExpr).getType()));
finalStrings.add(getTypedExpression(b, appendedString));
}
} else {
finalStrings.add(getTypedExpression(b, appendedString));
}
}
}
result = handleTempStringLiterals(b, lastExpr, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
for (final Expression finalString : finalStrings) {
if (result == null) {
result = finalString;
} else {
result = b.invoke(result, "append", finalString);
}
}
ctx.getRefactorings().replace(node, result);
}
Aggregations