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}.
497345
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;
  }
1275435
private void addSuperOuterPath(TypeDeclaration node) {
    TypeElement superclass = ElementUtil.getSuperclass(node.getTypeElement());
    if (superclass != null && captureInfo.needsOuterParam(superclass)) {
      node.setSuperOuter(getOuterPathInherited(ElementUtil.getDeclaringClass(superclass)));
    }
  }
429227
protected void findEntryPoint() {
        if (entryPoint == null) {
            StringBuilder path = new StringBuilder();
            Resource parent = resource;
            String name = null;
            while (parent != null && !ResourceUtil.CONTENT_NODE.equals(name = parent.getName())) {
                if (inheritanceType != Type.contentBased) {
                    if (path.length() > 0) {
                        path.insert(0, '/');
                    }
                    path.insert(0, name);
                }
                parent = parent.getParent();
            }
            if (inheritanceType != Type.nodeRelated && inheritanceType != Type.sameContent
                    && parent != null && ResourceUtil.CONTENT_NODE.equals(name)) {
                // the resource is a child of an element with a content subtree ('jcr:content/...')
                path.insert(0, '/');
                path.insert(0, ResourceUtil.CONTENT_NODE);
                relativePath = path.toString();
                entryPoint = parent.getParent();
            } else {
                // node inheritance or no content subtree ('jcr:content/...') detected...
                relativePath = "";
                entryPoint = resource.getParent();
                if (inheritanceType == Type.sameContent && parent != null && ResourceUtil.CONTENT_NODE.equals(name)) {
                    exitPoint = parent;
                }
            }
        }
    }
273125
public static <T> T fromXml(File xmlPath, Class<T> type) {
        BufferedReader reader = null;
        StringBuilder sb = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(xmlPath), ENCODING));
            String line = null;
            sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException(e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    //ignore
                }
            }
        }
        return fromXml(sb.toString(), type);
    }
1012971
static ClassNode boundUnboundedWildcards(ClassNode type) {
        if (type.isArray()) {
            return boundUnboundedWildcards(type.getComponentType()).makeArray();
        }
        ClassNode target = type.redirect();
        if (target == null || type == target || !isUsingGenericsOrIsArrayUsingGenerics(target)) return type;
        ClassNode newType = type.getPlainNodeReference();
        newType.setGenericsPlaceHolder(type.isGenericsPlaceHolder());
        newType.setGenericsTypes(boundUnboundedWildcards(type.getGenericsTypes(), target.getGenericsTypes()));
        return newType;
    }
918509
public CmsPathTree<P, V> findNode(List<P> path) {

        List<P> pathToConsume = Lists.newLinkedList(path);
        CmsPathTree<P, V> descendant = findNodeInternal(pathToConsume);
        if (!pathToConsume.isEmpty()) {
            return null;
        } else {
            return descendant;
        }
    }
1207402
@Nullable
  private static Node getEnclosingTarget(Node targetNode) {
    checkState(checkNotNull(targetNode).isValidAssignmentTarget(), targetNode);
    Node parent = checkNotNull(targetNode.getParent(), targetNode);
    boolean targetIsFirstChild = parent.getFirstChild() == targetNode;
    if (parent.isDefaultValue() || parent.isRest()) {
      // in `([something = targetNode] = x)` targetNode isn't actually acting
      // as a target.
      checkState(targetIsFirstChild, parent);
      // The DEFAULT_VALUE or REST occupies the place where the assignment target it contains would
      // otherwise be in the AST, so pretend it is the target for the logic below.
      targetNode = parent;
      parent = checkNotNull(targetNode.getParent());
      targetIsFirstChild = targetNode == parent.getFirstChild();
    }
    switch (parent.getToken()) {
      case ARRAY_PATTERN:
        // e.g. ([targetNode] = something)
        return parent;

      case OBJECT_PATTERN:
        // e.g. ({...rest} = something);
        return parent;

      case COMPUTED_PROP:
        // e.g. ({[expression]: targetNode} = something)
        // e.g. ({[expression]: targetNode = default} = something)
        // make sure the effective target (targetNode or DEFAULT_VALUE containing it)
        // isn't the expression part
        checkState(!targetIsFirstChild, parent);
        // otherwise the same as STRING_KEY so fall through
      case STRING_KEY:
        // e.g. ({parent: targetNode} = something)
        Node grandparent = checkNotNull(parent.getParent(), parent);
        checkState(grandparent.isObjectPattern(), grandparent);
        return grandparent;

      case PARAM_LIST:
        // e.g. `function foo(targetNode) {}`
      case LET:
      case CONST:
      case VAR:
        // non-destructured declarations
        // e.g. `let targetNode = 3;`
        return null;

      case FUNCTION:
      case CLASS:
        // e.g. `function targetNode() {}`
        // e.g. `class targetNode {}`
        checkState(targetIsFirstChild, targetNode);
        return null;

      case FOR_IN:
      case FOR_OF:
      case FOR_AWAIT_OF:
        // e.g. `for ({length} in obj) {}` // targetNode is `{length}`
        // e.g. `for ({length} of obj) {}` // targetNode is `{length}`
        checkState(targetIsFirstChild, targetNode);
        return null;

      case DESTRUCTURING_LHS:
        // destructured declarations
        // e.g. `let [a] = 3`; // targetNode is `[a]`
        checkState(targetIsFirstChild, targetNode);
        return null;

      case IMPORT:
        // e.g. `import targetNode from './foo/bar';`
        return null;

      case IMPORT_SPEC:
        // e.g. `import {bar as targetNode} from './foo/bar';`
        // e.g. `import {targetNode} from './foo/bar';` // AST will have {targetNode as targetNode}
        checkState(!targetIsFirstChild, parent);
        return null;

      case CATCH:
        // e.g. `try {} catch (foo) {}`
        return null;

      default:
        // e.g. targetNode = something
        checkState(isAssignmentOp(parent) && targetIsFirstChild, parent);
        return null;
    }
  }
1216178
@SuppressWarnings("unchecked")
	public TreeNode<T> findByPath(T... path) {
		return findByPath(Arrays.asList(path));
	}
1016263
public void getRelativePath(StringBuilder result, INode parserNode, INode node) {
		if (parserNode == node)
			return;
		if (isAncestor(parserNode, node)) {
			ICompositeNode parent = node.getParent();
			getRelativePath(result, parserNode, parent);
			int idx = 0;
			INode child = parent.getFirstChild();
			while(child != node && child.hasNextSibling()) {
				idx++;
				child = child.getNextSibling();
			}
			result.append("/").append(idx);
		} else {
			result.append("/..");
			getRelativePath(result, parserNode.getParent(), node);
		}
	}
166333
@Override
    public Path getPath(final String first, final String... more) {

        this.checkClosed();

        if (first == null) {
            throw new IllegalArgumentException("At least one path component must be specified");
        }

        final String merged = this.merge(first, more);
        final Path path = new ShrinkWrapPath(merged, this);

        return path;
    }
153214
public LinkedList<PlanNode> getPathTo( PlanNode descendant ) {
        assert descendant != null;
        LinkedList<PlanNode> stack = new LinkedList<PlanNode>();
        PlanNode node = descendant;
        while (node != this) {
            stack.addFirst(node);
            node = node.getParent();
            assert node != null : "The supplied node is not a descendant of this node";
        }
        stack.addFirst(this);
        return stack;
    }
89806
public static Tree getErasedTypeTree(Tree tree) {
    return tree.accept(
        new SimpleTreeVisitor<Tree, Void>() {
          @Override
          public Tree visitIdentifier(IdentifierTree tree, Void unused) {
            return tree;
          }

          @Override
          public Tree visitParameterizedType(ParameterizedTypeTree tree, Void unused) {
            return tree.getType();
          }
        },
        null);
  }
1163251
public static <T> String path(TreeDef.Parented<T> treeDef, T node, Function<? super T, String> toString) {
		return path(treeDef, node, toString, "/");
	}
286979
public INode getNode(final INode node, String path) {
		final List<String> split = Strings.split(path, '/');
		INode result = node;
		for (String string : split) {
			String trimmed = string.trim();
			if (trimmed.length() > 0) {
				if ("..".equals(trimmed)) {
					if (result.getParent() == null)
						throw new IllegalStateException("node has no parent");
					result = result.getParent();
				} else {
					int index = Integer.parseInt(string);
					if (index >= 0) {
						INode child = ((ICompositeNode) result).getFirstChild();
						while(index > 0) {
							child = child.getNextSibling();
							index--;
						}
						result = child;
					}
				}
			}
		}
		return result;
	}
677442
public static <T extends Comparable<?>> ComparablePath<T> comparablePath(Class<? extends T> type,
            Path<?> parent, String property) {
        return new ComparablePath<T>(type, PathMetadataFactory.forProperty(parent, property));
    }
118762
public static UnixPath getPath(boolean permitEmptyComponents, String path) {
    if (path.isEmpty()) {
      return EMPTY_PATH;
    } else if (isRootInternal(path)) {
      return ROOT_PATH;
    } else {
      return new UnixPath(permitEmptyComponents, path);
    }
  }
426955
public static Element getDeclaredElement(TreeNode node) {
    if (node instanceof AbstractTypeDeclaration) {
      return ((AbstractTypeDeclaration) node).getTypeElement();
    } else if (node instanceof MethodDeclaration) {
      return ((MethodDeclaration) node).getExecutableElement();
    } else if (node instanceof VariableDeclaration) {
      return ((VariableDeclaration) node).getVariableElement();
    }
    return null;
  }