use of com.sun.source.util.TreePath in project bazel by bazelbuild.
the class TreeUtils method enclosingOfClass.
/**
* Gets the first enclosing tree in path, of the specified class
*
* @param path the path defining the tree node
* @param treeClass the class of the desired tree
* @return the enclosing tree of the given type as given by the path
*/
public static <T extends Tree> T enclosingOfClass(final TreePath path, final Class<T> treeClass) {
TreePath p = path;
while (p != null) {
Tree leaf = p.getLeaf();
if (treeClass.isInstance(leaf))
return treeClass.cast(leaf);
p = p.getParentPath();
}
return null;
}
use of com.sun.source.util.TreePath in project bazel by bazelbuild.
the class TreeUtils method enclosingTopLevelBlock.
public static /*@Nullable*/
BlockTree enclosingTopLevelBlock(TreePath path) {
TreePath parpath = path.getParentPath();
while (parpath != null && parpath.getLeaf().getKind() != Tree.Kind.CLASS) {
path = parpath;
parpath = parpath.getParentPath();
}
if (path.getLeaf().getKind() == Tree.Kind.BLOCK) {
return (BlockTree) path.getLeaf();
}
return null;
}
use of com.sun.source.util.TreePath in project bazel by bazelbuild.
the class TreeUtils method pathTillOfKind.
/**
* Gets path to the the first enclosing tree with any one of the specified kinds.
*
* @param path the path defining the tree node
* @param kinds the set of kinds of the desired tree
* @return the path to the enclosing tree of the given type
*/
public static TreePath pathTillOfKind(final TreePath path, final Set<Tree.Kind> kinds) {
TreePath p = path;
while (p != null) {
Tree leaf = p.getLeaf();
assert leaf != null;
/*nninvariant*/
if (kinds.contains(leaf.getKind()))
return p;
p = p.getParentPath();
}
return null;
}
use of com.sun.source.util.TreePath in project buck by facebook.
the class TreeBackedTreesTest method testGetTreeNullGetPathRoundtripTypeParameterElement.
@Test
public void testGetTreeNullGetPathRoundtripTypeParameterElement() throws IOException {
compile("class Foo<T, U> { }");
TypeParameterElement tElement = elements.getTypeElement("Foo").getTypeParameters().get(0);
Tree tTree = trees.getTree(tElement);
TreePath tPath = trees.getPath(tElement);
// Odd behavior by javac, but we'll match it
assertNull(tTree);
assertSame(tElement, trees.getElement(tPath));
}
use of com.sun.source.util.TreePath in project j2objc by google.
the class JavadocConverter method convertJavadoc.
/**
* Returns an AST node for the javadoc comment of a specified class,
* method, or field element.
*/
static Javadoc convertJavadoc(Element element, String source, JavacEnvironment env, boolean reportWarnings) {
DocTrees docTrees = DocTrees.instance(env.task());
TreePath path = docTrees.getPath(element);
if (path == null) {
throw new AssertionError("could not find tree path for element");
}
DCTree.DCDocComment docComment = (DCTree.DCDocComment) docTrees.getDocCommentTree(path);
if (docComment == null) {
// Declaration does not have a javadoc comment.
return null;
}
JavadocConverter converter = new JavadocConverter(element, docComment, source, docTrees, path.getCompilationUnit(), reportWarnings);
Javadoc result = new Javadoc();
// First tag has no name.
TagElement newTag = new TagElement();
converter.scan(docComment.getFirstSentence(), newTag);
converter.scan(docComment.getBody(), newTag);
if (!newTag.getFragments().isEmpty()) {
List<TreeNode> fragments = newTag.getFragments();
int start = fragments.get(0).getStartPosition();
TreeNode lastFragment = fragments.get(fragments.size() - 1);
int end = start + lastFragment.getLength();
converter.setPos(newTag, start, end);
result.addTag(newTag);
}
for (DocTree tag : docComment.getBlockTags()) {
if (tag.getKind() != DocTree.Kind.ERRONEOUS) {
newTag = new TagElement();
converter.scan(tag, newTag);
result.addTag(newTag);
}
}
return result;
}
Aggregations