Examples of ExpressionTree


Examples of com.sun.source.tree.ExpressionTree

    SourcePositions positions = treeUtils.getSourcePositions();

    AnnotationTree annotationTree = (AnnotationTree) path.getLeaf();
    AssignmentTree assignTree =
        (AssignmentTree) annotationTree.getArguments().get(0);
    ExpressionTree exprTree = assignTree.getExpression();

    ArrayList<Long> lines = new ArrayList<Long>();
    if (exprTree.getKind() == Kind.STRING_LITERAL) {
      long pos = positions.getStartPosition(unitTree, exprTree);
      lines.add(lineMap.getLineNumber(pos));
    } else {
      NewArrayTree valuesTree = (NewArrayTree) exprTree;
      for (ExpressionTree valueTree : valuesTree.getInitializers()) {
View Full Code Here

Examples of com.sun.source.tree.ExpressionTree

   *   Preconditions.checkNotNull(foo.hasFoo()) ==> Preconditions.checkArgument(foo.hasFoo())
   * Otherwise, delete the checkNotNull call. E.g.:
   *   Preconditions.checkNotNull(foo); ==> [delete the line]
   */
  public Description describe(MethodInvocationTree methodInvocationTree, VisitorState state) {
    ExpressionTree arg1 = methodInvocationTree.getArguments().get(0);
    Tree parent = state.getPath().getParentPath().getLeaf();

    // Assignment, return, etc.
    if (parent.getKind() != Kind.EXPRESSION_STATEMENT) {
      return describeMatch(arg1,
          SuggestedFix.replace(methodInvocationTree, arg1.toString()));
    }

    // Comparison to null
    if (arg1.getKind() == Kind.EQUAL_TO || arg1.getKind() == Kind.NOT_EQUAL_TO) {
      BinaryTree binaryExpr = (BinaryTree) arg1;
      if (binaryExpr.getLeftOperand().getKind() == Kind.NULL_LITERAL) {
        return describeMatch(arg1,
            SuggestedFix.replace(arg1, binaryExpr.getRightOperand().toString()));
      }
      if (binaryExpr.getRightOperand().getKind() == Kind.NULL_LITERAL) {
        return describeMatch(arg1,
            SuggestedFix.replace(arg1, binaryExpr.getLeftOperand().toString()));
      }
    }

    if ((arg1 instanceof BinaryTree || arg1.getKind() == Kind.METHOD_INVOCATION ||
         arg1.getKind() == Kind.LOGICAL_COMPLEMENT) &&
        ((JCExpression) arg1).type == state.getSymtab().booleanType) {
      return describeMatch(arg1,
          createCheckArgumentOrStateCall(methodInvocationTree, state, arg1));
    }

View Full Code Here

Examples of com.sun.source.tree.ExpressionTree

  public Description matchEnhancedForLoop(EnhancedForLoopTree tree, VisitorState state) {
    JCEnhancedForLoop enhancedForLoop = (JCEnhancedForLoop) tree;
    IdentifierTree identifier =
        getIncrementedIdentifer(extractSingleStatement(enhancedForLoop.body));
    if (identifier != null) {
      ExpressionTree expression = tree.getExpression();
      Fix fix;
      if (isSubtypeOf("java.util.Collection").matches(expression, state)) {
        String replacement = identifier + " += " + expression + ".size();";
        fix = SuggestedFix.replace(tree, replacement);
      } else if (isArrayType().matches(expression, state)) {
View Full Code Here

Examples of com.sun.source.tree.ExpressionTree

  public boolean matches(AnnotationTree annotationTree, VisitorState state) {
    for (ExpressionTree argumentTree : annotationTree.getArguments()) {
      if (argumentTree.getKind() == Tree.Kind.ASSIGNMENT) {
        AssignmentTree assignmentTree = (AssignmentTree) argumentTree;
        if (assignmentTree.getVariable().toString().equals(element)) {
          ExpressionTree expressionTree = assignmentTree.getExpression();
          while (expressionTree instanceof ParenthesizedTree) {
            expressionTree = ((ParenthesizedTree) expressionTree).getExpression();
          }

          if (expressionTree instanceof NewArrayTree) {
View Full Code Here

Examples of com.sun.source.tree.ExpressionTree

      expressionDataflow(TreePath exprPath, Context context, T transfer) {
    final Tree leaf = exprPath.getLeaf();
    Preconditions.checkArgument(leaf instanceof ExpressionTree,
        "Leaf of exprPath must be of type ExpressionTree, but was %s", leaf.getClass().getName());

    final ExpressionTree expr = (ExpressionTree) leaf;
    final TreePath enclosingMethodPath =
        findPathFromEnclosingNodeToTopLevel(exprPath, MethodTree.class);

    if (enclosingMethodPath == null) {
      // TODO(user) this can happen in field initialization.
View Full Code Here

Examples of com.sun.source.tree.ExpressionTree

      anyOf(kindIs(EQUAL_TO), kindIs(NOT_EQUAL_TO)),
      binaryTree(SUBCLASS_OF_NUMBER, SUBCLASS_OF_NUMBER));

  @Override
  public Description matchBinary(BinaryTree tree, VisitorState state) {
    ExpressionTree leftOperand = tree.getLeftOperand();
    ExpressionTree rightOperand = tree.getRightOperand();
    Symbol left = ASTHelpers.getSymbol(leftOperand);
    Symbol right = ASTHelpers.getSymbol(rightOperand);
    if (left == null || right == null) {
      return Description.NO_MATCH;
    }
View Full Code Here

Examples of com.sun.source.tree.ExpressionTree

      public boolean matches(MethodInvocationTree t, VisitorState state) {
        List<? extends ExpressionTree> args = t.getArguments();
        if (args.size() <= argNum) {
          return false;
        }
        ExpressionTree arg = args.get(argNum);

        JCExpression methodSelect = (JCExpression) t.getMethodSelect();
        if (methodSelect instanceof JCFieldAccess) {
          JCFieldAccess fieldAccess = (JCFieldAccess) methodSelect;
          return ASTHelpers.sameVariable(fieldAccess.getExpression(), arg);
        } else if (methodSelect instanceof JCIdent) {
          // A bare method call: "equals(foo)".  Receiver is implicitly "this".
          return "this".equals(arg.toString());
        }

        return false;
      }
    };
View Full Code Here

Examples of com.sun.source.tree.ExpressionTree

    }

    StringBuilder fixedExpression = new StringBuilder();
    Fix fix = null;

    ExpressionTree leftOperand = tree.getLeftOperand();
    ExpressionTree rightOperand = tree.getRightOperand();
    Type leftType = ((JCTree) leftOperand).type;
    Types types = state.getTypes();
    Symtab symtab = state.getSymtab();

    /**
     * Try to figure out what they were trying to do.
     * Cases:
     * 1) (foo == foo) ==> (foo == other.foo)
     * 2) (foo == this.foo) ==> (other.foo == this.foo)
     * 3) (this.foo == foo) ==> (this.foo == other.foo)
     * 4) (this.foo == this.foo) ==> (this.foo == other.foo)
     */

    // Choose argument to replace.
    ExpressionTree toReplace;
    if (rightOperand.getKind() == Kind.IDENTIFIER) {
      toReplace = rightOperand;
    } else if (leftOperand.getKind() == Kind.IDENTIFIER) {
      toReplace = leftOperand;
    } else {
      // If we don't have a good reason to replace one or the other, replace the second.
      toReplace = rightOperand;
    }

    // Find containing block
    TreePath path = state.getPath();
    while (path.getLeaf() != null && path.getLeaf().getKind() != Kind.CLASS
        && path.getLeaf().getKind() != Kind.BLOCK) {
      path = path.getParentPath();
    }
    if (path.getLeaf() != null) {
      List<? extends JCTree> members;
      // Must be block or class
      if (path.getLeaf().getKind() == Kind.CLASS) {
        members = ((JCClassDecl) path.getLeaf()).getMembers();
      } else {
        members = ((JCBlock) path.getLeaf()).getStatements();
      }
      for (JCTree jcTree : members) {
        if (jcTree.getKind() == Kind.VARIABLE) {
          JCVariableDecl declaration = (JCVariableDecl) jcTree;
          TypeSymbol variableTypeSymbol = declaration.getType().type.tsym;

          if (ASTHelpers.getSymbol(toReplace).isMemberOf(variableTypeSymbol, state.getTypes())) {
            if (toReplace.getKind() == Kind.IDENTIFIER) {
              fix = SuggestedFix.prefixWith(toReplace, declaration.getName() + ".");
            } else {
              fix = SuggestedFix.replace(
                  ((JCFieldAccess) toReplace).getExpression(), declaration.getName().toString());
            }
View Full Code Here

Examples of com.sun.source.tree.ExpressionTree

   * Fixes the error by assigning the result of the call to the receiver reference, or deleting
   * the method call.
   */
  public Description describe(MethodInvocationTree methodInvocationTree, VisitorState state) {
    // Find the root of the field access chain, i.e. a.intern().trim() ==> a.
    ExpressionTree identifierExpr = ASTHelpers.getRootAssignable(methodInvocationTree);
    String identifierStr = null;
    Type identifierType = null;
    if (identifierExpr != null) {
      identifierStr = identifierExpr.toString();
      if (identifierExpr instanceof JCIdent) {
        identifierType = ((JCIdent) identifierExpr).sym.type;
      } else if (identifierExpr instanceof JCFieldAccess) {
        identifierType = ((JCFieldAccess) identifierExpr).sym.type;
      } else {
View Full Code Here

Examples of com.sun.source.tree.ExpressionTree

public class SelfAssignment extends BugChecker
    implements AssignmentTreeMatcher, VariableTreeMatcher {

  @Override
  public Description matchAssignment(AssignmentTree tree, VisitorState state) {
    ExpressionTree expression = stripCheckNotNull(tree.getExpression(), state);
    if(ASTHelpers.sameVariable(tree.getVariable(), expression)) {
      return describeForAssignment(tree, state);
    }
    return Description.NO_MATCH;
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.