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;
 }
 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);
 }
 @Override
 public void visitBinaryExpression(BinaryExpressionTree tree) {
   if (tree.is(Kind.PLUS)) {
     checkConcatenation(tree, tree.leftOperand(), tree.rightOperand());
   }
   super.visitBinaryExpression(tree);
 }
Пример #4
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);
 }
Пример #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);
 }
Пример #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()
             + "\"");
   }
 }
Пример #9
0
 @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);
   }
 }
Пример #12
0
 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;
 }
Пример #13
0
 private static boolean isNanTest(BinaryExpressionTree binaryExpressionTree) {
   return SyntacticEquivalence.areEquivalent(
       binaryExpressionTree.leftOperand(), binaryExpressionTree.rightOperand());
 }
Пример #14
0
 private void buildBinaryExpression(Tree tree) {
   BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
   currentBlock.elements.add(tree);
   build(binaryExpressionTree.rightOperand());
   build(binaryExpressionTree.leftOperand());
 }
 private static boolean isSecuringByte(BinaryExpressionTree tree) {
   return tree.is(Tree.Kind.AND) && (is0xff(tree.rightOperand()) || is0xff(tree.leftOperand()));
 }