@CheckForNull private static ForLoopIncrement increment(ExpressionTree expression, Integer value) { if (expression.is(Tree.Kind.IDENTIFIER)) { return new ForLoopIncrement((IdentifierTree) expression, value); } return null; }
private void checkPositiveIncrement(Tree tree, ForLoopIncrement loopIncrement) { if (loopIncrement.value() < 0) { reportIssue(tree, String.format("\"%s\" is decremented and will never reach \"stop condition\".", loopIncrement.identifier().name())); } }
@CheckForNull public static ForLoopIncrement findInUpdates(ForStatementTree forStatement) { ForLoopIncrement result = null; List<StatementTree> updates = forStatement.update(); if (updates.size() == 1) { ExpressionStatementTree statement = (ExpressionStatementTree) updates.get(0); ExpressionTree expression = statement.expression(); if (expression.is(Tree.Kind.POSTFIX_INCREMENT, Tree.Kind.PREFIX_INCREMENT)) { UnaryExpressionTree unaryExp = (UnaryExpressionTree) expression; result = increment(unaryExp.expression(), 1); } else if (expression.is(Tree.Kind.POSTFIX_DECREMENT, Tree.Kind.PREFIX_DECREMENT)) { UnaryExpressionTree unaryExp = (UnaryExpressionTree) expression; result = increment(unaryExp.expression(), -1); } else if (expression.is(Tree.Kind.PLUS_ASSIGNMENT)) { AssignmentExpressionTree assignmentExp = (AssignmentExpressionTree) expression; result = increment(assignmentExp.variable(), intLiteralValue(assignmentExp.expression())); } else if (expression.is(Tree.Kind.MINUS_ASSIGNMENT)) { AssignmentExpressionTree assignmentExp = (AssignmentExpressionTree) expression; result = increment(assignmentExp.variable(), minus(intLiteralValue(assignmentExp.expression()))); } else if (expression.is(Tree.Kind.ASSIGNMENT)) { AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression; result = assignmentIncrement(assignment); } } return result; }
@CheckForNull public static ForLoopIncrement findInUpdates(ForStatementTree forStatement) { ForLoopIncrement result = null; List<StatementTree> updates = forStatement.update(); if (updates.size() == 1) { ExpressionStatementTree statement = (ExpressionStatementTree) updates.get(0); ExpressionTree expression = statement.expression(); if (expression.is(Tree.Kind.POSTFIX_INCREMENT, Tree.Kind.PREFIX_INCREMENT)) { UnaryExpressionTree unaryExp = (UnaryExpressionTree) expression; result = increment(unaryExp.expression(), 1); } else if (expression.is(Tree.Kind.POSTFIX_DECREMENT, Tree.Kind.PREFIX_DECREMENT)) { UnaryExpressionTree unaryExp = (UnaryExpressionTree) expression; result = increment(unaryExp.expression(), -1); } else if (expression.is(Tree.Kind.PLUS_ASSIGNMENT)) { AssignmentExpressionTree assignmentExp = (AssignmentExpressionTree) expression; result = increment(assignmentExp.variable(), intLiteralValue(assignmentExp.expression())); } else if (expression.is(Tree.Kind.MINUS_ASSIGNMENT)) { AssignmentExpressionTree assignmentExp = (AssignmentExpressionTree) expression; result = increment(assignmentExp.variable(), minus(intLiteralValue(assignmentExp.expression()))); } else if (expression.is(Tree.Kind.ASSIGNMENT)) { AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression; result = assignmentIncrement(assignment); } } return result; }
@CheckForNull public static ForLoopIncrement findInUpdates(ForStatementTree forStatement) { ForLoopIncrement result = null; List<StatementTree> updates = forStatement.update(); if (updates.size() == 1) { ExpressionStatementTree statement = (ExpressionStatementTree) updates.get(0); ExpressionTree expression = statement.expression(); if (expression.is(Tree.Kind.POSTFIX_INCREMENT, Tree.Kind.PREFIX_INCREMENT)) { UnaryExpressionTree unaryExp = (UnaryExpressionTree) expression; result = increment(unaryExp.expression(), 1); } else if (expression.is(Tree.Kind.POSTFIX_DECREMENT, Tree.Kind.PREFIX_DECREMENT)) { UnaryExpressionTree unaryExp = (UnaryExpressionTree) expression; result = increment(unaryExp.expression(), -1); } else if (expression.is(Tree.Kind.PLUS_ASSIGNMENT)) { AssignmentExpressionTree assignmentExp = (AssignmentExpressionTree) expression; result = increment(assignmentExp.variable(), intLiteralValue(assignmentExp.expression())); } else if (expression.is(Tree.Kind.MINUS_ASSIGNMENT)) { AssignmentExpressionTree assignmentExp = (AssignmentExpressionTree) expression; result = increment(assignmentExp.variable(), minus(intLiteralValue(assignmentExp.expression()))); } else if (expression.is(Tree.Kind.ASSIGNMENT)) { AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression; result = assignmentIncrement(assignment); } } return result; }
private void checkIncrementSign(ExpressionTree condition, ForLoopIncrement loopIncrement) { if (condition.is(Tree.Kind.GREATER_THAN, Tree.Kind.GREATER_THAN_OR_EQUAL_TO)) { BinaryExpressionTree binaryExp = (BinaryExpressionTree) condition; if (loopIncrement.hasSameIdentifier(binaryExp.leftOperand())) { checkNegativeIncrement(condition, loopIncrement); } else if (loopIncrement.hasSameIdentifier(binaryExp.rightOperand())) { checkPositiveIncrement(condition, loopIncrement); } } else if (condition.is(Tree.Kind.LESS_THAN, Tree.Kind.LESS_THAN_OR_EQUAL_TO)) { BinaryExpressionTree binaryExp = (BinaryExpressionTree) condition; if (loopIncrement.hasSameIdentifier(binaryExp.leftOperand())) { checkPositiveIncrement(condition, loopIncrement); } else if (loopIncrement.hasSameIdentifier(binaryExp.rightOperand())) { checkNegativeIncrement(condition, loopIncrement); } } }
private void checkIncrementSign(ExpressionTree condition, ForLoopIncrement loopIncrement) { if (condition.is(Tree.Kind.GREATER_THAN, Tree.Kind.GREATER_THAN_OR_EQUAL_TO)) { BinaryExpressionTree binaryExp = (BinaryExpressionTree) condition; if (loopIncrement.hasSameIdentifier(binaryExp.leftOperand())) { checkNegativeIncrement(condition, loopIncrement); } else if (loopIncrement.hasSameIdentifier(binaryExp.rightOperand())) { checkPositiveIncrement(condition, loopIncrement); } } else if (condition.is(Tree.Kind.LESS_THAN, Tree.Kind.LESS_THAN_OR_EQUAL_TO)) { BinaryExpressionTree binaryExp = (BinaryExpressionTree) condition; if (loopIncrement.hasSameIdentifier(binaryExp.leftOperand())) { checkPositiveIncrement(condition, loopIncrement); } else if (loopIncrement.hasSameIdentifier(binaryExp.rightOperand())) { checkNegativeIncrement(condition, loopIncrement); } } }
private void checkIncrementSign(ExpressionTree condition, ForLoopIncrement loopIncrement) { if (condition.is(Tree.Kind.GREATER_THAN, Tree.Kind.GREATER_THAN_OR_EQUAL_TO)) { BinaryExpressionTree binaryExp = (BinaryExpressionTree) condition; if (loopIncrement.hasSameIdentifier(binaryExp.leftOperand())) { checkNegativeIncrement(condition, loopIncrement); } else if (loopIncrement.hasSameIdentifier(binaryExp.rightOperand())) { checkPositiveIncrement(condition, loopIncrement); } } else if (condition.is(Tree.Kind.LESS_THAN, Tree.Kind.LESS_THAN_OR_EQUAL_TO)) { BinaryExpressionTree binaryExp = (BinaryExpressionTree) condition; if (loopIncrement.hasSameIdentifier(binaryExp.leftOperand())) { checkPositiveIncrement(condition, loopIncrement); } else if (loopIncrement.hasSameIdentifier(binaryExp.rightOperand())) { checkNegativeIncrement(condition, loopIncrement); } } }
private static ForLoopIncrement assignmentIncrement(AssignmentExpressionTree assignmentExpression) { ExpressionTree expression = assignmentExpression.expression(); ExpressionTree variable = assignmentExpression.variable(); if (variable.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.PLUS, Tree.Kind.MINUS)) { BinaryExpressionTree binaryExp = (BinaryExpressionTree) expression; Integer increment = intLiteralValue(binaryExp.rightOperand()); if (increment != null && isSameIdentifier((IdentifierTree) variable, binaryExp.leftOperand())) { increment = expression.is(Tree.Kind.MINUS) ? minus(increment) : increment; return increment(variable, increment); } return new ForLoopIncrement((IdentifierTree) variable, null); } return null; }
private static ForLoopIncrement assignmentIncrement(AssignmentExpressionTree assignmentExpression) { ExpressionTree expression = assignmentExpression.expression(); ExpressionTree variable = assignmentExpression.variable(); if (variable.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.PLUS, Tree.Kind.MINUS)) { BinaryExpressionTree binaryExp = (BinaryExpressionTree) expression; Integer increment = intLiteralValue(binaryExp.rightOperand()); if (increment != null && isSameIdentifier((IdentifierTree) variable, binaryExp.leftOperand())) { increment = expression.is(Tree.Kind.MINUS) ? minus(increment) : increment; return increment(variable, increment); } return new ForLoopIncrement((IdentifierTree) variable, null); } return null; }
private void checkIncrement(ForStatementTree forStatement, IdentifierTree loopIdentifier, boolean positiveIncrement) { if (forStatement.update().size() <= 1) { ForLoopIncrement loopIncrement = ForLoopIncrement.findInUpdates(forStatement); if (loopIncrement == null || !loopIncrement.hasSameIdentifier(loopIdentifier)) { addIssue(forStatement); } else if (loopIncrement.hasValue()) { int requiredIncrement = positiveIncrement ? 1 : -1; if (loopIncrement.value() != requiredIncrement || forBodyUpdatesLoopIdentifier(forStatement, loopIdentifier)) { addIssue(forStatement); } } } }
private void checkNegativeIncrement(Tree tree, ForLoopIncrement loopIncrement) { if (loopIncrement.value() > 0) { addIssue(tree, String.format("\"%s\" is incremented and will never reach \"stop condition\".", loopIncrement.identifier().name())); } }
private static ForLoopIncrement assignmentIncrement(AssignmentExpressionTree assignmentExpression) { ExpressionTree expression = assignmentExpression.expression(); ExpressionTree variable = assignmentExpression.variable(); if (variable.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.PLUS, Tree.Kind.MINUS)) { BinaryExpressionTree binaryExp = (BinaryExpressionTree) expression; Integer increment = intLiteralValue(binaryExp.rightOperand()); if (increment != null && isSameIdentifier((IdentifierTree) variable, binaryExp.leftOperand())) { increment = expression.is(Tree.Kind.MINUS) ? minus(increment) : increment; return increment(variable, increment); } return new ForLoopIncrement((IdentifierTree) variable, null); } return null; }
private void checkIncrement(ForStatementTree forStatement, IdentifierTree loopIdentifier, boolean positiveIncrement) { if (forStatement.update().size() <= 1) { ForLoopIncrement loopIncrement = ForLoopIncrement.findInUpdates(forStatement); if (loopIncrement == null || !loopIncrement.hasSameIdentifier(loopIdentifier)) { addIssue(forStatement); } else if (loopIncrement.hasValue()) { int requiredIncrement = positiveIncrement ? 1 : -1; if (loopIncrement.value() != requiredIncrement || forBodyUpdatesLoopIdentifier(forStatement, loopIdentifier)) { addIssue(forStatement); } } } }
@Override public void visitForStatement(ForStatementTree forStatement) { ExpressionTree condition = forStatement.condition(); ForLoopIncrement loopIncrement = ForLoopIncrement.findInUpdates(forStatement); if (condition == null || loopIncrement == null || !loopIncrement.hasValue()) { return; } checkIncrementSign(condition, loopIncrement); }
@Override public void visitForStatement(ForStatementTree forStatement) { ExpressionTree condition = forStatement.condition(); ForLoopIncrement loopIncrement = ForLoopIncrement.findInUpdates(forStatement); if (condition == null || loopIncrement == null || !loopIncrement.hasValue()) { return; } checkIncrementSign(condition, loopIncrement); }
private void checkNegativeIncrement(Tree tree, ForLoopIncrement loopIncrement) { if (loopIncrement.value() > 0) { reportIssue(tree, String.format("\"%s\" is incremented and will never reach \"stop condition\".", loopIncrement.identifier().name())); } }
@CheckForNull private static ForLoopIncrement increment(ExpressionTree expression, Integer value) { if (expression.is(Tree.Kind.IDENTIFIER)) { return new ForLoopIncrement((IdentifierTree) expression, value); } return null; }
@CheckForNull private static ForLoopIncrement increment(ExpressionTree expression, Integer value) { if (expression.is(Tree.Kind.IDENTIFIER)) { return new ForLoopIncrement((IdentifierTree) expression, value); } return null; }
private void checkPositiveIncrement(Tree tree, ForLoopIncrement loopIncrement) { if (loopIncrement.value() < 0) { reportIssue(tree, String.format("\"%s\" is decremented and will never reach \"stop condition\".", loopIncrement.identifier().name())); } }