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;
}
Aggregations