Java Code Examples for com.sun.source.tree.Tree#equals()

The following examples show how to use com.sun.source.tree.Tree#equals() . 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: PreconditionsChecker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isLastInControlFlow(TreePath pathToInstruction) {
    Tree currentTree = pathToInstruction.getLeaf();
    Tree parentTree = pathToInstruction.getParentPath().getLeaf();
    if (parentTree.equals(this.loop)) {
        return true;
    } else if (parentTree.getKind() == Tree.Kind.BLOCK) {
        List<? extends StatementTree> ls = ((BlockTree) parentTree).getStatements();
        if (ls.get(ls.size() - 1).equals(currentTree)) {
            return isLastInControlFlow(pathToInstruction.getParentPath());
        } else {
            return false;
        }

    } else if (parentTree.getKind() == Tree.Kind.AND.IF && ((IfTree) parentTree).getElseStatement() != null) {
        return false;
    } else {
        return this.isLastInControlFlow(pathToInstruction.getParentPath());
    }


}
 
Example 2
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 3
Source File: LocalVariableCriterion.java    From annotation-tools with MIT License 4 votes vote down vote up
@Override
public boolean isSatisfiedBy(TreePath path) {
  if (path == null) {
    return false;
  }

  TreePath parentPath = path.getParentPath();
  if (parentPath == null) {
    return false;
  }

  Tree parent = parentPath.getLeaf();
  Tree leaf = path.getLeaf();
  if (parent instanceof VariableTree) {
    // parent is a variable declaration

    if (parentPath.getParentPath().getLeaf() instanceof MethodTree) {
      // formal parameter, not local variable
      return false;
    }

    VariableTree vtt = (VariableTree) parent;
    if (leaf.equals(vtt.getInitializer())) {
      // don't match in initializer
      return false;
    }

    String varName = vtt.getName().toString();

    if (Objects.equals(loc.varName, varName)) {
      int varIndex = LocalVariableScanner.indexOfVarTree(path, vtt, varName);
      return (loc.varIndex == varIndex);
    }

    Pair<String, Pair<Integer, Integer>> key =
            Pair.of(fullMethodName, Pair.of(loc.index, loc.scopeStart));
    String potentialVarName =
            LocalVariableScanner.getFromMethodNameIndexMap(key);
    if (potentialVarName != null) {
      if (varName.equals(potentialVarName)) {
        // now use methodNameCounter to ensure that if this is the
        // i'th variable of this name, its offset is the i'th offset
        // of all variables with this name
        List<Integer> allOffsetsWithThisName =
                LocalVariableScanner.getFromMethodNameCounter(fullMethodName, potentialVarName);
        //      methodNameCounter.get(fullMethodName).get(potentialVarName);
        Integer thisVariablesOffset =
                allOffsetsWithThisName.indexOf(loc.scopeStart);

        // now you need to make sure that this is the
        // thisVariablesOffset'th variable tree in the entire source
        int i = LocalVariableScanner.indexOfVarTree(path, parent, potentialVarName);

        if (i == thisVariablesOffset) {
          return true;
        }
      }
    }
    return false;
  }
  // isSatisfiedBy should return true not just for the local variable itself, but for its type.
  // So, if this is part of a type, try its parent.
  // For example, return true for the tree for "Integer"
  // within the local variable "List<Integer> foo;"
  //
  // But, stop the search once it gets to certain types, such as MethodTree.
  // Is it OK to stop at ExpressionTree too?
  else if (parent instanceof MethodTree) {
    return false;
  } else {
    return this.isSatisfiedBy(parentPath);
  }
}