public Description matchBinary()

in legacy/java/piranha/src/main/java/com/uber/piranha/XPFlagCleaner.java [870:920]


  public Description matchBinary(BinaryTree tree, VisitorState state) {
    if (shouldSkip(state)) return Description.NO_MATCH;
    if (overLaps(tree, state)) {
      return Description.NO_MATCH;
    }

    Value x = evalExpr(tree, state);
    Description d = updateCode(x, tree, tree, state);
    if (!d.equals(Description.NO_MATCH)) {
      return d;
    }

    ExpressionTree deletedSubTree = null;
    ExpressionTree remainingSubTree = null;
    Value l = evalExpr(tree.getLeftOperand(), state);
    Value r = evalExpr(tree.getRightOperand(), state);
    if (tree.getKind().equals(Kind.CONDITIONAL_AND)) {
      if (l.equals(Value.TRUE)) {
        deletedSubTree = tree.getLeftOperand();
        remainingSubTree = tree.getRightOperand();
      } else if (r.equals(Value.TRUE)) {
        deletedSubTree = tree.getRightOperand();
        remainingSubTree = tree.getLeftOperand();
      }
    } else if (tree.getKind().equals(Kind.CONDITIONAL_OR)) {
      if (l.equals(Value.FALSE)) {
        deletedSubTree = tree.getLeftOperand();
        remainingSubTree = tree.getRightOperand();
      } else if (r.equals(Value.FALSE)) {
        deletedSubTree = tree.getRightOperand();
        remainingSubTree = tree.getLeftOperand();
      }
    }

    if (deletedSubTree != null) {
      Preconditions.checkNotNull(
          remainingSubTree, "deletedSubTree != null => remainingSubTree !=null here.");
      Tree parent = state.getPath().getParentPath().getLeaf();
      Tree replacee = parent instanceof ParenthesizedTree ? parent : tree;
      Description.Builder builder = buildDescription(replacee);
      SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
      fixBuilder.replace(replacee, remainingSubTree.toString());
      decrementAllSymbolUsages(deletedSubTree, state, fixBuilder);
      builder.addFix(fixBuilder.build());

      endPos = state.getEndPosition(tree);
      return builder.build();
    }

    return Description.NO_MATCH;
  }