Search in sources :

Example 1 with TypeNameDecider

use of org.autorefactor.jdt.internal.corext.dom.TypeNameDecider in project AutoRefactor by JnRouvignac.

the class AndroidViewHolderCleanUp method visit.

@Override
public boolean visit(final MethodDeclaration node) {
    Block body = node.getBody();
    if (body != null && // $NON-NLS-1$//$NON-NLS-2$
    ASTNodes.usesGivenSignature(// $NON-NLS-1$//$NON-NLS-2$
    node, // $NON-NLS-1$//$NON-NLS-2$
    "android.widget.Adapter", // $NON-NLS-1$//$NON-NLS-2$
    "getView", // $NON-NLS-1$
    int.class.getSimpleName(), // $NON-NLS-1$
    "android.view.View", "android.view.ViewGroup")) {
        // $NON-NLS-1$
        GetViewVariableVisitor visitor = new GetViewVariableVisitor();
        body.accept(visitor);
        if (visitor.canApplyRefactoring()) {
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            ASTNodeFactory ast = cuRewrite.getASTBuilder();
            TextEditGroup group = new TextEditGroup(MultiFixMessages.AndroidViewHolderCleanUp_description);
            TypeNameDecider typeNameDecider = new TypeNameDecider(visitor.viewVariableName);
            // Transform tree
            // Create If statement
            SingleVariableDeclaration viewArg = (SingleVariableDeclaration) node.parameters().get(1);
            Variable convertViewVar = new Variable(viewArg.getName().getIdentifier(), ast);
            InfixExpression newInfixExpression = ast.newInfixExpression();
            newInfixExpression.setLeftOperand(convertViewVar.varName());
            newInfixExpression.setOperator(InfixExpression.Operator.EQUALS);
            newInfixExpression.setRightOperand(ast.newNullLiteral());
            InfixExpression condition = newInfixExpression;
            Block thenBlock = ast.newBlock();
            IfStatement newIfStatement = ast.newIfStatement();
            newIfStatement.setExpression(condition);
            newIfStatement.setThenStatement(thenBlock);
            rewrite.insertBefore(newIfStatement, visitor.viewAssignmentStatement, group);
            List<Statement> thenStatements = thenBlock.statements();
            thenStatements.add(ast.newExpressionStatement(ast.newAssignment(convertViewVar.varName(), Assignment.Operator.ASSIGN, ast.createCopyTarget(visitor.getInflateExpression()))));
            // Assign to local view variable when necessary
            if (!"convertView".equals(visitor.viewVariableName.getIdentifier())) {
                // $NON-NLS-1$
                Statement assignConvertViewToView = null;
                if (visitor.viewVariableDeclFragment != null) {
                    assignConvertViewToView = ast.declareStatement(ast.copyType(visitor.viewVariableName, typeNameDecider), ast.createCopyTarget(visitor.viewVariableName), convertViewVar.varName());
                } else if (visitor.viewVariableAssignment != null) {
                    assignConvertViewToView = ast.newExpressionStatement(ast.newAssignment(ast.createCopyTarget(visitor.viewVariableName), Assignment.Operator.ASSIGN, convertViewVar.varName()));
                }
                if (assignConvertViewToView != null) {
                    rewrite.insertBefore(assignConvertViewToView, visitor.viewAssignmentStatement, group);
                }
            }
            // Make sure method returns the view to be reused
            if (visitor.returnStatement != null) {
                rewrite.insertAfter(ast.newReturnStatement(ast.createCopyTarget(visitor.viewVariableName)), visitor.returnStatement, group);
                rewrite.remove(visitor.returnStatement, group);
            }
            // Optimize findViewById calls
            FindViewByIdVisitor findViewByIdVisitor = new FindViewByIdVisitor(visitor.viewVariableName);
            body.accept(findViewByIdVisitor);
            if (!findViewByIdVisitor.items.isEmpty()) {
                // TODO JNR name conflict? Use VariableNameDecider
                // $NON-NLS-1$ //$NON-NLS-2$
                Variable viewHolderItemVar = new Variable("ViewHolderItem", "viewHolderItem", ast);
                // Create ViewHolderItem class
                rewrite.insertBefore(createViewHolderItemClass(findViewByIdVisitor, viewHolderItemVar.typeName(), typeNameDecider), node, group);
                // Declare viewhHolderItem object
                rewrite.insertFirst(body, Block.STATEMENTS_PROPERTY, viewHolderItemVar.declareStatement(), group);
                // Initialize viewHolderItem
                thenStatements.add(ast.newExpressionStatement(ast.newAssignment(viewHolderItemVar.varName(), Assignment.Operator.ASSIGN, ast.newClassInstanceCreation(viewHolderItemVar.type()))));
                // Assign findViewById to ViewHolderItem
                for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
                    // Ensure we are accessing convertView object
                    FieldAccess fieldAccess = ast.newFieldAccess(viewHolderItemVar.varName(), ast.newSimpleName(item.variable.getIdentifier()));
                    // FIXME This does not work: not sure why??
                    // rewrite.set(item.findViewByIdInvocation,
                    // MethodInvocation.EXPRESSION_PROPERTY, convertViewVar.varName());
                    item.findViewByIdInvocation.setExpression(convertViewVar.varName());
                    // FIXME For some reason ast.copy() does not do what we would like
                    thenStatements.add(ast.newExpressionStatement(ast.newAssignment(fieldAccess, Assignment.Operator.ASSIGN, ast.copySubtree(item.findViewByIdExpression))));
                    // Replace previous findViewById with accesses to viewHolderItem
                    rewrite.replace(item.findViewByIdExpression, ast.createCopyTarget(fieldAccess), group);
                }
                MethodInvocation setTagMethod = ast.newMethodInvocation();
                // $NON-NLS-1$
                setTagMethod.setExpression(ASTNodeFactory.newName(ast, "convertView"));
                // $NON-NLS-1$
                setTagMethod.setName(ast.newSimpleName("setTag"));
                setTagMethod.arguments().add(viewHolderItemVar.varName());
                // Store viewHolderItem in convertView
                thenStatements.add(ast.newExpressionStatement(setTagMethod));
                Block newBlock = ast.newBlock();
                MethodInvocation getTagMethod = ast.newMethodInvocation();
                // $NON-NLS-1$
                getTagMethod.setExpression(ASTNodeFactory.newName(ast, "convertView"));
                // $NON-NLS-1$
                getTagMethod.setName(ast.newSimpleName("getTag"));
                newBlock.statements().add(ast.newExpressionStatement(ast.newAssignment(viewHolderItemVar.varName(), Assignment.Operator.ASSIGN, ast.newCastExpression(viewHolderItemVar.type(), getTagMethod))));
                // Retrieve viewHolderItem from convertView
                newIfStatement.setElseStatement(newBlock);
            }
            rewrite.remove(visitor.viewAssignmentStatement, group);
            return false;
        }
    }
    return true;
}
Also used : Variable(org.autorefactor.jdt.internal.corext.dom.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) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Block(org.eclipse.jdt.core.dom.Block) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 2 with TypeNameDecider

use of org.autorefactor.jdt.internal.corext.dom.TypeNameDecider in project AutoRefactor by JnRouvignac.

the class AbstractEnumCollectionReplacementCleanUp method handleAssignment.

private boolean handleAssignment(final ClassInstanceCreation node, final Assignment a, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    Expression leftHandSide = a.getLeftHandSide();
    if (isTargetType(leftHandSide.resolveTypeBinding())) {
        ITypeBinding[] typeArguments = leftHandSide.resolveTypeBinding().getTypeArguments();
        if (typeArguments.length > 0 && typeArguments[0].isEnum()) {
            TypeNameDecider typeNameDecider = new TypeNameDecider(leftHandSide);
            ASTNodeFactory ast = cuRewrite.getASTBuilder();
            Type[] types = new Type[typeArguments.length];
            for (int i = 0; i < types.length; i++) {
                types[i] = ast.toType(typeArguments[i], typeNameDecider);
            }
            return maybeReplace(node, classesToUseWithImport, importsToAdd, types);
        }
    }
    return true;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)

Example 3 with TypeNameDecider

use of org.autorefactor.jdt.internal.corext.dom.TypeNameDecider in project AutoRefactor by JnRouvignac.

the class SetRatherThanMapCleanUp method substituteType.

@Override
protected Type substituteType(final Type origType, final ASTNode originalExpression, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    ITypeBinding origTypeBinding = origType.resolveBinding();
    if (origTypeBinding == null) {
        return null;
    }
    String substitutingType = getSubstitutingClassName(origTypeBinding.getErasure().getQualifiedName());
    if (classesToUseWithImport.contains(substitutingType)) {
        importsToAdd.add(substitutingType);
        substitutingType = getSimpleName(substitutingType);
    }
    TypeNameDecider typeNameDecider = new TypeNameDecider(originalExpression);
    if (origTypeBinding.isParameterizedType()) {
        ITypeBinding[] origTypeArgs = origTypeBinding.getTypeArguments();
        Type[] newTypes;
        if (origTypeArgs.length > 0 && !((ParameterizedType) origType).typeArguments().isEmpty()) {
            newTypes = new Type[1];
            newTypes[0] = ast.toType(origTypeArgs[0], typeNameDecider);
        } else {
            newTypes = new Type[0];
        }
        return ast.newParameterizedType(substitutingType, newTypes);
    }
    return ast.type(substitutingType);
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)

Example 4 with TypeNameDecider

use of org.autorefactor.jdt.internal.corext.dom.TypeNameDecider in project AutoRefactor by JnRouvignac.

the class EntrySetRatherThanKeySetAndValueSearchCleanUp method replaceEntryIterationByKeyIteration.

private void replaceEntryIterationByKeyIteration(final EnhancedForStatement enhancedFor, final Expression mapExpression, final SingleVariableDeclaration parameter, final List<MethodInvocation> getValueMis) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.EntrySetRatherThanKeySetAndValueSearchCleanUp_description);
    VarDefinitionsUsesVisitor keyUseVisitor = new VarDefinitionsUsesVisitor(parameter.resolveBinding(), enhancedFor.getBody(), true);
    int keyUses = keyUseVisitor.getReads().size();
    int insertionPoint = ASTNodes.asList(enhancedFor.getBody()).get(0).getStartPosition() - 1;
    // $NON-NLS-1$ //$NON-NLS-2$
    String entryVar = new VariableNameDecider(enhancedFor.getBody(), insertionPoint).suggest("entry", "mapEntry");
    TypeNameDecider typeNameDecider = new TypeNameDecider(parameter);
    MethodInvocation getValueMi0 = getValueMis.get(0);
    ITypeBinding typeBinding = getValueMi0.getExpression().resolveTypeBinding();
    MethodInvocation entrySetMethod = ast.newMethodInvocation();
    entrySetMethod.setExpression(ASTNodes.createMoveTarget(rewrite, mapExpression));
    // $NON-NLS-1$
    entrySetMethod.setName(ast.newSimpleName("entrySet"));
    rewrite.set(enhancedFor, EnhancedForStatement.EXPRESSION_PROPERTY, entrySetMethod, group);
    MethodInvocation getKeyMethod = ast.newMethodInvocation();
    getKeyMethod.setExpression(ast.newSimpleName(entryVar));
    // $NON-NLS-1$
    getKeyMethod.setName(ast.newSimpleName("getKey"));
    if (typeBinding != null && typeBinding.isRawType()) {
        // for (Object key : map.keySet()) => for (Object key : map.entrySet())
        refactorRawMap(enhancedFor, parameter, getValueMis, rewrite, ast, group, keyUses, insertionPoint, entryVar, typeNameDecider, getKeyMethod);
    } else {
        // for (K key : map.keySet()) => for (K key : map.entrySet())
        // for (K key : map.entrySet()) => for (Map.Entry<K, V> mapEntry :
        // map.entrySet())
        refactorGenericMap(enhancedFor, parameter, getValueMis, rewrite, ast, group, keyUses, entryVar, typeNameDecider, getValueMi0, getKeyMethod);
    }
    // Replace all occurrences of map.get(key) => mapEntry.getValue()
    for (MethodInvocation getValueMi : getValueMis) {
        MethodInvocation getValueMethod = ast.newMethodInvocation();
        getValueMethod.setExpression(ast.newSimpleName(entryVar));
        // $NON-NLS-1$
        getValueMethod.setName(ast.newSimpleName("getValue"));
        MethodInvocation newMethodInvocation = getValueMethod;
        rewrite.replace(getValueMi, newMethodInvocation, group);
    }
}
Also used : VarDefinitionsUsesVisitor(org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 5 with TypeNameDecider

use of org.autorefactor.jdt.internal.corext.dom.TypeNameDecider in project AutoRefactor by JnRouvignac.

the class ObsoleteGenericMapRatherThanRawMapCleanUp method substituteType.

@Override
protected Type substituteType(final Type origType, final ASTNode originalExpression, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    if (origType.isParameterizedType()) {
        return null;
    }
    TypeNameDecider typeNameDecider = new TypeNameDecider(originalExpression);
    ParameterizedType parameterizedType = ast.getAST().newParameterizedType(ast.createCopyTarget(origType));
    List<Type> typeArgs = parameterizedType.typeArguments();
    typeArgs.clear();
    typeArgs.add(ast.toType(keyType, typeNameDecider));
    typeArgs.add(ast.toType(valueType, typeNameDecider));
    return parameterizedType;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)

Aggregations

TypeNameDecider (org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)12 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)11 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)5 Type (org.eclipse.jdt.core.dom.Type)4 TextEditGroup (org.eclipse.text.edits.TextEditGroup)4 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)2 TypeMethodReference (org.eclipse.jdt.core.dom.TypeMethodReference)2 Comparator (java.util.Comparator)1 VarDefinitionsUsesVisitor (org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor)1 Variable (org.autorefactor.jdt.internal.corext.dom.Variable)1 Block (org.eclipse.jdt.core.dom.Block)1 CreationReference (org.eclipse.jdt.core.dom.CreationReference)1 Expression (org.eclipse.jdt.core.dom.Expression)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)1 IfStatement (org.eclipse.jdt.core.dom.IfStatement)1 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)1 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)1