use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.
the class UpdateSetRatherThanTestingFirstRefactoring method maybeReplaceSetContains.
private boolean maybeReplaceSetContains(final IfStatement ifStmtToReplace, final Expression ifExpr, final Statement stmt, final Statement oppositeStmt, final boolean negate, final String methodName) {
final List<Statement> stmts = asList(stmt);
final MethodInvocation miContains = as(ifExpr, MethodInvocation.class);
if (!stmts.isEmpty() && isMethod(miContains, "java.util.Set", "contains", "java.lang.Object")) {
final Statement firstStmt = getFirst(stmts);
final MethodInvocation miAddOrRemove = asExpression(firstStmt, MethodInvocation.class);
final ASTMatcher astMatcher = new ASTMatcher();
if (isMethod(miAddOrRemove, "java.util.Set", methodName, "java.lang.Object") && match(astMatcher, miContains.getExpression(), miAddOrRemove.getExpression()) && match(astMatcher, arg0(miContains), arg0(miAddOrRemove))) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
if (stmts.size() == 1 && asList(oppositeStmt).isEmpty()) {
// Only one statement: replace if statement with col.add() (or col.remove())
r.replace(ifStmtToReplace, b.move(firstStmt));
return DO_NOT_VISIT_SUBTREE;
} else {
// There are other statements, replace the if condition with col.add() (or col.remove())
r.replace(ifStmtToReplace.getExpression(), negate ? b.negate(miAddOrRemove, ASTBuilder.Copy.MOVE) : b.move(miAddOrRemove));
r.remove(firstStmt);
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.
the class MapEliminateKeySetCallsRefactoring method replaceEntryIterationByKeyIteration.
private void replaceEntryIterationByKeyIteration(EnhancedForStatement enhancedFor, final Expression mapExpression, final SingleVariableDeclaration parameter, final List<MethodInvocation> getValueMis) {
final ASTBuilder b = ctx.getASTBuilder();
final Refactorings r = ctx.getRefactorings();
final VariableDefinitionsUsesVisitor keyUseVisitor = new VariableDefinitionsUsesVisitor(parameter);
enhancedFor.getBody().accept(keyUseVisitor);
int keyUses = keyUseVisitor.getUses().size();
final int insertionPoint = asList(enhancedFor.getBody()).get(0).getStartPosition() - 1;
final Variable entryVar = new Variable(new VariableNameDecider(enhancedFor.getBody(), insertionPoint).suggest("entry", "mapEntry"), b);
final TypeNameDecider typeNameDecider = new TypeNameDecider(parameter);
final MethodInvocation getValueMi0 = getValueMis.get(0);
final ITypeBinding typeBinding = getValueMi0.getExpression().resolveTypeBinding();
if (typeBinding != null && typeBinding.isRawType()) {
// for (Object key : map.keySet()) => for (Object key : map.entrySet())
r.set(enhancedFor, EXPRESSION_PROPERTY, b.invoke(b.move(mapExpression), "entrySet"));
final Type objectType = b.type(typeNameDecider.useSimplestPossibleName("java.lang.Object"));
final Variable objectVar = new Variable(new VariableNameDecider(enhancedFor.getBody(), insertionPoint).suggest("obj"), b);
r.set(enhancedFor, PARAMETER_PROPERTY, b.declareSingleVariable(objectVar.varNameRaw(), objectType));
// for (Map.Entry<K, V> mapEntry : map.entrySet()) {
// Map.Entry mapEntry = (Map.Entry) obj; // <--- add this statement
// Object key = mapEntry.getKey(); // <--- add this statement
final Type mapKeyType = b.copy(parameter.getType());
final VariableDeclarationStatement newKeyDecl = b.declareStmt(mapKeyType, b.move(parameter.getName()), b.invoke(entryVar.varName(), "getKey"));
r.insertFirst(enhancedFor.getBody(), Block.STATEMENTS_PROPERTY, newKeyDecl);
if (keyUses > getValueMis.size()) {
String mapEntryTypeName = typeNameDecider.useSimplestPossibleName("java.util.Map.Entry");
final VariableDeclarationStatement newEntryDecl = b.declareStmt(b.type(mapEntryTypeName), entryVar.varName(), b.cast(b.type(mapEntryTypeName), objectVar.varName()));
r.insertFirst(enhancedFor.getBody(), Block.STATEMENTS_PROPERTY, newEntryDecl);
}
} else {
// for (K key : map.keySet()) => for (K key : map.entrySet())
r.set(enhancedFor, EXPRESSION_PROPERTY, b.invoke(b.move(mapExpression), "entrySet"));
// for (K key : map.entrySet()) => for (Map.Entry<K, V> mapEntry : map.entrySet())
final Type mapEntryType = createMapEntryType(parameter, getValueMi0, typeNameDecider);
r.set(enhancedFor, PARAMETER_PROPERTY, b.declareSingleVariable(entryVar.varNameRaw(), mapEntryType));
if (keyUses > getValueMis.size()) {
// for (Map.Entry<K, V> mapEntry : map.entrySet()) {
// K key = mapEntry.getKey(); // <--- add this statement
final Type mapKeyType = b.copy(parameter.getType());
final VariableDeclarationStatement newKeyDeclaration = b.declareStmt(mapKeyType, b.move(parameter.getName()), b.invoke(entryVar.varName(), "getKey"));
r.insertFirst(enhancedFor.getBody(), Block.STATEMENTS_PROPERTY, newKeyDeclaration);
}
}
// Replace all occurrences of map.get(key) => mapEntry.getValue()
for (MethodInvocation getValueMi : getValueMis) {
r.replace(getValueMi, b.invoke(entryVar.varName(), "getValue"));
}
}
use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.
the class RemoveUselessBlockRefactoring method replaceBlock.
@SuppressWarnings("unchecked")
private void replaceBlock(final Block node) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
r.replace(node, b.copyRange(node.statements()));
}
use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.
the class SimplifyExpressionRefactoring method maybeReduceBooleanExpression.
private boolean maybeReduceBooleanExpression(final InfixExpression node, final Expression leftExpr, final Expression rightExpr) {
final Boolean leftBoolean = getBooleanLiteral(leftExpr);
final Boolean rightBoolean = getBooleanLiteral(rightExpr);
if (leftBoolean != null) {
return replace(node, leftBoolean.booleanValue(), rightExpr);
} else if (rightBoolean != null) {
return replace(node, rightBoolean.booleanValue(), leftExpr);
}
Expression leftOppositeExpr = null;
final PrefixExpression leftPrefix = as(leftExpr, PrefixExpression.class);
if (leftPrefix != null && hasOperator(leftPrefix, NOT)) {
leftOppositeExpr = leftPrefix.getOperand();
}
Expression rightOppositeExpr = null;
final PrefixExpression rightPrefix = as(rightExpr, PrefixExpression.class);
if (rightPrefix != null && hasOperator(rightPrefix, NOT)) {
rightOppositeExpr = rightPrefix.getOperand();
}
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
if (leftOppositeExpr != null && rightOppositeExpr != null) {
r.replace(node, b.infixExpr(b.copy(leftOppositeExpr), getAppropriateOperator(node), b.copy(rightOppositeExpr)));
return DO_NOT_VISIT_SUBTREE;
} else if (leftOppositeExpr != null) {
final Operator reverseOp = getReverseOperator(node);
r.replace(node, b.infixExpr(b.copy(leftOppositeExpr), reverseOp, b.copy(rightExpr)));
return DO_NOT_VISIT_SUBTREE;
} else if (rightOppositeExpr != null) {
final Operator reverseOp = getReverseOperator(node);
r.replace(node, b.infixExpr(b.copy(leftExpr), reverseOp, b.copy(rightOppositeExpr)));
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.
the class StaticConstantRatherThanInstanceConstantRefactoring method addStaticModifier.
private void addStaticModifier(final Modifier finalModifier) {
final ASTBuilder b = ctx.getASTBuilder();
final Refactorings r = ctx.getRefactorings();
r.insertBefore(b.static0(), finalModifier);
}
Aggregations