Java Code Examples for com.sun.source.tree.BinaryTree#getKind()

The following examples show how to use com.sun.source.tree.BinaryTree#getKind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BoxedIdentityComparison.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
    TreePath p = ctx.getPath();
    if (p.getLeaf().getKind() != Tree.Kind.EQUAL_TO  && p.getLeaf().getKind() != Tree.Kind.NOT_EQUAL_TO) {
        // TODO - report ?
        return;
    }
    BinaryTree bt = (BinaryTree)p.getLeaf();
    TreeMaker mk = ctx.getWorkingCopy().getTreeMaker();
    ExpressionTree replace = mk.MethodInvocation(
        Collections.<ExpressionTree>emptyList(),
        mk.MemberSelect(
            mk.QualIdent(JU_OBJECTS), "equals" // NOI18N
        ), 
        Arrays.asList(bt.getLeftOperand(), bt.getRightOperand())
    );
    if (bt.getKind() == Tree.Kind.NOT_EQUAL_TO) {
        replace = mk.Unary(Tree.Kind.LOGICAL_COMPLEMENT, replace);
    }
    ctx.getWorkingCopy().rewrite(bt, replace);
}
 
Example 2
Source File: MethodMetrics.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitBinary(BinaryTree node, Object p) {
    if (node.getKind() == Tree.Kind.NOT_EQUAL_TO) {
        negationsCount++;
    }
    return super.visitBinary(node, p);
}
 
Example 3
Source File: RemoveUnnecessary.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitBinary(BinaryTree node, Object p) {
    switch (node.getKind()) {
        case CONDITIONAL_AND:
        case CONDITIONAL_OR:
            return transformLogAndOr(node, p);
    }
    scan(node.getLeftOperand(), p);
    scan(node.getRightOperand(), p);
    return null;
}
 
Example 4
Source File: InfiniteRecursion.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public State visitBinary(BinaryTree node, Void p) {
    State s1, s2;

    if (returnIfRecurse(s1 = scan(node.getLeftOperand(), p))) {
        return s1;
    }
    Object result = null;
    if (node.getKind() == Tree.Kind.CONDITIONAL_AND || node.getKind() == Tree.Kind.CONDITIONAL_OR) {
        // does not recurse, but there's a chance the first part of the conditional will complete the condition
        // and will avoid the recursion.
        result = ArithmeticUtilities.compute(ci,
                new TreePath(getCurrentPath(), node.getLeftOperand()), true, true);
        // return if:
        // - we have OR and the left op is const true
        // - we have AND and the left op is const false;
        if (result != null && 
            ((result == Boolean.TRUE) == (node.getKind() == Tree.Kind.CONDITIONAL_OR))) {
            return State.NO;
        }
    }
    boolean saveDefinite = definitePath;
    definitePath = result != null;
    returnIfRecurse(s2 = scan(node.getRightOperand(), p));
    definitePath = saveDefinite;
    thisTree = null;
    return s2;
}
 
Example 5
Source File: WrongStringComparison.java    From netbeans with Apache License 2.0 5 votes vote down vote up
ExpressionTree matchSign(TreeMaker make, BinaryTree oldTree, ExpressionTree et) {
    if (oldTree.getKind() == Tree.Kind.NOT_EQUAL_TO) {
        return make.Unary(Tree.Kind.LOGICAL_COMPLEMENT, et);
    } else {
        return et;
    }
}
 
Example 6
Source File: NullAway.java    From NullAway with MIT License 5 votes vote down vote up
/**
 * @param path tree path to read operation
 * @return true if it is permissible to perform this read before the field has been initialized,
 *     false otherwise
 */
private boolean okToReadBeforeInitialized(TreePath path) {
  TreePath parentPath = path.getParentPath();
  Tree leaf = path.getLeaf();
  Tree parent = parentPath.getLeaf();
  if (parent instanceof AssignmentTree) {
    // ok if it's actually a write
    AssignmentTree assignment = (AssignmentTree) parent;
    return assignment.getVariable().equals(leaf);
  } else if (parent instanceof BinaryTree) {
    // ok if we're comparing to null
    BinaryTree binaryTree = (BinaryTree) parent;
    Tree.Kind kind = binaryTree.getKind();
    if (kind.equals(Tree.Kind.EQUAL_TO) || kind.equals(Tree.Kind.NOT_EQUAL_TO)) {
      ExpressionTree left = binaryTree.getLeftOperand();
      ExpressionTree right = binaryTree.getRightOperand();
      return (left.equals(leaf) && right.getKind().equals(Tree.Kind.NULL_LITERAL))
          || (right.equals(leaf) && left.getKind().equals(Tree.Kind.NULL_LITERAL));
    }
  } else if (parent instanceof MethodInvocationTree) {
    // ok if it's invoking castToNonNull and the read is the argument
    MethodInvocationTree methodInvoke = (MethodInvocationTree) parent;
    Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(methodInvoke);
    String qualifiedName =
        ASTHelpers.enclosingClass(methodSymbol) + "." + methodSymbol.getSimpleName().toString();
    if (qualifiedName.equals(config.getCastToNonNullMethod())) {
      List<? extends ExpressionTree> arguments = methodInvoke.getArguments();
      return arguments.size() == 1 && leaf.equals(arguments.get(0));
    }
  }
  return false;
}
 
Example 7
Source File: TreeConverter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private TreeNode convertBinary(BinaryTree node, TreePath parent) {
  TreePath path = getTreePath(parent, node);
  InfixExpression newNode = new InfixExpression();
  newNode
      .setTypeMirror(getTypeMirror(path))
      .setOperator(InfixExpression.Operator.from(node.getKind()));

  // Flatten this tree to avoid stack overflow with very deep trees. This
  // code traverses the subtree non-recursively and merges all children
  // that have the same operator into this node.
  List<StackState> stack = Lists.newArrayList();
  stack.add(new StackState(node));
  while (!stack.isEmpty()) {
    StackState currentState = stack.get(stack.size() - 1);
    ExpressionTree child = currentState.nextChild();
    if (child == null) {
      stack.remove(stack.size() - 1);
      continue;
    }
    if (child instanceof BinaryTree) {
      BinaryTree infixChild = (BinaryTree) child;
      if (infixChild.getKind() == node.getKind()) {
        stack.add(new StackState(infixChild));
        continue;
      }
    }
    newNode.addOperand((Expression) convert(child, path));
  }
  return newNode;
}
 
Example 8
Source File: TreePruner.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean visitBinary(BinaryTree node, Void p) {
  switch (node.getKind()) {
    case MULTIPLY:
    case DIVIDE:
    case REMAINDER:
    case PLUS:
    case MINUS:
    case LEFT_SHIFT:
    case RIGHT_SHIFT:
    case UNSIGNED_RIGHT_SHIFT:
    case LESS_THAN:
    case LESS_THAN_EQUAL:
    case GREATER_THAN:
    case GREATER_THAN_EQUAL:
    case AND:
    case XOR:
    case OR:
    case CONDITIONAL_AND:
    case CONDITIONAL_OR:
    case EQUAL_TO:
    case NOT_EQUAL_TO:
      break;
    default:
      // non-constant binary expression
      return false;
  }
  return reduce(
      node.getLeftOperand().accept(this, null), node.getRightOperand().accept(this, null));
}
 
Example 9
Source File: RemoveUnnecessary.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public Object transformLogAndOr(BinaryTree node, Object p) {
    List<StatementTree> saveStats = this.statements;
    boolean saveRemove = this.remove;
    this.remove = true;
    this.statements = new ArrayList<>();
    scan(node.getRightOperand(), p);
    
    if (remove) {
        // the if statement would be empty; attempt to transform the
        // left operand if it has at least something. Omit the left operand
        // at all.
        this.statements = saveStats;
        scan(node.getLeftOperand(), p);
        if (remove) {
            this.remove &= saveRemove;
            return null;
        }
        this.remove &= saveRemove;
    } else {
        List<StatementTree> elseStats = this.statements;
        this.statements = saveStats;
        remove = false;
        if (mk != null) {
            ExpressionTree condition;
            if (node.getKind() == Tree.Kind.CONDITIONAL_AND) {
                condition = node.getLeftOperand();
            } else {
                condition = Utilities.negate(mk, node.getLeftOperand(), node);
            }
            statements.add(
                    mk.If(condition, 
                          elseStats.size() == 1 ?
                                  elseStats.get(0) :
                                  mk.Block(elseStats, false),
                          null
                    )
            );
        }
    }
    return null;
}
 
Example 10
Source File: Flow.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Boolean visitBinary(BinaryTree node, ConstructorData p) {
    Boolean left = scan(node.getLeftOperand(), p);

    if (left != null && (node.getKind() == Kind.CONDITIONAL_AND || node.getKind() == Kind.CONDITIONAL_OR)) {
        if (left) {
            if (node.getKind() == Kind.CONDITIONAL_AND) {
                return scan(node.getRightOperand(), p);
            } else {
                return true;
            }
        } else {
            if (node.getKind() == Kind.CONDITIONAL_AND) {
                return false;
            } else {
                return scan(node.getRightOperand(), p);
            }
        }
    }

    Map<Element, State> oldVariable2State = variable2State;

    variable2State = new HashMap<Element, Flow.State>(oldVariable2State);
    
    Boolean right = scan(node.getRightOperand(), p);

    variable2State = mergeOr(variable2State, oldVariable2State);

    if (left == null || right == null) {
        return null;
    }

    switch (node.getKind()) {
        case AND: case CONDITIONAL_AND: return left && right;
        case OR: case CONDITIONAL_OR: return left || right;
        case EQUAL_TO: return left == right;
        case NOT_EQUAL_TO: return left != right;
    }
    
    return null;
}
 
Example 11
Source File: FlipOperands.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@TriggerTreeKind({
    Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO, Tree.Kind.MULTIPLY, Tree.Kind.PLUS,
    Tree.Kind.AND, Tree.Kind.OR, Tree.Kind.XOR, Tree.Kind.CONDITIONAL_AND, Tree.Kind.CONDITIONAL_OR,
    Tree.Kind.DIVIDE, Tree.Kind.REMAINDER, Tree.Kind.MINUS, Tree.Kind.LEFT_SHIFT,
    Tree.Kind.RIGHT_SHIFT, Tree.Kind.UNSIGNED_RIGHT_SHIFT, Tree.Kind.MULTIPLY,
    Tree.Kind.DIVIDE, Tree.Kind.REMAINDER, Tree.Kind.PLUS, Tree.Kind.MINUS,
    Tree.Kind.LEFT_SHIFT, Tree.Kind.RIGHT_SHIFT, Tree.Kind.UNSIGNED_RIGHT_SHIFT,
    Tree.Kind.LESS_THAN, Tree.Kind.GREATER_THAN, Tree.Kind.LESS_THAN_EQUAL,
    Tree.Kind.GREATER_THAN_EQUAL, Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO,
    Tree.Kind.AND, Tree.Kind.OR, Tree.Kind.XOR, Tree.Kind.CONDITIONAL_AND,
    Tree.Kind.CONDITIONAL_OR,
})
public static ErrorDescription equals(HintContext ctx) {
    final BinaryTree bt = (BinaryTree)ctx.getPath().getLeaf();
    Tree.Kind kind = bt.getKind();
    Tree.Kind targetKind;
    String displayName;
    final CompilationInfo ci = ctx.getInfo();
    final boolean unsafe;
    
    if (kind == Tree.Kind.PLUS) {
        // special case: if either of the operands is String, + is not commutative
        TypeMirror leftType = ci.getTrees().getTypeMirror(new TreePath(ctx.getPath(), bt.getLeftOperand()));
        if (Utilities.isJavaString(ci, leftType)) {
            unsafe = true;
        } else {
            TypeMirror rightType = ci.getTrees().getTypeMirror(new TreePath(ctx.getPath(), bt.getLeftOperand()));
            unsafe = Utilities.isJavaString(ci, rightType);
        }
    } else {
        unsafe = false;
    }

    if (unsafe || UNSAFE_FLIP.contains(kind)) {
        displayName = Bundle.FIX_FlipOperands3(OPERATOR_DN.get(kind));
        targetKind = kind;
    } else if (SAFE_FLIP.contains(kind)) {
        displayName = Bundle.FIX_FlipOperands1(OPERATOR_DN.get(kind));
        targetKind = kind;
    } else {
        targetKind = CONVERT_FLIP.get(kind);
        displayName = Bundle.FIX_FlipOperands2(OPERATOR_DN.get(kind), OPERATOR_DN.get(targetKind));
    }
    
    return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.DESC_FlipOperands(), new FixImpl(ctx.getInfo(), ctx.getPath(), displayName, targetKind).toEditorFix());
}
 
Example 12
Source File: ModelBuilder.java    From vertx-codetrans with Apache License 2.0 4 votes vote down vote up
@Override
public ExpressionModel visitBinary(BinaryTree node, VisitContext context) {
  ExpressionModel left = scan(node.getLeftOperand(), context);
  ExpressionModel right = scan(node.getRightOperand(), context);
  String op;
  switch (node.getKind()) {
    case CONDITIONAL_AND:
      op = "&&";
      break;
    case CONDITIONAL_OR:
      op = "||";
      break;
    case EQUAL_TO:
      op = "==";
      break;
    case NOT_EQUAL_TO:
      op = "!=";
      break;
    case PLUS:
      op = "+";
      break;
    case LESS_THAN:
      op = "<";
      break;
    case LESS_THAN_EQUAL:
      op = "<=";
      break;
    case GREATER_THAN:
      op = ">";
      break;
    case GREATER_THAN_EQUAL:
      op = ">=";
      break;
    case MULTIPLY:
      op = "*";
      break;
    case DIVIDE:
      op = "/";
      break;
    case AND:
      op = "&";
      break;
    case OR:
      op = "|";
      break;
    case XOR:
      op = "^";
      break;
    case MINUS:
      op = "-";
      break;
    case REMAINDER:
      op = "%";
      break;
    default:
      throw new UnsupportedOperationException("Binary operator " + node.getKind().name() + " not yet implemented");
  }
  return context.builder.combine(left, op, right);
}