use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class AndroidViewHolderRefactoring method createViewHolderItemClass.
private TypeDeclaration createViewHolderItemClass(FindViewByIdVisitor findViewByIdVisitor, SimpleName typeName, TypeNameDecider typeNameDecider) {
final ASTBuilder b = this.ctx.getASTBuilder();
TypeDeclaration result = b.getAST().newTypeDeclaration();
modifiers(result).addAll(asList(b.private0(), b.static0()));
result.setName(typeName);
List<BodyDeclaration> viewItemsFieldDecls = bodyDeclarations(result);
for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
viewItemsFieldDecls.add(item.toFieldDecl(b, typeNameDecider));
}
return result;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class CommonCodeInIfElseStatementRefactoring method visit.
// TODO handle switch statements
// TODO also handle ternary operator, ConditionalExpression
@Override
public boolean visit(IfStatement node) {
if (node.getElseStatement() == null) {
return VISIT_SUBTREE;
}
if (!(node.getParent() instanceof Block)) {
// when not inside curly braces
return VISIT_SUBTREE;
}
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
final List<List<Statement>> allCasesStmts = new ArrayList<List<Statement>>();
final List<List<ASTNode>> removedCaseStmts = new LinkedList<List<ASTNode>>();
// Collect all the if / else if / else if / ... / else cases
if (collectAllCases(allCasesStmts, node)) {
// initialize removedCaseStmts list
for (int i = 0; i < allCasesStmts.size(); i++) {
removedCaseStmts.add(new LinkedList<ASTNode>());
}
// If all cases exist
final ASTMatcher matcher = new ASTMatcherSameVariablesAndMethods();
final int minSize = minSize(allCasesStmts);
final List<Statement> caseStmts = allCasesStmts.get(0);
boolean result = VISIT_SUBTREE;
// Identify matching statements starting from the beginning of each case
for (int stmtIndex = 0; stmtIndex < minSize; stmtIndex++) {
if (!match(matcher, allCasesStmts, true, stmtIndex, 0, allCasesStmts.size())) {
break;
}
r.insertBefore(b.copy(caseStmts.get(stmtIndex)), node);
removeStmts(allCasesStmts, true, stmtIndex, removedCaseStmts);
result = DO_NOT_VISIT_SUBTREE;
}
// Identify matching statements starting from the end of each case
for (int stmtIndex = 1; 0 <= minSize - stmtIndex; stmtIndex++) {
if (!match(matcher, allCasesStmts, false, stmtIndex, 0, allCasesStmts.size()) || anyContains(removedCaseStmts, allCasesStmts, stmtIndex)) {
break;
}
r.insertAfter(b.copy(caseStmts.get(caseStmts.size() - stmtIndex)), node);
removeStmts(allCasesStmts, false, stmtIndex, removedCaseStmts);
result = DO_NOT_VISIT_SUBTREE;
}
// Remove the nodes common to all cases
final boolean[] areCasesEmpty = new boolean[allCasesStmts.size()];
for (int i = 0; i < allCasesStmts.size(); i++) {
areCasesEmpty[i] = false;
}
removeStmtsFromCases(allCasesStmts, removedCaseStmts, areCasesEmpty);
if (allEmpty(areCasesEmpty)) {
r.removeButKeepComment(node);
return DO_NOT_VISIT_SUBTREE;
}
// Remove empty cases
if (areCasesEmpty[0]) {
if (areCasesEmpty.length == 2 && !areCasesEmpty[1]) {
// Then clause is empty and there is only one else clause
// => revert if statement
r.replace(node, b.if0(b.not(b.parenthesizeIfNeeded(b.move(node.getExpression()))), b.move(node.getElseStatement())));
} else {
r.replace(node.getThenStatement(), b.block());
}
result = DO_NOT_VISIT_SUBTREE;
}
for (int i = 1; i < areCasesEmpty.length; i++) {
if (areCasesEmpty[i]) {
final Statement firstStmt = allCasesStmts.get(i).get(0);
r.remove(findNodeToRemove(firstStmt, firstStmt.getParent()));
result = DO_NOT_VISIT_SUBTREE;
}
}
return result;
}
return VISIT_SUBTREE;
}
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 IfRatherThanWhileAndFallsThroughRefactoring method visit.
@Override
public boolean visit(WhileStatement node) {
if (isEndingWithExit(node.getBody())) {
final BreakVisitor breakVisitor = new BreakVisitor(node);
breakVisitor.visitNode(node);
if (breakVisitor.canBeRefactored()) {
final ASTBuilder b = ctx.getASTBuilder();
for (final BreakStatement breakStmt : breakVisitor.getBreaks()) {
ctx.getRefactorings().remove(breakStmt);
}
ctx.getRefactorings().replace(node, b.if0(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 InlineCodeRatherThanPeremptoryConditionRefactoring method replaceBlockByPlainCode.
@SuppressWarnings("unchecked")
private void replaceBlockByPlainCode(final Statement sourceNode, final Statement unconditionnalStatement) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
if (unconditionnalStatement instanceof Block && sourceNode.getParent() instanceof Block) {
r.replace(sourceNode, b.copyRange(((Block) unconditionnalStatement).statements()));
} else {
r.replace(sourceNode, b.copy(unconditionnalStatement));
}
}
Aggregations