Search in sources :

Example 6 with VarDefinitionsUsesVisitor

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

the class DoWhileRatherThanWhileCleanUp method peremptoryValue.

private Object peremptoryValue(final ASTNode visited, final Expression condition) {
    Object constantCondition = condition.resolveConstantExpressionValue();
    if (constantCondition != null) {
        return constantCondition;
    }
    Long integerLiteral = ASTNodes.getIntegerLiteral(condition);
    if (integerLiteral != null) {
        return integerLiteral;
    }
    SimpleName variable = ASTNodes.as(condition, SimpleName.class);
    if (variable != null && variable.resolveBinding() != null && variable.resolveBinding().getKind() == IBinding.VARIABLE) {
        List<ASTNode> precedingStatements = getPrecedingCode(visited);
        Collections.reverse(precedingStatements);
        for (ASTNode precedingStatement : precedingStatements) {
            if (isConditionalCode(precedingStatement)) {
                return null;
            }
            VarDefinitionsUsesVisitor visitor = new VarDefinitionsUsesVisitor((IVariableBinding) variable.resolveBinding(), precedingStatement, true);
            if (visitor.getWrites().size() > 1) {
                return null;
            }
            for (SimpleName astNode : visitor.getReads()) {
                ASTNode parent = astNode.getParent();
                while (parent instanceof ParenthesizedExpression) {
                    parent = astNode.getParent();
                }
                if (parent instanceof PrefixExpression && ASTNodes.hasOperator((PrefixExpression) parent, PrefixExpression.Operator.INCREMENT, PrefixExpression.Operator.DECREMENT) || parent instanceof PostfixExpression && ASTNodes.hasOperator((PostfixExpression) parent, PostfixExpression.Operator.INCREMENT, PostfixExpression.Operator.DECREMENT)) {
                    return null;
                }
            }
            if (!visitor.getWrites().isEmpty()) {
                SimpleName write = visitor.getWrites().get(0);
                ASTNode parent = write;
                while (parent != precedingStatement) {
                    if (isConditionalCode(parent)) {
                        return null;
                    }
                    parent = parent.getParent();
                }
                switch(write.getParent().getNodeType()) {
                    case ASTNode.ASSIGNMENT:
                        Assignment assignment = (Assignment) write.getParent();
                        if (ASTNodes.hasOperator(assignment, Assignment.Operator.ASSIGN)) {
                            return peremptoryValue(precedingStatement, assignment.getRightHandSide());
                        }
                        break;
                    case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
                        VariableDeclarationFragment fragment = (VariableDeclarationFragment) write.getParent();
                        if (fragment.getInitializer() != null) {
                            return peremptoryValue(precedingStatement, fragment.getInitializer());
                        }
                        break;
                    case ASTNode.SINGLE_VARIABLE_DECLARATION:
                        SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration) write.getParent();
                        if (singleVariableDeclaration.getInitializer() != null) {
                            return peremptoryValue(precedingStatement, singleVariableDeclaration.getInitializer());
                        }
                        break;
                    default:
                        break;
                }
                return null;
            }
        }
        return null;
    }
    InfixExpression infixExpression = ASTNodes.as(condition, InfixExpression.class);
    if (infixExpression != null) {
        if (!infixExpression.hasExtendedOperands() && ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS, InfixExpression.Operator.GREATER, InfixExpression.Operator.GREATER_EQUALS, InfixExpression.Operator.LESS, InfixExpression.Operator.LESS_EQUALS)) {
            Object leftOperand = peremptoryValue(visited, infixExpression.getLeftOperand());
            Object rightOperand = peremptoryValue(visited, infixExpression.getRightOperand());
            if (leftOperand instanceof Number && rightOperand instanceof Number) {
                Number leftNumber = (Number) leftOperand;
                Number rightNumber = (Number) rightOperand;
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.EQUALS)) {
                    return leftNumber.longValue() == rightNumber.longValue();
                }
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.NOT_EQUALS)) {
                    return leftNumber.longValue() != rightNumber.longValue();
                }
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.GREATER)) {
                    return leftNumber.longValue() > rightNumber.longValue();
                }
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.GREATER_EQUALS)) {
                    return leftNumber.longValue() >= rightNumber.longValue();
                }
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.LESS)) {
                    return leftNumber.longValue() < rightNumber.longValue();
                }
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.LESS_EQUALS)) {
                    return leftNumber.longValue() <= rightNumber.longValue();
                }
            }
            return null;
        }
        if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND)) {
            for (Expression operand : ASTNodes.allOperands(infixExpression)) {
                final Object hasAlwaysValue = peremptoryValue(visited, operand);
                if (!Boolean.TRUE.equals(hasAlwaysValue)) {
                    return hasAlwaysValue;
                }
            }
            return Boolean.TRUE;
        }
        if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.OR)) {
            for (Expression operand : ASTNodes.allOperands(infixExpression)) {
                final Object hasAlwaysValue = peremptoryValue(visited, operand);
                if (!Boolean.FALSE.equals(hasAlwaysValue)) {
                    return hasAlwaysValue;
                }
            }
            return Boolean.FALSE;
        }
    }
    return false;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Assignment(org.eclipse.jdt.core.dom.Assignment) VarDefinitionsUsesVisitor(org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression)

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