@Override public void visitBinaryExpression(BinaryExpressionTree tree) { if (tree.is(Kind.PLUS)) { checkConcatenation(tree, tree.leftOperand(), tree.rightOperand()); } super.visitBinaryExpression(tree); }
private static boolean isNanTest(BinaryExpressionTree tree) { Type leftOperandType = tree.leftOperand().symbolType(); if (tree.is(Tree.Kind.NOT_EQUAL_TO) && (leftOperandType.isPrimitive(Type.Primitives.FLOAT) || leftOperandType.isPrimitive(Type.Primitives.DOUBLE))) { return true; } return false; }
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 visitNode(Tree tree) { BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree; if (binaryExpressionTree.is(Tree.Kind.CONDITIONAL_AND, Tree.Kind.CONDITIONAL_OR) && isIndirectEquality(binaryExpressionTree)) { binaryExpressionTree = (BinaryExpressionTree) binaryExpressionTree.leftOperand(); } if ((hasFloatingType(binaryExpressionTree.leftOperand()) || hasFloatingType(binaryExpressionTree.rightOperand())) && !isNanTest(binaryExpressionTree)) { addIssue( binaryExpressionTree, "Equality tests should not be made with floating point values."); } }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { super.visitBinaryExpression(tree); if (tree.is(Tree.Kind.EQUAL_TO)) { Type leftOperandType = ((JavaTree.AbstractExpressionTree) tree.leftOperand()).getType(); Type rightOperandType = ((JavaTree.AbstractExpressionTree) tree.rightOperand()).getType(); if (leftOperandType == null || rightOperandType == null) { // FIXME null type should not happen. return; } if (!isNullComparison(leftOperandType, rightOperandType) && (isClass(leftOperandType) || isClass(rightOperandType))) { context.addIssue(tree, ruleKey, "Change this comparison to use the equals method."); } } }
private static boolean isIndirectEquality( BinaryExpressionTree binaryExpressionTree, Tree.Kind indirectOperator, Tree.Kind comparator1, Tree.Kind comparator2) { if (binaryExpressionTree.is(indirectOperator) && binaryExpressionTree.leftOperand().is(comparator1, comparator2)) { BinaryExpressionTree leftOp = (BinaryExpressionTree) binaryExpressionTree.leftOperand(); if (binaryExpressionTree.rightOperand().is(comparator1, comparator2)) { BinaryExpressionTree rightOp = (BinaryExpressionTree) binaryExpressionTree.rightOperand(); if (((JavaTree) leftOp).getKind().equals(((JavaTree) rightOp).getKind())) { // same operator return SyntacticEquivalence.areEquivalent(leftOp.leftOperand(), rightOp.rightOperand()) && SyntacticEquivalence.areEquivalent(leftOp.rightOperand(), rightOp.leftOperand()); } else { // different operator return SyntacticEquivalence.areEquivalent(leftOp.leftOperand(), rightOp.leftOperand()) && SyntacticEquivalence.areEquivalent(leftOp.rightOperand(), rightOp.rightOperand()); } } } return false; }
private static boolean isSecuringByte(BinaryExpressionTree tree) { return tree.is(Tree.Kind.AND) && (is0xff(tree.rightOperand()) || is0xff(tree.leftOperand())); }
private static boolean isShifting(BinaryExpressionTree tree) { return tree.is(Tree.Kind.LEFT_SHIFT, Tree.Kind.RIGHT_SHIFT, Tree.Kind.UNSIGNED_RIGHT_SHIFT); }