@Override
 public void visitBinaryExpression(BinaryExpressionTree tree) {
   if (tree.is(Kind.PLUS)) {
     checkConcatenation(tree, tree.leftOperand(), tree.rightOperand());
   }
   super.visitBinaryExpression(tree);
 }
 public static boolean hasEquivalentOperand(BinaryExpressionTree tree) {
   if (isNanTest(tree) || isLeftShiftOnOne(tree)) {
     return false;
   }
   Tree.Kind binaryKind = tree.kind();
   return areOperandEquivalent(tree.leftOperand(), tree.rightOperand(), binaryKind);
 }
Exemple #3
0
 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 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;
 }
Exemple #5
0
 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);
 }
Exemple #6
0
 private void buildConditionalOr(BinaryExpressionTree tree) {
   // process RHS
   Block trueBlock = currentBlock;
   currentBlock = createBlock(trueBlock);
   build(tree.rightOperand());
   Block falseBlock = currentBlock;
   // process LHS
   currentBlock = createBranch(tree, trueBlock, falseBlock);
   build(tree.leftOperand());
 }
 @Override
 public void visitNode(Tree tree) {
   BinaryExpressionTree bet = (BinaryExpressionTree) tree;
   if (bet.leftOperand().is(Tree.Kind.STRING_LITERAL)
       || bet.rightOperand().is(Tree.Kind.STRING_LITERAL)) {
     addIssue(
         tree,
         "Replace \"==\" and \"!=\" by \"equals()\" and \"!equals()\" respectively to compare these strings.");
   }
 }
 @Override
 public void visitNode(Tree tree) {
   BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
   if (hasEquivalentOperand(binaryExpressionTree)) {
     addIssue(
         binaryExpressionTree.rightOperand(),
         "Identical sub-expressions on both sides of operator \""
             + binaryExpressionTree.operatorToken().text()
             + "\"");
   }
 }
 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.");
     }
   }
 }
 @Override
 public void visitBinaryExpression(BinaryExpressionTree tree) {
   super.visitBinaryExpression(tree);
   if (isShifting(tree)) {
     shifts.add(tree);
     return;
   }
   if (isSecuringByte(tree)) {
     byteContainments.add(tree);
     return;
   }
   if (isIntegerOrLongExpected(tree.symbolType())) {
     ExpressionTree leftOperand = ExpressionUtils.skipParentheses(tree.leftOperand());
     ExpressionTree rightOperand = ExpressionUtils.skipParentheses(tree.rightOperand());
     checkShiftWithoutByteSecuring(leftOperand, rightOperand);
     checkShiftWithoutByteSecuring(rightOperand, leftOperand);
   }
 }
 private static boolean isNanTest(BinaryExpressionTree binaryExpressionTree) {
   return SyntacticEquivalence.areEquivalent(
       binaryExpressionTree.leftOperand(), binaryExpressionTree.rightOperand());
 }
 private static boolean isShifting(BinaryExpressionTree tree) {
   return tree.is(Tree.Kind.LEFT_SHIFT, Tree.Kind.RIGHT_SHIFT, Tree.Kind.UNSIGNED_RIGHT_SHIFT);
 }
 private static boolean isSecuringByte(BinaryExpressionTree tree) {
   return tree.is(Tree.Kind.AND) && (is0xff(tree.rightOperand()) || is0xff(tree.leftOperand()));
 }
 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;
 }
Exemple #17
0
 private void buildBinaryExpression(Tree tree) {
   BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
   currentBlock.elements.add(tree);
   build(binaryExpressionTree.rightOperand());
   build(binaryExpressionTree.leftOperand());
 }