private static ExpressionTree getNonNullOperand(ExpressionTree expressionTree) { BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) ExpressionUtils.skipParentheses(expressionTree); if (binaryExpressionTree.leftOperand().is(Tree.Kind.NULL_LITERAL)) { return binaryExpressionTree.rightOperand(); } return binaryExpressionTree.leftOperand(); }
private static boolean isBiggerTreeWithSameTruthiness(@Nullable Tree parent, Tree child, boolean isTrue) { if (parent == null) { return false; } if (parent.is(Tree.Kind.PARENTHESIZED_EXPRESSION)) { return true; } Tree.Kind operator = isTrue ? Tree.Kind.CONDITIONAL_OR : Tree.Kind.CONDITIONAL_AND; return parent.is(operator) && ((BinaryExpressionTree) parent).leftOperand() == child; } }
private void buildBinaryExpression(Tree tree) { BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree; currentBlock.elements.add(tree); build(binaryExpressionTree.rightOperand()); build(binaryExpressionTree.leftOperand()); }
private Stream<BinaryExpressionTree> flattenLogicalExpression(ExpressionTree expression) { if (expression.is(CONDITIONAL_AND, CONDITIONAL_OR)) { ignored.add(expression); BinaryExpressionTree binaryExpr = (BinaryExpressionTree) expression; ExpressionTree left = ExpressionUtils.skipParentheses(binaryExpr.leftOperand()); ExpressionTree right = ExpressionUtils.skipParentheses(binaryExpr.rightOperand()); return Stream.concat(Stream.concat(flattenLogicalExpression(left), Stream.of(binaryExpr)), flattenLogicalExpression(right)); } return Stream.empty(); }
private static boolean isLeftShiftOnOne(BinaryExpressionTree tree) { // 1 << 1 is used for bit masks construction and should be excluded. if (tree.is(Tree.Kind.LEFT_SHIFT) && tree.leftOperand().is(Tree.Kind.INT_LITERAL) && tree.rightOperand().is(Tree.Kind.INT_LITERAL)) { String left = ((LiteralTree) tree.leftOperand()).value(); String right = ((LiteralTree) tree.rightOperand()).value(); if ("1".equals(right) && "1".equals(left)) { return true; } } return false; }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { hasBinaryExpression = true; if (!isConstant(tree.rightOperand())) { tree.rightOperand().accept(this); } if (!isConstant(tree.leftOperand())) { tree.leftOperand().accept(this); } }
public ConditionType(ExpressionTree condition, UpdatesCollector collector) { if (condition.is(Tree.Kind.LESS_THAN, Tree.Kind.LESS_THAN_OR_EQUAL_TO)) { matched = canBeMatched(((BinaryExpressionTree) condition).leftOperand(), ((BinaryExpressionTree) condition).rightOperand(), collector); } else if (condition.is(Tree.Kind.GREATER_THAN, Tree.Kind.GREATER_THAN_OR_EQUAL_TO)) { matched = canBeMatched(((BinaryExpressionTree) condition).rightOperand(), ((BinaryExpressionTree) condition).leftOperand(), collector); } else { matched = true; } }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { tree.leftOperand().accept(this); tree.rightOperand().accept(this); }
@Override public void visitNode(Tree tree) { BinaryExpressionTree binaryTree = (BinaryExpressionTree) tree; if (tree.is(Tree.Kind.GREATER_THAN)) { checkForIssue(tree, binaryTree.leftOperand(), LiteralUtils.longLiteralValue(binaryTree.rightOperand())); } else { checkForIssue(tree, binaryTree.rightOperand(), LiteralUtils.longLiteralValue(binaryTree.leftOperand())); } }
private void buildConditionalAnd(BinaryExpressionTree conditionalAnd, Block trueBlock, Block falseBlock) { buildCondition(conditionalAnd.rightOperand(), trueBlock, falseBlock); Block newTrueBlock = currentBlock; // process LHS currentBlock = createBranch(conditionalAnd, newTrueBlock, falseBlock); buildCondition(conditionalAnd.leftOperand(), newTrueBlock, falseBlock); }
private void buildConditionalOr(BinaryExpressionTree conditionalOr, Block trueBlock, Block falseBlock) { // process RHS buildCondition(conditionalOr.rightOperand(), trueBlock, falseBlock); Block newFalseBlock = currentBlock; // process LHS currentBlock = createBranch(conditionalOr, trueBlock, newFalseBlock); buildCondition(conditionalOr.leftOperand(), trueBlock, newFalseBlock); }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { tree.leftOperand().accept(this); tree.rightOperand().accept(this); registerBinaryExpressionType(tree); }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { if (tree.is(Tree.Kind.PLUS)) { checkPathDelimiter(tree.leftOperand()); checkPathDelimiter(tree.rightOperand()); } super.visitBinaryExpression(tree); }
private static Integer handleStringConcatenation(BinaryExpressionTree expr) { Integer left = getNumberQuery(expr.leftOperand()); Integer right = getNumberQuery(expr.rightOperand()); return (left == null && right == null) ? null : (zeroIfNull(left) + zeroIfNull(right)); }
private static boolean isNullComparison(ExpressionTree expressionTree, Tree.Kind comparatorKind) { ExpressionTree tree = ExpressionUtils.skipParentheses(expressionTree); if (tree.is(comparatorKind)) { BinaryExpressionTree binary = (BinaryExpressionTree) tree; return binary.leftOperand().is(Tree.Kind.NULL_LITERAL) || binary.rightOperand().is(Tree.Kind.NULL_LITERAL); } return false; }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { if (isRelationalExpression(tree)) { visitInnerExpression(tree.leftOperand()); visitInnerExpression(tree.rightOperand()); } else { super.visitBinaryExpression(tree); } }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { if (tree.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) { checkOperand(tree.leftOperand()); checkOperand(tree.rightOperand()); } super.visitBinaryExpression(tree); }
private static boolean isNullCheck(ExpressionTree condition) { if (condition.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) { BinaryExpressionTree bet = (BinaryExpressionTree) condition; ExpressionTree rightOperand = ExpressionUtils.skipParentheses(bet.rightOperand()); ExpressionTree leftOperand = ExpressionUtils.skipParentheses(bet.leftOperand()); return rightOperand.is(Tree.Kind.NULL_LITERAL) || leftOperand.is(Tree.Kind.NULL_LITERAL); } return false; }
@Override public void visitNode(Tree tree) { ExpressionTree leftOperand = ExpressionUtils.skipParentheses(((BinaryExpressionTree) tree).leftOperand()); ExpressionTree rightOperand = ExpressionUtils.skipParentheses(((BinaryExpressionTree) tree).rightOperand()); if (leftOperand.is(Tree.Kind.METHOD_INVOCATION)) { checkIndexOfInvocation((MethodInvocationTree) leftOperand, rightOperand); } else if (rightOperand.is(Tree.Kind.METHOD_INVOCATION)) { checkIndexOfInvocation((MethodInvocationTree) rightOperand, leftOperand); } }
@Override protected void checkEqualityExpression(BinaryExpressionTree tree) { Type leftOpType = tree.leftOperand().symbolType(); Type rightOpType = tree.rightOperand().symbolType(); if (!isNullComparison(leftOpType, rightOpType) && (isStringType(leftOpType, rightOpType) || isBoxedType(leftOpType, rightOpType))) { reportIssue(tree.operatorToken()); } }