Search in sources :

Example 1 with VarDefinitionsUsesVisitor

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

the class BooleanCleanUp method noThenReturnStatement.

private boolean noThenReturnStatement(final IfStatement visited) {
    Assignment thenAssignment = ASTNodes.asExpression(visited.getThenStatement(), Assignment.class);
    if (ASTNodes.hasOperator(thenAssignment, Assignment.Operator.ASSIGN) && ASTNodes.asList(visited.getElseStatement()).isEmpty() && (thenAssignment.getLeftHandSide() instanceof Name || thenAssignment.getLeftHandSide() instanceof FieldAccess || thenAssignment.getLeftHandSide() instanceof SuperFieldAccess)) {
        Statement previousSibling = ASTNodes.getPreviousSibling(visited);
        if (previousSibling instanceof VariableDeclarationStatement) {
            VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement) previousSibling;
            VariableDeclarationFragment fragment = getVariableDeclarationFragment(variableDeclarationStatement, thenAssignment.getLeftHandSide());
            if (fragment != null) {
                VarDefinitionsUsesVisitor variableUseVisitor;
                try {
                    variableUseVisitor = new VarDefinitionsUsesVisitor(fragment.resolveBinding(), visited.getExpression(), true);
                } catch (Exception e) {
                    return true;
                }
                if (variableUseVisitor.getReads().isEmpty()) {
                    ITypeBinding typeBinding = variableDeclarationStatement.getType().resolveBinding();
                    return maybeReplace(visited, thenAssignment, typeBinding, fragment.getInitializer());
                }
            }
        } else if (previousSibling instanceof ExpressionStatement) {
            Assignment elseAssignment = ASTNodes.asExpression(previousSibling, Assignment.class);
            if (ASTNodes.hasOperator(elseAssignment, Assignment.Operator.ASSIGN) && ASTNodes.isSameVariable(thenAssignment.getLeftHandSide(), elseAssignment.getLeftHandSide())) {
                ITypeBinding typeBinding = elseAssignment.resolveTypeBinding();
                return maybeReplace(visited, thenAssignment, typeBinding, elseAssignment.getRightHandSide());
            }
        }
    }
    return true;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VarDefinitionsUsesVisitor(org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) 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) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Example 2 with VarDefinitionsUsesVisitor

use of org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor 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 3 with VarDefinitionsUsesVisitor

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

the class OptimizeRegExCleanUp method isRegEx.

private boolean isRegEx(ITypeBinding typeBinding, final IVariableBinding variableBinding, final int extraDimensions, final Expression initializer) {
    if (ASTNodes.hasType(typeBinding, String.class.getCanonicalName()) && extraDimensions == 0 && initializer != null) {
        VarDefinitionsUsesVisitor varOccurrencesVisitor = new VarDefinitionsUsesVisitor(variableBinding, initializer.getRoot(), true);
        List<SimpleName> reads = varOccurrencesVisitor.getReads();
        List<SimpleName> writes = varOccurrencesVisitor.getWrites();
        for (SimpleName write : writes) {
            if (write.getParent() instanceof Assignment && write.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY && !ASTNodes.hasOperator((Assignment) write.getParent(), Assignment.Operator.ASSIGN)) {
                return false;
            }
        }
        if (!reads.isEmpty()) {
            for (SimpleName read : reads) {
                if (!isRegEx(read)) {
                    return false;
                }
            }
            return true;
        }
    }
    return false;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VarDefinitionsUsesVisitor(org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName)

Example 4 with VarDefinitionsUsesVisitor

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

the class ObsoleteRemoveOverriddenAssignmentCleanUp method visit.

@Override
public boolean visit(final VariableDeclarationStatement visited) {
    VariableDeclarationFragment fragment = ASTNodes.getUniqueFragment(visited);
    if (fragment != null && fragment.getInitializer() != null && ASTNodes.isPassiveWithoutFallingThrough(fragment.getInitializer())) {
        SimpleName varName = fragment.getName();
        IVariableBinding variable = fragment.resolveBinding();
        Statement stmtToInspect = ASTNodes.getNextSibling(visited);
        boolean isOverridden = false;
        while (stmtToInspect != null) {
            if (!new VarDefinitionsUsesVisitor(variable, stmtToInspect, true).getReads().isEmpty()) {
                return true;
            }
            Assignment assignment = ASTNodes.asExpression(stmtToInspect, Assignment.class);
            if (assignment != null && ASTNodes.isSameVariable(varName, assignment.getLeftHandSide())) {
                if (!ASTNodes.hasOperator(assignment, Assignment.Operator.ASSIGN)) {
                    return true;
                }
                isOverridden = true;
                break;
            }
            stmtToInspect = ASTNodes.getNextSibling(stmtToInspect);
        }
        if (isOverridden) {
            TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRemoveOverriddenAssignmentCleanUp_description);
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            rewrite.remove(fragment.getInitializer(), group);
            return false;
        }
    }
    return true;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VarDefinitionsUsesVisitor(org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) Statement(org.eclipse.jdt.core.dom.Statement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 5 with VarDefinitionsUsesVisitor

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

the class DeclarationOutsideLoopRatherThanInsideCleanUp method isEffectivelyFinalRequired.

private boolean isEffectivelyFinalRequired(final VariableDeclarationStatement declaration, final VariableDeclarationFragment fragment) {
    if (Modifier.isFinal(declaration.getModifiers())) {
        return true;
    }
    VarDefinitionsUsesVisitor visitor = new VarDefinitionsUsesVisitor(fragment);
    List<SimpleName> reads = visitor.getReads();
    for (SimpleName read : reads) {
        ASTNode ancestor = ASTNodes.getFirstAncestorOrNull(read, AnonymousClassDeclaration.class, LambdaExpression.class);
        if (ancestor != null && !ASTNodes.isParent(fragment, ancestor)) {
            return true;
        }
    }
    return false;
}
Also used : VarDefinitionsUsesVisitor(org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

VarDefinitionsUsesVisitor (org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor)6 Assignment (org.eclipse.jdt.core.dom.Assignment)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)3 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 Statement (org.eclipse.jdt.core.dom.Statement)2 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 TextEditGroup (org.eclipse.text.edits.TextEditGroup)2 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)1 TypeNameDecider (org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)1 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)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 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)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