Package com.sun.source.util

Examples of com.sun.source.util.TreePath


      // 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;
View Full Code Here


  @Override
  public boolean matches(T stmt, VisitorState state) {
    // TODO(user): should re-use Enclosing.BlockOrCase
    // find enclosing block
    TreePath path = state.getPath();
    Tree prev = null;
    Tree curr = path.getLeaf();     // initialized to curr node (if stmt)
    boolean found = false;
    while (path != null) {
      prev = curr;
      path = path.getParentPath();
      curr = path.getLeaf();
      if (curr.getKind() == Kind.BLOCK) {
        found = true;
        break;
      }
    }
View Full Code Here

        rhsName = ((JCFieldAccess) rhs).name.toString();
      }

      // find method parameters of the same type
      Type type = ((JCFieldAccess) lhs).type;
      TreePath path = state.getPath();
      while (path != null && path.getLeaf().getKind() != METHOD) {
        path = path.getParentPath();
      }
      JCMethodDecl method = (JCMethodDecl) path.getLeaf();
      int minEditDistance = Integer.MAX_VALUE;
      String replacement = null;
      for (JCVariableDecl var : method.params) {
        if (var.type == type) {
          int editDistance = EditDistance.getEditDistance(rhsName, var.name.toString());
          if (editDistance < minEditDistance) {
            // pick one with minimum edit distance
            minEditDistance = editDistance;
            replacement = var.name.toString();
          }
        }
      }
      if (replacement != null) {
        // suggest replacing rhs with the parameter
        fix = SuggestedFix.replace(rhs, replacement);
      }
    } else if (rhs.getKind() == IDENTIFIER) {
      // find a field of the same type and similar name and suggest it as the lhs

      // lhs should be identifier
      assert(lhs.getKind() == IDENTIFIER);

      // get current name of lhs
      String lhsName = ((JCIdent) rhs).name.toString();

      // find class instance fields of the same type
      Type type = ((JCIdent) lhs).type;
      TreePath path = state.getPath();
      while (path != null && !(path.getLeaf() instanceof JCClassDecl)) {
        path = path.getParentPath();
      }
      if (path == null) {
        throw new IllegalStateException("Expected to find an enclosing class declaration");
      }
      JCClassDecl klass = (JCClassDecl) path.getLeaf();
      int minEditDistance = Integer.MAX_VALUE;
      String replacement = null;
      for (JCTree member : klass.getMembers()) {
        if (member.getKind() == VARIABLE) {
          JCVariableDecl var = (JCVariableDecl) member;
View Full Code Here

     * @returns true if an error is found.
     */
    @Override
    public boolean matches(T tree, VisitorState state) {

      TreePath path = state.getPath();
      Tree prevTree = path.getLeaf();

      while (path != null && path.getLeaf().getKind() != Kind.METHOD
          && path.getLeaf().getKind() != Kind.COMPILATION_UNIT) {
        prevTree = path.getLeaf();
        path = path.getParentPath();

        MatchResult mr = (matchAncestor(path.getLeaf(), prevTree));
        if (mr != MatchResult.KEEP_LOOKING) {
          return mr == MatchResult.FOUND_ERROR;
        }
      }

View Full Code Here

      } else if (rhs.getKind() == MEMBER_SELECT) {
        rhsName = ((JCFieldAccess) rhs).name.toString();
      }

      // find method parameters of the type "Collection"
      TreePath path = state.getPath();
      while (path != null && path.getLeaf().getKind() != METHOD) {
        path = path.getParentPath();
      }
      JCMethodDecl method = (JCMethodDecl) path.getLeaf();
      int minEditDistance = Integer.MAX_VALUE;
      String replacement = null;
      for (JCVariableDecl var : method.params) {
        if (variableType(isSubtypeOf("java.util.Collection")).matches(var, state)) {
          int editDistance = EditDistance.getEditDistance(rhsName, var.name.toString());
          if (editDistance < minEditDistance) {
            // pick one with minimum edit distance
            minEditDistance = editDistance;
            replacement = var.name.toString();
          }
        }
      }
      if (replacement != null) {
        // suggest replacing rhs with the parameter
        fix = SuggestedFix.replace(rhs, replacement);
      }
    } else if (rhs.getKind() == IDENTIFIER) {
      // find a field of the same type and similar name and suggest it as the lhs

      // lhs should be identifier
      assert(lhs.getKind() == IDENTIFIER);

      // get current name of lhs
      String lhsName = ((JCIdent) rhs).name.toString();

      // find class instance fields of the type "Collection"
      TreePath path = state.getPath();
      while (path != null && path.getLeaf().getKind() != CLASS) {
        path = path.getParentPath();
      }
      JCClassDecl klass = (JCClassDecl) path.getLeaf();
      int minEditDistance = Integer.MAX_VALUE;
      String replacement = null;
      for (JCTree member : klass.getMembers()) {
        if (member.getKind() == VARIABLE) {
          JCVariableDecl var = (JCVariableDecl) member;
View Full Code Here

      } else {
        // If we don't have a good reason to replace one or the other, replace the second.
        toReplace = args.get(1);
      }
      // Find containing block
      TreePath path = state.getPath();
      while(path.getLeaf().getKind() != Kind.BLOCK) {
        path = path.getParentPath();
      }
      JCBlock block = (JCBlock)path.getLeaf();
      for (JCStatement jcStatement : block.getStatements()) {
        if (jcStatement.getKind() == Kind.VARIABLE) {
          JCVariableDecl declaration = (JCVariableDecl) jcStatement;
          TypeSymbol variableTypeSymbol = declaration.getType().type.tsym;
View Full Code Here

        final JCMethodDecl tree = (JCMethodDecl) elementUtils.getTree(e);

        thisExp = tm.This((Type) encClass.asType());

        final TreePath treePath = trees.getPath(e);
        final CompilationUnitTree cut = treePath.getCompilationUnit();
        tree.body = processElement(tree.body, cut, tree);

        if (reflectionInjected || methodInjected || constructorInjected) {
            tree.thrown = tree.thrown.append(getId("java.lang.ClassNotFoundException"));
            tree.thrown = tree.thrown.append(getId("java.lang.NoSuchFieldException"));
View Full Code Here

    private Scope getScope(Tree stmt, final CompilationUnitTree cut, Scope validScope) {
        if (stmt instanceof JCVariableDecl) {
            JCExpression exp = ((JCVariableDecl) stmt).init;
            ((JCVariableDecl) stmt).init = null;
            TreePath path = TreePath.getPath(cut, stmt);
            validScope = trees.getScope(path);
            ((JCVariableDecl) stmt).init = exp;
        } else if (stmt instanceof JCEnhancedForLoop) {
            return getScope(((JCEnhancedForLoop) stmt).var, cut, validScope);
        } else if (stmt instanceof JCForLoop) {
View Full Code Here

        return validScope;
    }

    protected JCBlock processElement(BlockTree tree, final CompilationUnitTree cut, Tree getScope) {
        if(tree == null) return null;
        TreePath path = trees.getPath(cut, getScope);
        return processElement((JCBlock) tree, cut, trees.getScope(path));
    }
View Full Code Here

            if (refDecl != null) {
                refStmts[0] = refDecl;
                refStmts[1] = setAccessibleExec;
                encBlock.stats = injectBefore((JCStatement) n.actual, encBlock.stats, refStmts);
                TreePath refPath = trees.getPath(cut, refStmts[1]);
                n.scope = getScope(n.actual, cut, trees.getScope(refPath));
            }

            reflectionInjected = true;
        }
View Full Code Here

TOP

Related Classes of com.sun.source.util.TreePath

Copyright © 2018 www.massapicom. 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.