本文整理了Java中com.sun.source.tree.Tree.getKind()
方法的一些代码示例,展示了Tree.getKind()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Tree.getKind()
方法的具体详情如下:
包路径:com.sun.source.tree.Tree
类名称:Tree
方法名:getKind
[英]Gets the kind of this tree.
[中]得到了这棵树的类型。
代码示例来源: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
/** Returns true if all declarations inside the given compilation unit have been visited. */
private boolean finishedCompilation(CompilationUnitTree tree) {
OUTER:
for (Tree decl : tree.getTypeDecls()) {
switch (decl.getKind()) {
case EMPTY_STATEMENT:
// ignore ";" at the top level, which counts as an empty type decl
continue OUTER;
case IMPORT:
// The spec disallows mixing imports and empty top-level declarations (";"), but
// javac has a bug that causes it to accept empty declarations interspersed with imports:
// http://mail.openjdk.java.net/pipermail/compiler-dev/2013-August/006968.html
//
// Any import declarations after the first semi are incorrectly added to the list
// of type declarations, so we have to skip over them here.
continue OUTER;
default:
break;
}
if (!seen.contains(decl)) {
return false;
}
}
return true;
}
}
代码示例来源: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: org.netbeans.modules/org-netbeans-modules-java-editor-base
private void typeUsed(Element decl, TreePath expr, boolean methodInvocation) {
if (decl != null && (expr == null || expr.getLeaf().getKind() == Kind.IDENTIFIER || expr.getLeaf().getKind() == Kind.PARAMETERIZED_TYPE)) {
if (!isErroneous(decl)) {
ImportTree imp = element2Import.get(decl);
if (imp != null) {
addUsage(imp);
if (isStar(imp)) {
//TODO: explain
handleUnresolvableImports(decl, methodInvocation, false);
}
}
} else {
handleUnresolvableImports(decl, methodInvocation, true);
for (Entry<Element, ImportTree> e : element2Import.entrySet()) {
if (importedBySingleImport.contains(e.getKey())) continue;
if (e.getKey().getSimpleName().equals(decl.getSimpleName())) {
import2Highlight.remove(e.getValue());
}
}
}
}
}
代码示例来源:origin: dcaoyuan/nbscala
public void run(CompilationController control) throws Exception {
if (JavaSource.Phase.ELEMENTS_RESOLVED.compareTo(control.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED))<=0) {
Elements elements = control.getElements();
Trees trees = control.getTrees();
Types types = control.getTypes();
TypeElement applet = elements.getTypeElement("java.applet.Applet"); //NOI18N
TypeElement japplet = elements.getTypeElement("javax.swing.JApplet"); //NOI18N
CompilationUnitTree cu = control.getCompilationUnit();
List<? extends Tree> topLevels = cu.getTypeDecls();
for (Tree topLevel : topLevels) {
if (topLevel.getKind() == Tree.Kind.CLASS) {
TypeElement type = (TypeElement) trees.getElement(TreePath.getPath(cu, topLevel));
if (type != null) {
Set<Modifier> modifiers = type.getModifiers();
if (modifiers.contains(Modifier.PUBLIC) &&
((applet != null && types.isSubtype(type.asType(), applet.asType()))
|| (japplet != null && types.isSubtype(type.asType(), japplet.asType())))) {
result[0] = true;
break;
}
}
}
}
}
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-debugger-jpda-projects
Tree tree = ci.getTreeUtilities().pathFor(offset).getLeaf();
if (tree.getKind() == Tree.Kind.METHOD) {
Element el = ci.getTrees().getElement(ci.getTrees().getPath(ci.getCompilationUnit(), tree));
代码示例来源:origin: google/error-prone
path = new TreePath(taskEvent.getCompilationUnit());
verify(seen.add(path.getLeaf()), "Duplicate FLOW event for: %s", taskEvent.getTypeElement());
Context subContext = new SubContext(context);
subContext.put(ErrorProneOptions.class, errorProneOptions);
return;
if (path.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) {
transformer.get().apply(new TreePath(compilation), subContext, countingDescriptionListener);
代码示例来源:origin: org.netbeans.api/org-netbeans-modules-java-source-base
public static <T extends Tree> T importFQNs(WorkingCopy copy, T tree) {
if (tree == null) return null;
TranslateIdentifier ti = new TranslateIdentifier(copy);
//XXX: the TreePath constructed below below depends on javac internals (that elements are attributes of a tree, not a tree path):
ti.scan(tree.getKind() == Kind.COMPILATION_UNIT ? new TreePath((CompilationUnitTree) tree) : new TreePath(new TreePath(copy.getCompilationUnit()), tree), null);
return (T) copy.getTreeUtilities().translate(tree, ti.translateMap);
}
代码示例来源: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
@Override
@Nullable
protected Choice<Unifier> defaultAction(Tree tree, @Nullable Unifier unifier) {
return Choice.condition(allowed().contains(tree.getKind()), unifier)
.thenChoose(unifications(expression(), tree));
}
}
代码示例来源:origin: org.netbeans.api/org-netbeans-modules-java-source-base
public static LinkedList<Diff> reformat(String text, TokenSequence<JavaTokenId> tokens, TreePath path, SourcePositions sp, CodeStyle cs, int rightMargin) {
Pretty pretty = new Pretty(text, tokens, path, sp, cs, 0, text.length(), rightMargin);
pretty.scan(path, null);
CompilationUnitTree cut = (CompilationUnitTree) path.getLeaf();
List<? extends Tree> typeDecls = cut.getTypeDecls();
int size = typeDecls.size();
int cnt = size > 0 && org.netbeans.api.java.source.TreeUtilities.CLASS_TREE_KINDS.contains(typeDecls.get(size - 1).getKind()) ? cs.getBlankLinesAfterClass() : 1;
if (cnt < 1)
cnt = 1;
String s = pretty.getNewlines(cnt);
tokens.moveEnd();
tokens.movePrevious();
if (tokens.token().id() != WHITESPACE)
pretty.diffs.addFirst(new Diff(text.length(), text.length(), s));
else if (!s.contentEquals(tokens.token().text()))
pretty.diffs.addFirst(new Diff(tokens.offset(), tokens.offset() + tokens.token().length(), s));
return pretty.diffs;
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-swingapp
@Override
public void run(CompilationController controller) throws Exception {
controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
for (Tree t: controller.getCompilationUnit().getTypeDecls()) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(t.getKind())) {
ClassTree classT = (ClassTree) t;
if (sourceFile.getName().equals(classT.getSimpleName().toString())) {
TreePath classTPath = controller.getTrees().getPath(controller.getCompilationUnit(), classT);
TypeElement classEl = (TypeElement) controller.getTrees().getElement(classTPath);
result = run(controller, classT, classEl);
return;
}
}
}
}
}
代码示例来源: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: org.netbeans.api/org-netbeans-modules-java-source-base
public TreePath resolve(CompilationInfo compilationInfo) throws IllegalArgumentException {
TreePath p = parent.resolve(compilationInfo);
if (p == null) return null;
List<Tree> children = listChildren(p.getLeaf());
if (index < children.size()) {
Tree t = children.get(index);
if (t.getKind() == kind) {
return new TreePath(p, t);
}
}
return null;
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-java-editor-base
@Override
public Void visitTypeCast(TypeCastTree tree, EnumSet<UseTypes> d) {
Tree expr = tree.getExpression();
if (expr.getKind() == Kind.IDENTIFIER) {
handlePossibleIdentifier(new TreePath(getCurrentPath(), expr), EnumSet.of(UseTypes.READ));
}
Tree cast = tree.getType();
if (cast.getKind() == Kind.IDENTIFIER) {
handlePossibleIdentifier(new TreePath(getCurrentPath(), cast), EnumSet.of(UseTypes.READ));
}
super.visitTypeCast(tree, d);
return null;
}
代码示例来源: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 boolean apply(Tree tree) {
switch (tree.getKind()) {
case CLASS:
return ((ClassTree) tree).getModifiers().getFlags().contains(STATIC);
case METHOD:
return ((MethodTree) tree).getModifiers().getFlags().contains(STATIC);
case VARIABLE:
return ((VariableTree) tree).getModifiers().getFlags().contains(STATIC);
case BLOCK:
return ((BlockTree) tree).isStatic();
case ENUM:
case ANNOTATION_TYPE:
case INTERFACE:
return true;
default:
throw new AssertionError("unknown member type:" + tree.getKind());
}
}
});
代码示例来源:origin: org.netbeans.api/org-netbeans-modules-java-source-base
public static LinkedList<Diff> reformat(CompilationInfo info, TreePath path, CodeStyle cs, int startOffset, int endOffset, boolean templateEdit, int firstLineIndent) {
Pretty pretty = new Pretty(info, path, cs, startOffset, endOffset, templateEdit);
if (pretty.indent >= 0) {
if (firstLineIndent >= 0)
pretty.indent = pretty.lastIndent = firstLineIndent;
pretty.scan(path, null);
}
if (path.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) {
CompilationUnitTree cut = (CompilationUnitTree) path.getLeaf();
List<? extends Tree> typeDecls = cut.getTypeDecls();
int size = typeDecls.size();
int cnt = size > 0 && org.netbeans.api.java.source.TreeUtilities.CLASS_TREE_KINDS.contains(typeDecls.get(size - 1).getKind()) ? cs.getBlankLinesAfterClass() : 1;
if (cnt < 1)
cnt = 1;
String s = pretty.getNewlines(cnt);
pretty.tokens.moveEnd();
pretty.tokens.movePrevious();
if (pretty.tokens.token().id() != WHITESPACE) {
if (!pretty.tokens.token().text().toString().endsWith(s)) {
String text = info.getText();
pretty.diffs.addFirst(new Diff(text.length(), text.length(), s));
}
} else if (!s.contentEquals(pretty.tokens.token().text())) {
pretty.diffs.addFirst(new Diff(pretty.tokens.offset(), pretty.tokens.offset() + pretty.tokens.token().length(), s));
}
}
return pretty.diffs;
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-swingapp
@Override
public void run(CompilationController controller) throws Exception {
controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
for (Tree t: controller.getCompilationUnit().getTypeDecls()) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(t.getKind())) {
ClassTree classT = (ClassTree) t;
if (fileName.equals(classT.getSimpleName().toString())) {
Tree superT = classT.getExtendsClause();
if (superT != null) {
Trees trees = controller.getTrees();
TreePath classTPath = trees.getPath(controller.getCompilationUnit(), classT);
TypeElement classEl = (TypeElement) trees.getElement(classTPath);
TypeElement appEl = controller.getElements().getTypeElement("org.jdesktop.application.Application"); // NOI18N
Types types = controller.getTypes();
TypeMirror tm1 = types.erasure(classEl.asType());
TypeMirror tm2 = types.erasure(appEl.asType());
if (types.isSubtype(tm1, tm2)) {
String clsName = classEl.getQualifiedName().toString();
if (!clsName.startsWith("org.jdesktop.application.")) { // NOI18N
result[0] = clsName;
return;
}
}
break;
}
}
}
}
}
}, true);
代码示例来源:origin: google/error-prone
/** Get the outermost class/interface/enum of an element, or null if none. */
private Type getOutermostClass(VisitorState state) {
TreePath path = state.getPath();
Type type = null;
while (path != null) {
if (path.getLeaf().getKind() == Kind.CLASS
|| path.getLeaf().getKind() == Kind.INTERFACE
|| path.getLeaf().getKind() == Kind.ENUM) {
type = ASTHelpers.getSymbol(path.getLeaf()).type;
}
path = path.getParentPath();
}
return type;
}
内容来源于网络,如有侵权,请联系作者删除!