com.sun.source.util.TreePath.getParentPath()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(66)

本文整理了Java中com.sun.source.util.TreePath.getParentPath()方法的一些代码示例,展示了TreePath.getParentPath()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TreePath.getParentPath()方法的具体详情如下:
包路径:com.sun.source.util.TreePath
类名称:TreePath
方法名:getParentPath

TreePath.getParentPath介绍

[英]Get the path for the enclosing node, or null if there is no enclosing node.
[中]获取封闭节点的路径,如果没有封闭节点,则为null。

代码示例

代码示例来源:origin: robolectric/robolectric

private static boolean isPartOfMethodInvocation(TreePath idPath) {
 Kind parentKind = idPath.getParentPath().getLeaf().getKind();
 if (parentKind == Kind.METHOD_INVOCATION) {
  // must be an argument
  return true;
 }
 if (parentKind == Kind.MEMBER_SELECT) {
  Tree maybeMethodInvocation = idPath.getParentPath().getParentPath().getLeaf();
  // likely the target of the method invocation
  return maybeMethodInvocation.getKind() == Kind.METHOD_INVOCATION;
 }
 return false;
}

代码示例来源:origin: google/error-prone

/**
 * Given a TreePath, finds the first enclosing node of the given type and returns the path from
 * the enclosing node to the top-level {@code CompilationUnitTree}.
 */
public static <T> TreePath findPathFromEnclosingNodeToTopLevel(TreePath path, Class<T> klass) {
 if (path != null) {
  do {
   path = path.getParentPath();
  } while (path != null && !(klass.isInstance(path.getLeaf())));
 }
 return path;
}

代码示例来源:origin: google/error-prone

/**
 * Returns the enclosing method of the given visitor state. Returns null if the state is within a
 * lambda expression or anonymous class.
 */
@Nullable
private static MethodTree enclosingMethod(VisitorState state) {
 for (Tree node : state.getPath().getParentPath()) {
  switch (node.getKind()) {
   case LAMBDA_EXPRESSION:
   case NEW_CLASS:
    return null;
   case METHOD:
    return (MethodTree) node;
   default:
    break;
  }
 }
 return null;
}

代码示例来源:origin: google/error-prone

@Override
public IdentifierTree visitIdentifier(IdentifierTree node, Void aVoid) {
 Symbol nodeSymbol = ASTHelpers.getSymbol(node);
 if (symbols.contains(nodeSymbol) && !isSuppressed(node)) {
  if (getCurrentPath().getParentPath().getLeaf().getKind() != Kind.CASE) {
   builder.prefixWith(node, enclosingReplacement);
   moveTypeAnnotations(node);
   return node;
  }
 }
 return super.visitIdentifier(node, aVoid);
}

代码示例来源:origin: google/error-prone

private static boolean isInPrivateScope(VisitorState state) {
  TreePath treePath = state.getPath();
  do {
   Tree currentLeaf = treePath.getLeaf();
   if (currentLeaf instanceof ClassTree) {
    ClassTree currentClassTree = (ClassTree) currentLeaf;
    if (currentClassTree.getModifiers().getFlags().contains(PRIVATE)) {
     return true;
    }
   }
   treePath = treePath.getParentPath();
  } while (treePath != null);

  return false;
 }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-beans

/**
     * Must be run under MDR transaction!
     */
    public javax.lang.model.element.Element getJavaClass() {
      TreePath path = null;
//            try {
//                path = getCompletionTreePath(getController(), endOffset, COMPLETION_QUERY_TYPE);
//            } catch (IOException ex) {
//                Exceptions.printStackTrace(ex);
//            }
      javax.lang.model.element.Element el = null;
      try {
        getController().toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
      } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
      }
      while ((el == null || !(ElementKind.CLASS == el.getKind() || ElementKind.INTERFACE == el.getKind())) && path != null) {
        path.getCompilationUnit().getTypeDecls();
        el = getController().getTrees().getElement(path);
        path = path.getParentPath();
      }
      return el;
    }

代码示例来源:origin: google/error-prone

@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
 ExpressionTree initializer = stripNullCheck(tree.getInitializer(), state);
 Tree parent = state.getPath().getParentPath().getLeaf();
 // must be a static class variable with member select initializer
 if (initializer == null
   || initializer.getKind() != MEMBER_SELECT
   || parent.getKind() != CLASS
   || !tree.getModifiers().getFlags().contains(STATIC)) {
  return Description.NO_MATCH;
 }
 MemberSelectTree rhs = (MemberSelectTree) initializer;
 Symbol rhsClass = ASTHelpers.getSymbol(rhs.getExpression());
 Symbol lhsClass = ASTHelpers.getSymbol(parent);
 if (rhsClass != null
   && lhsClass != null
   && rhsClass.equals(lhsClass)
   && rhs.getIdentifier().contentEquals(tree.getName())) {
  return describeForVarDecl(tree, state);
 }
 return Description.NO_MATCH;
}

代码示例来源:origin: google/error-prone

while (path != null && !(path.getLeaf() instanceof MethodTree)) {
 path = path.getParentPath();
MethodTree methodDecl = (MethodTree) path.getLeaf();
for (VariableTree param : methodDecl.getParameters()) {
 if (symbols.contains(ASTHelpers.getSymbol(param))) {
  return true;

代码示例来源:origin: google/error-prone

@Nullable
 private static JCMethodDecl findSurroundingMethod(TreePath path) {
  while (path.getLeaf().getKind() != Kind.METHOD) {
   if (path.getLeaf().getKind() == Kind.LAMBDA_EXPRESSION) {
    // Ignore return statements in lambda expressions. There's no method declaration to suggest
    // annotations for anyway.
    return null;
   }
   path = path.getParentPath();
  }
  return (JCMethodDecl) path.getLeaf();
 }
}

代码示例来源:origin: google/error-prone

private static boolean collectionUsed(VisitorState state) {
 TreePath path = state.getPath();
 return !(path.getParentPath().getLeaf() instanceof MemberSelectTree)
   || !(path.getParentPath().getParentPath().getLeaf() instanceof MethodInvocationTree)
   || !COLLECTION_SETTER.matches(
     (MethodInvocationTree) path.getParentPath().getParentPath().getLeaf(), state)
   || ASTHelpers.targetType(state.withPath(path.getParentPath().getParentPath())) != null;
}

代码示例来源:origin: google/error-prone

@Override
 public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
  if (!FENCE_MATCHER.matches(tree, state)) {
   return NO_MATCH;
  }
  Tree previous = null;
  OUTER:
  for (Tree enclosing : state.getPath().getParentPath()) {
   switch (enclosing.getKind()) {
    case TRY:
     if (((TryTree) enclosing).getFinallyBlock().equals(previous)) {
      return NO_MATCH;
     }
     break;
    case CLASS:
    case METHOD:
    case LAMBDA_EXPRESSION:
     break OUTER;
    default: // fall out
   }
   previous = enclosing;
  }
  return describeMatch(tree);
 }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-java-editor-base

private static TreePath findTypePath(TreePath tp) {
  if (!TYPE_PATH_ELEMENT.contains(tp.getLeaf().getKind()))
    return null;
  while (TYPE_PATH_ELEMENT.contains(tp.getParentPath().getLeaf().getKind())) {
    tp = tp.getParentPath();
  }
  return tp;
}

代码示例来源:origin: google/error-prone

@Override
 public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
  if (!MATCHER.matches(tree, state)) {
   return NO_MATCH;
  }
  Tree parent = state.getPath().getParentPath().getLeaf();
  if (!(parent instanceof TypeCastTree)) {
   return NO_MATCH;
  }
  if (!((TypeCastTree) parent).getExpression().equals(tree)) {
   return NO_MATCH;
  }
  Type type = ASTHelpers.getType(parent);
  if (type == null || !INTEGRAL.contains(type.getKind())) {
   return NO_MATCH;
  }
  return describeMatch(tree);
 }
}

代码示例来源:origin: robolectric/robolectric

private static TreePath findParentOfKind(VisitorState state, Kind kind) {
 TreePath path = state.getPath();
 while (path != null && path.getLeaf().getKind() != kind) {
  path = path.getParentPath();
 }
 return path;
}

代码示例来源:origin: google/error-prone

private Tree getCurrentlyAnnotatedNode(VisitorState state) {
  return state.getPath().getParentPath().getParentPath().getLeaf();
 }
}

代码示例来源:origin: google/error-prone

@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
 switch (tree.getKind()) {
  case AND:
  case OR:
   break;
  default:
   return NO_MATCH;
 }
 if (!isSameType(getType(tree), state.getSymtab().booleanType, state)) {
  return NO_MATCH;
 }
 Iterator<Tree> stateIterator = state.getPath().getParentPath().iterator();
 Tree parent = stateIterator.next();
 if (parent instanceof BinaryTree
   && (parent.getKind() == Kind.AND || parent.getKind() == Kind.OR)) {
  return NO_MATCH;
 } else {
  SuggestedFix.Builder fix = SuggestedFix.builder();
  new TreeScannerBinary(state).scan(tree, fix);
  return describeMatch(tree, fix.build());
 }
}

代码示例来源:origin: org.netbeans.api/org-netbeans-modules-java-source-base

/**
 * Returns path to the deepest tree of one of the given kinds containing
 * the given starting tree.
 * @param kinds requested tree kinds
 * @param path path to the starting tree
 * @return requested path or null if no tree of the given kinds encloses
 * the given starting tree
 *
 * @since 0.136
 */
public TreePath getPathElementOfKind(Set<Tree.Kind> kinds, TreePath path) {
  while (path != null) {
    if (kinds.contains(path.getLeaf().getKind())) {
      return path;
    }
    path = path.getParentPath();
  }
  return null;        
}

代码示例来源:origin: google/error-prone

Tree toReplace = null;
for (TreePath path = state.getPath().getParentPath();
  path != null;
  path = path.getParentPath()) {
 Tree enclosing = path.getLeaf();
 if (unmodifiableMatcher.matches(enclosing, state)) {
  unmodifiable = enclosing;
  constant =
    symbol.isStatic()
      && symbol.getModifiers().contains(Modifier.FINAL)
      && symbol.getKind() == ElementKind.FIELD;

代码示例来源:origin: google/error-prone

private TreePath findEnclosing(VisitorState state) {
  for (TreePath path = state.getPath(); path != null; path = path.getParentPath()) {
   switch (path.getLeaf().getKind()) {
    case METHOD:
    case LAMBDA_EXPRESSION:
     return path;
    case CLASS:
     return null;
    default: // fall out
   }
  }
  return null;
 }
}

代码示例来源:origin: google/error-prone

private Tree getCurrentlyAnnotatedNode(VisitorState state) {
  return state.getPath().getParentPath().getParentPath().getLeaf();
 }
}

相关文章

微信公众号

最新文章

更多