use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class RemoveUselessModifiersRefactoring method moveToIndex.
private void moveToIndex(Modifier m, int index) {
final ASTBuilder b = ctx.getASTBuilder();
ctx.getRefactorings().moveToIndex(m, index, b.move(m));
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class AbstractClassSubstituteRefactoring method replaceClass.
private void replaceClass(final ClassInstanceCreation originalInstanceCreation, final List<VariableDeclaration> variableDecls, final List<MethodInvocation> methodCallsToRefactor) {
final ASTBuilder b = ctx.getASTBuilder();
final Type substituteType = substituteType(b, originalInstanceCreation.getType(), originalInstanceCreation);
if (substituteType != null) {
ctx.getRefactorings().replace(originalInstanceCreation.getType(), substituteType);
originalInstanceCreation.setType(substituteType);
}
for (final MethodInvocation methodCall : methodCallsToRefactor) {
final MethodInvocation copyOfMethodCall = b.copySubtree(methodCall);
refactorMethod(b, methodCall, copyOfMethodCall);
ctx.getRefactorings().replace(methodCall, copyOfMethodCall);
}
for (final VariableDeclaration variableDecl : variableDecls) {
final VariableDeclarationStatement oldDeclareStmt = (VariableDeclarationStatement) variableDecl.getParent();
final Type substituteVarType = substituteType(b, oldDeclareStmt.getType(), (ASTNode) oldDeclareStmt.fragments().get(0));
if (substituteVarType != null) {
ctx.getRefactorings().replace(oldDeclareStmt.getType(), substituteVarType);
}
}
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class AbstractEnumCollectionReplacementRefactoring method handleAssignment.
private boolean handleAssignment(final ClassInstanceCreation node, final Assignment a) {
Expression lhs = a.getLeftHandSide();
if (isTargetType(lhs.resolveTypeBinding())) {
ITypeBinding[] typeArguments = lhs.resolveTypeBinding().getTypeArguments();
if (typeArguments.length > 0 && typeArguments[0].isEnum()) {
final TypeNameDecider typeNameDecider = new TypeNameDecider(lhs);
ASTBuilder b = ctx.getASTBuilder();
Type[] types = new Type[typeArguments.length];
for (int i = 0; i < types.length; i++) {
types[i] = b.toType(typeArguments[i], typeNameDecider);
}
return replace(node, types);
}
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class AbstractPrimitiveRatherThanWrapperRefactoring method refactorWrapper.
private void refactorWrapper(final VariableDeclarationStatement node) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Type primitiveType = b.type(getPrimitiveTypeName());
ctx.getRefactorings().replace(node.getType(), primitiveType);
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class AndroidViewHolderRefactoring method visit.
@Override
public boolean visit(MethodDeclaration node) {
Block body = node.getBody();
if (body != null && isMethod(node, "android.widget.Adapter", "getView", "int", "android.view.View", "android.view.ViewGroup")) {
final GetViewVariableVisitor visitor = new GetViewVariableVisitor();
body.accept(visitor);
if (visitor.canApplyRefactoring()) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
final TypeNameDecider typeNameDecider = new TypeNameDecider(visitor.viewVariableName);
// Transform tree
// Create If statement
final SingleVariableDeclaration viewArg = parameters(node).get(1);
final Variable convertViewVar = new Variable(viewArg.getName().getIdentifier(), b);
final InfixExpression condition = b.infixExpr(convertViewVar.varName(), EQUALS, b.null0());
final Block thenBlock = b.block();
final IfStatement ifStmt = b.if0(condition, thenBlock);
r.insertBefore(ifStmt, visitor.viewAssignmentStmt);
final List<Statement> thenStmts = statements(thenBlock);
thenStmts.add(b.toStmt(b.assign(convertViewVar.varName(), ASSIGN, b.copy(visitor.getInflateExpr()))));
// assign to local view variable when necessary
if (!"convertView".equals(visitor.viewVariableName.getIdentifier())) {
Statement assignConvertViewToView = null;
if (visitor.viewVariableDeclFragment != null) {
assignConvertViewToView = b.declareStmt(b.copyType(visitor.viewVariableName, typeNameDecider), b.copy(visitor.viewVariableName), convertViewVar.varName());
} else if (visitor.viewVariableAssignment != null) {
assignConvertViewToView = b.toStmt(b.assign(b.copy(visitor.viewVariableName), ASSIGN, convertViewVar.varName()));
}
if (assignConvertViewToView != null) {
r.insertBefore(assignConvertViewToView, visitor.viewAssignmentStmt);
}
}
// make sure method returns the view to be reused
if (visitor.returnStmt != null) {
r.insertAfter(b.return0(b.copy(visitor.viewVariableName)), visitor.returnStmt);
r.remove(visitor.returnStmt);
}
// Optimize findViewById calls
final FindViewByIdVisitor findViewByIdVisitor = new FindViewByIdVisitor(visitor.viewVariableName);
body.accept(findViewByIdVisitor);
if (!findViewByIdVisitor.items.isEmpty()) {
// TODO JNR name conflict? Use VariableNameDecider
Variable viewHolderItemVar = new Variable("ViewHolderItem", "viewHolderItem", b);
// create ViewHolderItem class
r.insertBefore(createViewHolderItemClass(findViewByIdVisitor, viewHolderItemVar.typeName(), typeNameDecider), node);
// declare viewhHolderItem object
r.insertFirst(body, Block.STATEMENTS_PROPERTY, viewHolderItemVar.declareStmt());
// initialize viewHolderItem
thenStmts.add(b.toStmt(b.assign(viewHolderItemVar.varName(), ASSIGN, b.new0(viewHolderItemVar.type()))));
// Assign findViewById to ViewHolderItem
for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
// ensure we are accessing convertView object
FieldAccess fieldAccess = b.fieldAccess(viewHolderItemVar.varName(), b.simpleName(item.variable.getIdentifier()));
// FIXME This does not work: not sure why??
// r.set(item.findViewByIdInvocation,
// MethodInvocation.EXPRESSION_PROPERTY, convertViewVar.varName());
item.findViewByIdInvocation.setExpression(convertViewVar.varName());
// FIXME For some reason b.copy() does not do what we would like
thenStmts.add(b.toStmt(b.assign(fieldAccess, ASSIGN, b.copySubtree(item.findViewByIdExpr))));
// replace previous findViewById with accesses to viewHolderItem
r.replace(item.findViewByIdExpr, b.copy(fieldAccess));
}
// store viewHolderItem in convertView
thenStmts.add(b.toStmt(b.invoke("convertView", "setTag", viewHolderItemVar.varName())));
// retrieve viewHolderItem from convertView
ifStmt.setElseStatement(b.block(b.toStmt(b.assign(viewHolderItemVar.varName(), ASSIGN, b.cast(viewHolderItemVar.type(), b.invoke("convertView", "getTag"))))));
}
r.remove(visitor.viewAssignmentStmt);
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
Aggregations