use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class EnumMapRatherThanHashMapRefactoring method replace.
/**
* Replace given class instance creation with suitable EnumMap constructor. <br>
* <br>
* Replacement is not correct if HashMap constructor accepts map <br>
* other than EnumMap, because it throws <code>IllegalArgumentException</code> if map is empty,
* <br>
* and HashMap(Map) does not. Therefore, for correctness reasons, it should not be refactored.
* <br>
*
* @see {@link java.util.EnumMap#EnumMap(java.util.Map)}
* @see {@link java.util.HashMap#HashMap(java.util.Map)}
*/
@Override
boolean replace(ClassInstanceCreation cic, Type... types) {
if (types == null || types.length < 2) {
return VISIT_SUBTREE;
}
Type keyType = types[0];
Type valueType = types[1];
ASTBuilder b = ctx.getASTBuilder();
List<Expression> arguments = arguments(cic);
if (!arguments.isEmpty() && isTargetType(arguments.get(0).resolveTypeBinding()) && !hasType(arguments.get(0).resolveTypeBinding(), "java.util.EnumMap")) {
return VISIT_SUBTREE;
}
Expression newParam = resolveParameter(keyType, arguments);
Type newType = b.genericType("java.util.EnumMap", b.copy(keyType), b.copy(valueType));
// remove them from replacement
if (typeArgs(cic.getType()).isEmpty()) {
typeArgs(newType).clear();
}
ctx.getRefactorings().replace(cic, b.new0(newType, newParam));
return DO_NOT_VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class MergeConditionalBlocksRefactoring method maybeMergeBlocks.
private boolean maybeMergeBlocks(final Expression firstCondition, final List<Statement> ifCode, final IfStatement subNode, final Expression secondCondition, final Statement doubleStmts, final Statement remainingStmts, final boolean isPositive) {
if (isSameCode(ifCode, asList(doubleStmts))) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
final Expression additionalCondition;
if (isPositive) {
additionalCondition = b.copy(secondCondition);
} else {
additionalCondition = b.negate(secondCondition, Copy.COPY);
}
r.replace(firstCondition, b.infixExpr(b.parenthesizeIfNeeded(b.copy(firstCondition)), InfixExpression.Operator.CONDITIONAL_OR, b.parenthesizeIfNeeded(additionalCondition)));
if (remainingStmts != null) {
r.replace(subNode, b.copy(remainingStmts));
} else {
r.remove(subNode);
}
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class StringValueOfRatherThanConcatRefactoring method maybeReplaceStringConcatenation.
private boolean maybeReplaceStringConcatenation(final InfixExpression node, final Expression expr, final Expression variable) {
if (expr instanceof StringLiteral && ((StringLiteral) expr).getLiteralValue().matches("") && !hasType(variable, "java.lang.String", "char[]")) {
final ASTBuilder b = this.ctx.getASTBuilder();
ctx.getRefactorings().replace(node, b.invoke("String", "valueOf", b.copy(variable)));
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class UseStringContainsRefactoring method replaceWithStringContains.
private boolean replaceWithStringContains(InfixExpression ie, MethodInvocation node, boolean negate) {
final Refactorings r = this.ctx.getRefactorings();
final ASTBuilder b = this.ctx.getASTBuilder();
r.set(node, MethodInvocation.NAME_PROPERTY, b.simpleName("contains"));
if (negate) {
r.replace(ie, b.not(b.move(node)));
} else {
r.replace(ie, b.move(node));
}
return DO_NOT_VISIT_SUBTREE;
}
Aggregations