Search in sources :

Example 56 with ASTBuilder

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));
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 57 with ASTBuilder

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);
        }
    }
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ASTHelper.hasType(org.autorefactor.refactoring.ASTHelper.hasType) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 58 with ASTBuilder

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;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ASTHelper.hasType(org.autorefactor.refactoring.ASTHelper.hasType) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) TypeNameDecider(org.autorefactor.refactoring.TypeNameDecider) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 59 with ASTBuilder

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);
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ASTHelper.hasType(org.autorefactor.refactoring.ASTHelper.hasType) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 60 with ASTBuilder

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;
}
Also used : Variable(org.autorefactor.refactoring.Variable) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) IfStatement(org.eclipse.jdt.core.dom.IfStatement) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Block(org.eclipse.jdt.core.dom.Block) TypeNameDecider(org.autorefactor.refactoring.TypeNameDecider) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Aggregations

ASTBuilder (org.autorefactor.refactoring.ASTBuilder)79 Expression (org.eclipse.jdt.core.dom.Expression)25 Refactorings (org.autorefactor.refactoring.Refactorings)23 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)19 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 IfStatement (org.eclipse.jdt.core.dom.IfStatement)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)9 Statement (org.eclipse.jdt.core.dom.Statement)9 Type (org.eclipse.jdt.core.dom.Type)9 Block (org.eclipse.jdt.core.dom.Block)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 ArrayList (java.util.ArrayList)5 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)5 ASTHelper.hasType (org.autorefactor.refactoring.ASTHelper.hasType)4 NotImplementedException (org.autorefactor.util.NotImplementedException)4 ASTMatcher (org.eclipse.jdt.core.dom.ASTMatcher)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)4 ForStatement (org.eclipse.jdt.core.dom.ForStatement)4 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)4