Skip to content

Commit

Permalink
ASTHelpers: add getAnnotations method, to allow extraction of annotat…
Browse files Browse the repository at this point in the history
…ions from annotatable Trees without a ModifierTree (e.g. TypeParameterTree)

PiperOrigin-RevId: 405887249
  • Loading branch information
awturner authored and Error Prone Team committed Oct 27, 2021
1 parent cdfa8b8 commit f91fff5
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.google.errorprone.matchers.TestNgMatchers;
import com.google.errorprone.suppliers.Supplier;
import com.google.errorprone.suppliers.Suppliers;
import com.sun.source.tree.AnnotatedTypeTree;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ArrayAccessTree;
import com.sun.source.tree.AssertTree;
Expand All @@ -70,6 +71,7 @@
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.ModuleTree;
import com.sun.source.tree.NewArrayTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.PackageTree;
Expand Down Expand Up @@ -1168,13 +1170,41 @@ public static boolean isSameType(Type s, Type t, VisitorState state) {
public static ModifiersTree getModifiers(Tree tree) {
if (tree instanceof ClassTree) {
return ((ClassTree) tree).getModifiers();
} else if (tree instanceof MethodTree) {
}
if (tree instanceof MethodTree) {
return ((MethodTree) tree).getModifiers();
} else if (tree instanceof VariableTree) {
}
if (tree instanceof VariableTree) {
return ((VariableTree) tree).getModifiers();
} else {
return null;
}
if (tree instanceof ModifiersTree) {
return (ModifiersTree) tree;
}
return null;
}

/** Returns the annotations of the given tree, or an empty list. */
public static List<? extends AnnotationTree> getAnnotations(Tree tree) {
if (tree instanceof TypeParameterTree) {
return ((TypeParameterTree) tree).getAnnotations();
}
if (tree instanceof ModuleTree) {
return ((ModuleTree) tree).getAnnotations();
}
if (tree instanceof PackageTree) {
return ((PackageTree) tree).getAnnotations();
}
if (tree instanceof NewArrayTree) {
return ((NewArrayTree) tree).getAnnotations();
}
if (tree instanceof AnnotatedTypeTree) {
return ((AnnotatedTypeTree) tree).getAnnotations();
}
if (tree instanceof ModifiersTree) {
return ((ModifiersTree) tree).getAnnotations();
}
ModifiersTree modifiersTree = getModifiers(tree);
return modifiersTree == null ? ImmutableList.of() : modifiersTree.getAnnotations();
}

/**
Expand Down

0 comments on commit f91fff5

Please sign in to comment.