Class ASTHelpers

java.lang.Object
com.google.errorprone.util.ASTHelpers

public class ASTHelpers
extends Object
This class contains utility methods to work with the javac AST.
  • Nested Class Summary

    Nested Classes
    Modifier and Type Class Description
    static class  ASTHelpers.ScanThrownTypes
    Scanner for determining what types are thrown by a tree.
    static class  ASTHelpers.TargetType
    An expression's target type, see targetType(com.google.errorprone.VisitorState).
  • Constructor Summary

    Constructors
    Constructor Description
    ASTHelpers()  
  • Method Summary

    Modifier and Type Method Description
    static Set<com.sun.tools.javac.util.Name> annotationsAmong​(com.sun.tools.javac.code.Symbol sym, Set<? extends com.sun.tools.javac.util.Name> annotationClasses, VisitorState state)
    Determines which of a set of annotations are present on a symbol.
    static Object constValue​(com.sun.source.tree.Tree tree)
    Returns the compile-time constant value of a tree if it has one, or null.
    static <T> T constValue​(com.sun.source.tree.Tree tree, Class<? extends T> clazz)
    Returns the compile-time constant value of a tree if it is of type clazz, or null.
    static boolean containsComments​(com.sun.source.tree.Tree tree, VisitorState state)
    Returns whether the given tree contains any comments in its source.
    static String createPrivateConstructor​(com.sun.source.tree.ClassTree classTree)
    Returns a no arg private constructor for the ClassTree.
    static com.sun.tools.javac.code.Symbol.ClassSymbol enclosingClass​(com.sun.tools.javac.code.Symbol sym)
    Return the enclosing ClassSymbol of the given symbol, or null.
    static com.sun.tools.javac.code.Symbol.PackageSymbol enclosingPackage​(com.sun.tools.javac.code.Symbol sym)
    Return the enclosing PackageSymbol of the given symbol, or null.
    static LinkedHashSet<String> enumValues​(com.sun.tools.javac.code.Symbol.TypeSymbol enumType)  
    static com.sun.source.tree.ClassTree findClass​(com.sun.tools.javac.code.Symbol.ClassSymbol symbol, VisitorState state)
    Returns the class tree that matches the given symbol within the compilation unit, or null if none was found.
    static <T> T findEnclosingNode​(com.sun.source.util.TreePath path, Class<T> klass)
    Given a TreePath, walks up the tree until it finds a node of the given type.
    static Set<com.sun.tools.javac.code.Symbol.MethodSymbol> findMatchingMethods​(com.sun.tools.javac.util.Name name, com.google.common.base.Predicate<com.sun.tools.javac.code.Symbol.MethodSymbol> predicate, com.sun.tools.javac.code.Type startClass, com.sun.tools.javac.code.Types types)
    Finds all methods in any superclass of startClass with a certain name that match the given predicate.
    static com.sun.source.tree.MethodTree findMethod​(com.sun.tools.javac.code.Symbol.MethodSymbol symbol, VisitorState state)
    Returns the method tree that matches the given symbol within the compilation unit, or null if none was found.
    static <T> com.sun.source.util.TreePath findPathFromEnclosingNodeToTopLevel​(com.sun.source.util.TreePath path, Class<T> klass)
    Given a TreePath, finds the first enclosing node of the given type and returns the path from the enclosing node to the top-level CompilationUnitTree.
    static Optional<com.sun.tools.javac.code.Symbol.MethodSymbol> findSuperMethod​(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol, com.sun.tools.javac.code.Types types)
    Finds (if it exists) first (in the class hierarchy) non-interface super method of given method.
    static com.sun.tools.javac.code.Symbol.MethodSymbol findSuperMethodInType​(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol, com.sun.tools.javac.code.Type superType, com.sun.tools.javac.code.Types types)  
    static Set<com.sun.tools.javac.code.Symbol.MethodSymbol> findSuperMethods​(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol, com.sun.tools.javac.code.Types types)  
    static <T extends Annotation>
    T
    getAnnotation​(com.sun.source.tree.Tree tree, Class<T> annotationClass)
    Deprecated.
    If annotationClass contains a member that is a Class or an array of them, attempting to access that member from the Error Prone checker code will result in a runtime exception.
    static <T extends Annotation>
    T
    getAnnotation​(com.sun.tools.javac.code.Symbol sym, Class<T> annotationClass)
    Deprecated.
    If annotationClass contains a member that is a Class or an array of them, attempting to access that member from the Error Prone checker code will result in a runtime exception.
    static AnnotationMirror getAnnotationMirror​(com.sun.source.tree.AnnotationTree annotationTree)
    Return a mirror of this annotation.
    static String getAnnotationName​(com.sun.source.tree.AnnotationTree tree)  
    static List<com.sun.source.tree.AnnotationTree> getAnnotationsWithSimpleName​(List<? extends com.sun.source.tree.AnnotationTree> annotations, String name)
    Returns a list of AnnotationTree with the given simple name.
    static com.sun.tools.javac.code.TypeAnnotations.AnnotationType getAnnotationType​(com.sun.source.tree.AnnotationTree anno, com.sun.tools.javac.code.Symbol target, VisitorState state)
    Returns whether anno corresponds to a type annotation, or null if it could not be determined.
    static com.sun.source.tree.AnnotationTree getAnnotationWithSimpleName​(List<? extends com.sun.source.tree.AnnotationTree> annotations, String name)
    Returns an AnnotationTree with the given simple name, or null.
    static List<com.sun.source.tree.MethodTree> getConstructors​(com.sun.source.tree.ClassTree classTree)
    Returns the list of all constructors defined in the class (including generated ones).
    static com.google.common.collect.ImmutableList<com.sun.tools.javac.code.Symbol.MethodSymbol> getConstructors​(com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol)
    Returns the list of all constructors defined in the class.
    static Stream<com.sun.tools.javac.code.Attribute.Compound> getDeclarationAndTypeAttributes​(com.sun.tools.javac.code.Symbol sym)
    Returns declaration annotations of the given symbol, as well as 'top-level' type annotations, including : Type annotations of the return type of a method.
    static com.sun.tools.javac.code.Symbol getDeclaredSymbol​(com.sun.source.tree.Tree tree)
    Gets the symbol declared by a tree.
    static com.sun.source.tree.Tree getErasedTypeTree​(com.sun.source.tree.Tree tree)
    Returns the erasure of the given type tree, i.e.
    static String getFileName​(com.sun.source.tree.CompilationUnitTree tree)
    Extract the filename from a CompilationUnitTree, with special handling for jar files.
    static String getFileNameFromUri​(URI uri)
    Extract the filename from the URI, with special handling for jar files.
    static com.google.common.collect.ImmutableSet<String> getGeneratedBy​(VisitorState state)
    Returns the value of the @Generated annotation on enclosing classes, if present.
    static com.google.common.collect.ImmutableSet<String> getGeneratedBy​(com.sun.tools.javac.code.Symbol symbol, VisitorState state)
    Returns the values of the given symbol's javax.annotation.Generated or javax.annotation.processing.Generated annotation, if present.
    static com.sun.source.tree.ModifiersTree getModifiers​(com.sun.source.tree.Tree tree)
    Returns the modifiers tree of the given class, method, or variable declaration.
    static Nullness getNullnessValue​(com.sun.source.tree.ExpressionTree expr, VisitorState state, NullnessAnalysis nullnessAnalysis)
    Returns the Nullness for an expression as determined by the nullness dataflow analysis.
    static com.sun.source.tree.ExpressionTree getReceiver​(com.sun.source.tree.ExpressionTree expressionTree)
    Returns the receiver of an expression.
    static com.sun.tools.javac.code.Type getReceiverType​(com.sun.source.tree.ExpressionTree expressionTree)
    Returns the type of a receiver of a method call expression.
    static com.sun.tools.javac.code.Type getResultType​(com.sun.source.tree.ExpressionTree expressionTree)
    Returns the type that this expression tree will evaluate to.
    static com.sun.tools.javac.code.Type getReturnType​(com.sun.source.tree.ExpressionTree expressionTree)
    Gives the return type of an ExpressionTree that represents a method select.
    static com.sun.source.tree.ExpressionTree getRootAssignable​(com.sun.source.tree.MethodInvocationTree methodInvocationTree)
    Find the root assignable expression of a chain of field accesses.
    static int getStartPosition​(com.sun.source.tree.Tree tree)
    Returns the start position of the node.
    static com.sun.tools.javac.code.Symbol.ClassSymbol getSymbol​(com.sun.source.tree.ClassTree tree)
    Gets the symbol for a class.
    static com.sun.tools.javac.code.Symbol.MethodSymbol getSymbol​(com.sun.source.tree.MemberReferenceTree tree)
    Gets the symbol for a member reference.
    static com.sun.tools.javac.code.Symbol.MethodSymbol getSymbol​(com.sun.source.tree.MethodInvocationTree tree)
    Gets the symbol for a method invocation.
    static com.sun.tools.javac.code.Symbol.MethodSymbol getSymbol​(com.sun.source.tree.MethodTree tree)
    Gets the symbol for a method.
    static com.sun.tools.javac.code.Symbol.MethodSymbol getSymbol​(com.sun.source.tree.NewClassTree tree)
    Gets the method symbol for a new class.
    static com.sun.tools.javac.code.Symbol.PackageSymbol getSymbol​(com.sun.source.tree.PackageTree tree)
    Gets the symbol for a package.
    static com.sun.tools.javac.code.Symbol getSymbol​(com.sun.source.tree.Tree tree)
    Gets the symbol for a tree.
    static com.sun.tools.javac.code.Symbol.VarSymbol getSymbol​(com.sun.source.tree.VariableTree tree)
    Gets the symbol for a variable.
    static com.google.common.collect.ImmutableSet<com.sun.tools.javac.code.Type> getThrownExceptions​(com.sun.source.tree.Tree tree, VisitorState state)
    Returns the exceptions thrown by tree.
    static com.sun.tools.javac.code.Type.ClassType getType​(com.sun.source.tree.ClassTree tree)
    Returns the ClassType for the given type ClassTree or null if the type could not be determined.
    static com.sun.tools.javac.code.Type getType​(com.sun.source.tree.Tree tree)
    Returns the Type of the given tree, or null if the type could not be determined.
    static com.sun.tools.javac.code.Type getUpperBound​(com.sun.tools.javac.code.Type type, com.sun.tools.javac.code.Types types)
    Returns the upper bound of a type if it has one, or the type itself if not.
    static boolean hasAnnotation​(com.sun.source.tree.Tree tree, Class<? extends Annotation> annotationClass, VisitorState state)
    Check for the presence of an annotation, considering annotation inheritance.
    static boolean hasAnnotation​(com.sun.source.tree.Tree tree, String annotationClass, VisitorState state)
    Check for the presence of an annotation, considering annotation inheritance.
    static boolean hasAnnotation​(com.sun.tools.javac.code.Symbol sym, Class<? extends Annotation> annotationClass, VisitorState state)
    Check for the presence of an annotation, considering annotation inheritance.
    static boolean hasAnnotation​(com.sun.tools.javac.code.Symbol sym, String annotationClass, VisitorState state)
    Determines whether a symbol has an annotation of the given type.
    static boolean hasDirectAnnotationWithSimpleName​(com.sun.source.tree.Tree tree, String simpleName)
    Check for the presence of an annotation with a specific simple name directly on this symbol.
    static boolean hasDirectAnnotationWithSimpleName​(com.sun.tools.javac.code.Symbol sym, String simpleName)
    Check for the presence of an annotation with a specific simple name directly on this symbol.
    static boolean inSamePackage​(com.sun.tools.javac.code.Symbol targetSymbol, VisitorState state)
    Return true if the given symbol is defined in the current package.
    static boolean isCastable​(com.sun.tools.javac.code.Type s, com.sun.tools.javac.code.Type t, VisitorState state)
    Returns true if erasure(s) is castable to erasure(t).
    static boolean isCheckedExceptionType​(com.sun.tools.javac.code.Type t, VisitorState state)
    Returns true if t is a subtype of Throwable but not a subtype of RuntimeException or Error.
    static boolean isConsideredFinal​(com.sun.tools.javac.code.Symbol symbol)
    Returns whether symbol is final or effectively final.
    static boolean isGeneratedConstructor​(com.sun.source.tree.MethodTree tree)
    Returns true if the given tree is a generated constructor.
    static boolean isJUnitTestCode​(VisitorState state)
    Returns true if the leaf node in the TreePath from state sits somewhere underneath a class or method that is marked as JUnit 3 or 4 test code.
    static boolean isSameType​(com.sun.tools.javac.code.Type s, com.sun.tools.javac.code.Type t, VisitorState state)
    Returns true if erasure(s) == erasure(t).
    static boolean isSubtype​(com.sun.tools.javac.code.Type s, com.sun.tools.javac.code.Type t, VisitorState state)
    Returns true if erasure(s) <: erasure(t).
    static boolean isSuper​(com.sun.source.tree.Tree tree)  
    static boolean isTestNgTestCode​(VisitorState state)
    Returns true if the leaf node in the TreePath from state sits somewhere underneath a class or method that is marked as TestNG test code.
    static boolean isVoidType​(com.sun.tools.javac.code.Type type, VisitorState state)
    Return true if the given type is 'void' or 'Void'.
    static List<com.sun.source.tree.ExpressionTree> matchBinaryTree​(com.sun.source.tree.BinaryTree tree, List<Matcher<com.sun.source.tree.ExpressionTree>> matchers, VisitorState state)
    Given a BinaryTree to match against and a list of two matchers, applies the matchers to the operands in both orders.
    static Stream<com.sun.tools.javac.code.Symbol.MethodSymbol> matchingMethods​(com.sun.tools.javac.util.Name name, Predicate<com.sun.tools.javac.code.Symbol.MethodSymbol> predicate, com.sun.tools.javac.code.Type startClass, com.sun.tools.javac.code.Types types)
    Finds all methods in any superclass of startClass with a certain name that match the given predicate.
    static boolean methodCanBeOverridden​(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol)
    Determines whether a method can be overridden.
    static com.sun.tools.javac.code.Symbol.ClassSymbol outermostClass​(com.sun.tools.javac.code.Symbol symbol)
    Returns the outermost enclosing owning class, or null.
    static boolean requiresParentheses​(com.sun.source.tree.ExpressionTree expression, VisitorState state)  
    static com.sun.tools.javac.code.Symbol.MethodSymbol resolveExistingMethod​(VisitorState state, com.sun.tools.javac.code.Symbol.TypeSymbol base, com.sun.tools.javac.util.Name name, Iterable<com.sun.tools.javac.code.Type> argTypes, Iterable<com.sun.tools.javac.code.Type> tyargTypes)
    Given a Type (base), find the method named name, with the appropriate argTypes and tyargTypes and return its MethodSymbol.
    static boolean sameVariable​(com.sun.source.tree.ExpressionTree expr1, com.sun.source.tree.ExpressionTree expr2)
    Determines whether two expressions refer to the same variable.
    static com.sun.source.tree.ExpressionTree stripParentheses​(com.sun.source.tree.ExpressionTree tree)
    Given an ExpressionTree, removes any enclosing parentheses.
    static com.sun.source.tree.Tree stripParentheses​(com.sun.source.tree.Tree tree)
    Removes any enclosing parentheses from the tree.
    static ASTHelpers.TargetType targetType​(VisitorState state)
    Returns the target type of the tree at the given VisitorState's path, or else null.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ASTHelpers

      public ASTHelpers()
  • Method Details

    • sameVariable

      public static boolean sameVariable​(com.sun.source.tree.ExpressionTree expr1, com.sun.source.tree.ExpressionTree expr2)
      Determines whether two expressions refer to the same variable. Note that returning false doesn't necessarily mean the expressions do *not* refer to the same field. We don't attempt to do any complex analysis here, just catch the obvious cases.
    • getDeclaredSymbol

      @Nullable public static com.sun.tools.javac.code.Symbol getDeclaredSymbol​(com.sun.source.tree.Tree tree)
      Gets the symbol declared by a tree. Returns null if tree does not declare a symbol or is null.
    • getSymbol

      public static com.sun.tools.javac.code.Symbol getSymbol​(com.sun.source.tree.Tree tree)
      Gets the symbol for a tree. Returns null if this tree does not have a symbol because it is of the wrong type, if tree is null, or if the symbol cannot be found due to a compilation error.
    • getSymbol

      public static com.sun.tools.javac.code.Symbol.ClassSymbol getSymbol​(com.sun.source.tree.ClassTree tree)
      Gets the symbol for a class.
    • getSymbol

      public static com.sun.tools.javac.code.Symbol.PackageSymbol getSymbol​(com.sun.source.tree.PackageTree tree)
      Gets the symbol for a package.
    • getSymbol

      public static com.sun.tools.javac.code.Symbol.MethodSymbol getSymbol​(com.sun.source.tree.MethodTree tree)
      Gets the symbol for a method.
    • getSymbol

      @Nullable public static com.sun.tools.javac.code.Symbol.MethodSymbol getSymbol​(com.sun.source.tree.NewClassTree tree)
      Gets the method symbol for a new class.
    • getSymbol

      public static com.sun.tools.javac.code.Symbol.VarSymbol getSymbol​(com.sun.source.tree.VariableTree tree)
      Gets the symbol for a variable.
    • getSymbol

      @Nullable public static com.sun.tools.javac.code.Symbol.MethodSymbol getSymbol​(com.sun.source.tree.MethodInvocationTree tree)
      Gets the symbol for a method invocation.
    • getSymbol

      @Nullable public static com.sun.tools.javac.code.Symbol.MethodSymbol getSymbol​(com.sun.source.tree.MemberReferenceTree tree)
      Gets the symbol for a member reference.
    • requiresParentheses

      public static boolean requiresParentheses​(com.sun.source.tree.ExpressionTree expression, VisitorState state)
    • stripParentheses

      public static com.sun.source.tree.Tree stripParentheses​(com.sun.source.tree.Tree tree)
      Removes any enclosing parentheses from the tree.
    • stripParentheses

      public static com.sun.source.tree.ExpressionTree stripParentheses​(com.sun.source.tree.ExpressionTree tree)
      Given an ExpressionTree, removes any enclosing parentheses.
    • findPathFromEnclosingNodeToTopLevel

      public static <T> com.sun.source.util.TreePath findPathFromEnclosingNodeToTopLevel​(com.sun.source.util.TreePath path, Class<T> klass)
      Given a TreePath, finds the first enclosing node of the given type and returns the path from the enclosing node to the top-level CompilationUnitTree.
    • findEnclosingNode

      @Nullable public static <T> T findEnclosingNode​(com.sun.source.util.TreePath path, Class<T> klass)
      Given a TreePath, walks up the tree until it finds a node of the given type. Returns null if no such node is found.
    • getRootAssignable

      @Nullable public static com.sun.source.tree.ExpressionTree getRootAssignable​(com.sun.source.tree.MethodInvocationTree methodInvocationTree)
      Find the root assignable expression of a chain of field accesses. If there is no root (i.e, a bare method call or a static method call), return null.

      Examples:

      
       a.trim().intern() ==> a
       a.b.trim().intern() ==> a.b
       this.intValue.foo() ==> this.intValue
       this.foo() ==> this
       intern() ==> null
       String.format() ==> null
       java.lang.String.format() ==> null
       
    • getReturnType

      public static com.sun.tools.javac.code.Type getReturnType​(com.sun.source.tree.ExpressionTree expressionTree)
      Gives the return type of an ExpressionTree that represents a method select.

      TODO(eaftan): Are there other places this could be used?

    • getResultType

      @Nullable public static com.sun.tools.javac.code.Type getResultType​(com.sun.source.tree.ExpressionTree expressionTree)
      Returns the type that this expression tree will evaluate to. If its a literal, an identifier, or a member select this is the actual type, if it's a method invocation then it's the return type of the method (after instantiating generic types), if it's a constructor then it's the type of the returned class.

      TODO(andrewrice) consider replacing getReturnType with this method

      Parameters:
      expressionTree - the tree to evaluate
      Returns:
      the result type of this tree or null if unable to resolve it
    • getReceiverType

      public static com.sun.tools.javac.code.Type getReceiverType​(com.sun.source.tree.ExpressionTree expressionTree)
      Returns the type of a receiver of a method call expression. Precondition: the expressionTree corresponds to a method call.

      Examples:

      
       a.b.foo() ==> type of a.b
       a.bar().foo() ==> type of a.bar()
       this.foo() ==> type of this
       foo() ==> type of this
       TheClass.aStaticMethod() ==> TheClass
       aStaticMethod() ==> type of class in which method is defined
       
    • getReceiver

      @Nullable public static com.sun.source.tree.ExpressionTree getReceiver​(com.sun.source.tree.ExpressionTree expressionTree)
      Returns the receiver of an expression.

      Examples:

      
       a.foo() ==> a
       a.b.foo() ==> a.b
       a.bar().foo() ==> a.bar()
       a.b.c ==> a.b
       a.b().c ==> a.b()
       this.foo() ==> this
       foo() ==> null
       TheClass.aStaticMethod() ==> TheClass
       aStaticMethod() ==> null
       aStaticallyImportedMethod() ==> null
       
    • matchBinaryTree

      @Nullable public static List<com.sun.source.tree.ExpressionTree> matchBinaryTree​(com.sun.source.tree.BinaryTree tree, List<Matcher<com.sun.source.tree.ExpressionTree>> matchers, VisitorState state)
      Given a BinaryTree to match against and a list of two matchers, applies the matchers to the operands in both orders. If both matchers match, returns a list with the operand that matched each matcher in the corresponding position.
      Parameters:
      tree - a BinaryTree AST node
      matchers - a list of matchers
      state - the VisitorState
      Returns:
      a list of matched operands, or null if at least one did not match
    • findMethod

      @Nullable public static com.sun.source.tree.MethodTree findMethod​(com.sun.tools.javac.code.Symbol.MethodSymbol symbol, VisitorState state)
      Returns the method tree that matches the given symbol within the compilation unit, or null if none was found.
    • findClass

      @Nullable public static com.sun.source.tree.ClassTree findClass​(com.sun.tools.javac.code.Symbol.ClassSymbol symbol, VisitorState state)
      Returns the class tree that matches the given symbol within the compilation unit, or null if none was found.
    • findSuperMethodInType

      @Nullable public static com.sun.tools.javac.code.Symbol.MethodSymbol findSuperMethodInType​(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol, com.sun.tools.javac.code.Type superType, com.sun.tools.javac.code.Types types)
    • findSuperMethods

      public static Set<com.sun.tools.javac.code.Symbol.MethodSymbol> findSuperMethods​(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol, com.sun.tools.javac.code.Types types)
    • findSuperMethod

      public static Optional<com.sun.tools.javac.code.Symbol.MethodSymbol> findSuperMethod​(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol, com.sun.tools.javac.code.Types types)
      Finds (if it exists) first (in the class hierarchy) non-interface super method of given method.
    • matchingMethods

      public static Stream<com.sun.tools.javac.code.Symbol.MethodSymbol> matchingMethods​(com.sun.tools.javac.util.Name name, Predicate<com.sun.tools.javac.code.Symbol.MethodSymbol> predicate, com.sun.tools.javac.code.Type startClass, com.sun.tools.javac.code.Types types)
      Finds all methods in any superclass of startClass with a certain name that match the given predicate.
      Returns:
      The (possibly empty) list of methods in any superclass that match predicate and have the given name. Results are returned least-abstract first, i.e., starting in the startClass itself, progressing through its superclasses, and finally interfaces in an unspecified order.
    • findMatchingMethods

      public static Set<com.sun.tools.javac.code.Symbol.MethodSymbol> findMatchingMethods​(com.sun.tools.javac.util.Name name, com.google.common.base.Predicate<com.sun.tools.javac.code.Symbol.MethodSymbol> predicate, com.sun.tools.javac.code.Type startClass, com.sun.tools.javac.code.Types types)
      Finds all methods in any superclass of startClass with a certain name that match the given predicate.
      Returns:
      The (possibly empty) set of methods in any superclass that match predicate and have the given name. The set's iteration order will be the same as the order documented in matchingMethods(Name, java.util.function.Predicate, Type, Types).
    • methodCanBeOverridden

      public static boolean methodCanBeOverridden​(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol)
      Determines whether a method can be overridden.
      Returns:
      true if the method can be overridden.
    • hasAnnotation

      public static boolean hasAnnotation​(com.sun.tools.javac.code.Symbol sym, String annotationClass, VisitorState state)
      Determines whether a symbol has an annotation of the given type. This includes annotations inherited from superclasses due to @Inherited.
      Parameters:
      annotationClass - the binary class name of the annotation (e.g. "javax.annotation.Nullable", or "some.package.OuterClassName$InnerClassName")
      Returns:
      true if the symbol is annotated with given type.
    • annotationsAmong

      public static Set<com.sun.tools.javac.util.Name> annotationsAmong​(com.sun.tools.javac.code.Symbol sym, Set<? extends com.sun.tools.javac.util.Name> annotationClasses, VisitorState state)
      Determines which of a set of annotations are present on a symbol.
      Parameters:
      sym - The symbol to inspect for annotations
      annotationClasses - The annotations of interest to look for, Each name must be in binary form, e.g. "com.google.Foo$Bar", not "com.google.Foo.Bar".
      Returns:
      A possibly-empty set of annotations present on the queried element.
    • hasAnnotation

      public static boolean hasAnnotation​(com.sun.tools.javac.code.Symbol sym, Class<? extends Annotation> annotationClass, VisitorState state)
      Check for the presence of an annotation, considering annotation inheritance.
      Returns:
      true if the symbol is annotated with given type.
    • hasAnnotation

      public static boolean hasAnnotation​(com.sun.source.tree.Tree tree, String annotationClass, VisitorState state)
      Check for the presence of an annotation, considering annotation inheritance.
      Parameters:
      annotationClass - the binary class name of the annotation (e.g. "javax.annotation.Nullable", or "some.package.OuterClassName$InnerClassName")
      Returns:
      true if the tree is annotated with given type.
    • hasAnnotation

      public static boolean hasAnnotation​(com.sun.source.tree.Tree tree, Class<? extends Annotation> annotationClass, VisitorState state)
      Check for the presence of an annotation, considering annotation inheritance.
      Returns:
      true if the tree is annotated with given type.
    • hasDirectAnnotationWithSimpleName

      public static boolean hasDirectAnnotationWithSimpleName​(com.sun.tools.javac.code.Symbol sym, String simpleName)
      Check for the presence of an annotation with a specific simple name directly on this symbol. Does *not* consider annotation inheritance.
      Parameters:
      sym - the symbol to check for the presence of the annotation
      simpleName - the simple name of the annotation to look for, e.g. "Nullable" or "CheckReturnValue"
    • hasDirectAnnotationWithSimpleName

      public static boolean hasDirectAnnotationWithSimpleName​(com.sun.source.tree.Tree tree, String simpleName)
      Check for the presence of an annotation with a specific simple name directly on this symbol. Does *not* consider annotation inheritance.
      Parameters:
      tree - the tree to check for the presence of the annotation
      simpleName - the simple name of the annotation to look for, e.g. "Nullable" or "CheckReturnValue"
    • getAnnotation

      @Nullable @Deprecated public static <T extends Annotation> T getAnnotation​(com.sun.source.tree.Tree tree, Class<T> annotationClass)
      Deprecated.
      If annotationClass contains a member that is a Class or an array of them, attempting to access that member from the Error Prone checker code will result in a runtime exception. Instead, operate on sym.getAnnotationMirrors() to meta-syntactically inspect the annotation.
      Retrieves an annotation, considering annotation inheritance.
    • getAnnotation

      @Nullable @Deprecated public static <T extends Annotation> T getAnnotation​(com.sun.tools.javac.code.Symbol sym, Class<T> annotationClass)
      Deprecated.
      If annotationClass contains a member that is a Class or an array of them, attempting to access that member from the Error Prone checker code will result in a runtime exception. Instead, operate on sym.getAnnotationMirrors() to meta-syntactically inspect the annotation.
      Retrieves an annotation, considering annotation inheritance.
    • enumValues

      public static LinkedHashSet<String> enumValues​(com.sun.tools.javac.code.Symbol.TypeSymbol enumType)
      Returns:
      all values of the given enum type, in declaration order.
    • isGeneratedConstructor

      public static boolean isGeneratedConstructor​(com.sun.source.tree.MethodTree tree)
      Returns true if the given tree is a generated constructor. *
    • getConstructors

      public static List<com.sun.source.tree.MethodTree> getConstructors​(com.sun.source.tree.ClassTree classTree)
      Returns the list of all constructors defined in the class (including generated ones).
    • getConstructors

      public static com.google.common.collect.ImmutableList<com.sun.tools.javac.code.Symbol.MethodSymbol> getConstructors​(com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol)
      Returns the list of all constructors defined in the class.
    • getType

      @Nullable public static com.sun.tools.javac.code.Type getType​(@Nullable com.sun.source.tree.Tree tree)
      Returns the Type of the given tree, or null if the type could not be determined.
    • getType

      @Nullable public static com.sun.tools.javac.code.Type.ClassType getType​(@Nullable com.sun.source.tree.ClassTree tree)
      Returns the ClassType for the given type ClassTree or null if the type could not be determined.
    • getAnnotationName

      @Nullable public static String getAnnotationName​(com.sun.source.tree.AnnotationTree tree)
    • getErasedTypeTree

      public static com.sun.source.tree.Tree getErasedTypeTree​(com.sun.source.tree.Tree tree)
      Returns the erasure of the given type tree, i.e. List for List<Foo>.
    • enclosingClass

      public static com.sun.tools.javac.code.Symbol.ClassSymbol enclosingClass​(com.sun.tools.javac.code.Symbol sym)
      Return the enclosing ClassSymbol of the given symbol, or null.
    • enclosingPackage

      public static com.sun.tools.javac.code.Symbol.PackageSymbol enclosingPackage​(com.sun.tools.javac.code.Symbol sym)
      Return the enclosing PackageSymbol of the given symbol, or null.
    • inSamePackage

      public static boolean inSamePackage​(com.sun.tools.javac.code.Symbol targetSymbol, VisitorState state)
      Return true if the given symbol is defined in the current package.
    • getNullnessValue

      public static Nullness getNullnessValue​(com.sun.source.tree.ExpressionTree expr, VisitorState state, NullnessAnalysis nullnessAnalysis)
      Returns the Nullness for an expression as determined by the nullness dataflow analysis.
    • constValue

      @Nullable public static Object constValue​(com.sun.source.tree.Tree tree)
      Returns the compile-time constant value of a tree if it has one, or null.
    • constValue

      @Nullable public static <T> T constValue​(com.sun.source.tree.Tree tree, Class<? extends T> clazz)
      Returns the compile-time constant value of a tree if it is of type clazz, or null.
    • isVoidType

      public static boolean isVoidType​(com.sun.tools.javac.code.Type type, VisitorState state)
      Return true if the given type is 'void' or 'Void'.
    • isSubtype

      public static boolean isSubtype​(com.sun.tools.javac.code.Type s, com.sun.tools.javac.code.Type t, VisitorState state)
      Returns true if erasure(s) <: erasure(t).
    • isCheckedExceptionType

      public static boolean isCheckedExceptionType​(com.sun.tools.javac.code.Type t, VisitorState state)
      Returns true if t is a subtype of Throwable but not a subtype of RuntimeException or Error.
    • isCastable

      public static boolean isCastable​(com.sun.tools.javac.code.Type s, com.sun.tools.javac.code.Type t, VisitorState state)
      Returns true if erasure(s) is castable to erasure(t).
    • isSameType

      public static boolean isSameType​(com.sun.tools.javac.code.Type s, com.sun.tools.javac.code.Type t, VisitorState state)
      Returns true if erasure(s) == erasure(t).
    • getModifiers

      @Nullable public static com.sun.source.tree.ModifiersTree getModifiers​(com.sun.source.tree.Tree tree)
      Returns the modifiers tree of the given class, method, or variable declaration.
    • getUpperBound

      public static com.sun.tools.javac.code.Type getUpperBound​(com.sun.tools.javac.code.Type type, com.sun.tools.javac.code.Types types)
      Returns the upper bound of a type if it has one, or the type itself if not. Correctly handles wildcards and capture variables.
    • isJUnitTestCode

      public static boolean isJUnitTestCode​(VisitorState state)
      Returns true if the leaf node in the TreePath from state sits somewhere underneath a class or method that is marked as JUnit 3 or 4 test code.
    • isTestNgTestCode

      public static boolean isTestNgTestCode​(VisitorState state)
      Returns true if the leaf node in the TreePath from state sits somewhere underneath a class or method that is marked as TestNG test code.
    • getAnnotationWithSimpleName

      @Nullable public static com.sun.source.tree.AnnotationTree getAnnotationWithSimpleName​(List<? extends com.sun.source.tree.AnnotationTree> annotations, String name)
      Returns an AnnotationTree with the given simple name, or null.
    • getAnnotationsWithSimpleName

      public static List<com.sun.source.tree.AnnotationTree> getAnnotationsWithSimpleName​(List<? extends com.sun.source.tree.AnnotationTree> annotations, String name)
      Returns a list of AnnotationTree with the given simple name. This is useful for Repeatable annotations
    • getAnnotationType

      @Nullable public static com.sun.tools.javac.code.TypeAnnotations.AnnotationType getAnnotationType​(com.sun.source.tree.AnnotationTree anno, @Nullable com.sun.tools.javac.code.Symbol target, VisitorState state)
      Returns whether anno corresponds to a type annotation, or null if it could not be determined.
    • getFileName

      @Nullable public static String getFileName​(com.sun.source.tree.CompilationUnitTree tree)
      Extract the filename from a CompilationUnitTree, with special handling for jar files. The return value is normalized to always use '/' to separate elements of the path and to always have a leading '/'.
    • getFileNameFromUri

      @Nullable public static String getFileNameFromUri​(URI uri)
      Extract the filename from the URI, with special handling for jar files. The return value is normalized to always use '/' to separate elements of the path and to always have a leading '/'.
    • resolveExistingMethod

      @Nullable public static com.sun.tools.javac.code.Symbol.MethodSymbol resolveExistingMethod​(VisitorState state, com.sun.tools.javac.code.Symbol.TypeSymbol base, com.sun.tools.javac.util.Name name, Iterable<com.sun.tools.javac.code.Type> argTypes, Iterable<com.sun.tools.javac.code.Type> tyargTypes)
      Given a Type (base), find the method named name, with the appropriate argTypes and tyargTypes and return its MethodSymbol.

      Ex:

      
       .....
       class A {}
       class B {
         public int hashCode() { return 42; }
       }
       .....
      
       MethodSymbol meth =  ASTHelpers.resolveExistingMethod(
          state,
          symbol,
          state.getName("hashCode"),
          ImmutableList.<Type>of(),
          ImmutableList.<Type>of());
       
      meth could be different MethodSymbol's depending on whether symbol represented B or A. (B's hashCode method or Object#hashCode).
      Returns:
      a MethodSymbol representing the method symbol resolved from the context of this type, or null if the method could not be resolved.
    • getGeneratedBy

      public static com.google.common.collect.ImmutableSet<String> getGeneratedBy​(VisitorState state)
      Returns the value of the @Generated annotation on enclosing classes, if present.

      Although @Generated can be applied to non-class program elements, there are no known cases of that happening, so it isn't supported here.

    • getGeneratedBy

      public static com.google.common.collect.ImmutableSet<String> getGeneratedBy​(com.sun.tools.javac.code.Symbol symbol, VisitorState state)
      Returns the values of the given symbol's javax.annotation.Generated or javax.annotation.processing.Generated annotation, if present.
    • isSuper

      public static boolean isSuper​(com.sun.source.tree.Tree tree)
    • targetType

      @Nullable public static ASTHelpers.TargetType targetType​(VisitorState state)
      Returns the target type of the tree at the given VisitorState's path, or else null.

      For example, the target type of an assignment expression is the variable's type, and the target type of a return statement is the enclosing method's type.

    • getDeclarationAndTypeAttributes

      public static Stream<com.sun.tools.javac.code.Attribute.Compound> getDeclarationAndTypeAttributes​(com.sun.tools.javac.code.Symbol sym)
      Returns declaration annotations of the given symbol, as well as 'top-level' type annotations, including :
      • Type annotations of the return type of a method.
      • Type annotations on the type of a formal parameter or field.

      One might expect this to be equivalent to information returned by Type.getAnnotationMirrors(), but javac doesn't associate type annotation information with types for symbols completed from class files, so that approach doesn't work across compilation boundaries.

    • getAnnotationMirror

      public static AnnotationMirror getAnnotationMirror​(com.sun.source.tree.AnnotationTree annotationTree)
      Return a mirror of this annotation.
      Returns:
      an AnnotationMirror for the annotation represented by annotationTree.
    • containsComments

      public static boolean containsComments​(com.sun.source.tree.Tree tree, VisitorState state)
      Returns whether the given tree contains any comments in its source.
    • outermostClass

      @Nullable public static com.sun.tools.javac.code.Symbol.ClassSymbol outermostClass​(com.sun.tools.javac.code.Symbol symbol)
      Returns the outermost enclosing owning class, or null. Doesn't crash on symbols that aren't containing in a package, unlike Symbol.outermostClass() (see b/123431414).
    • isConsideredFinal

      public static boolean isConsideredFinal​(com.sun.tools.javac.code.Symbol symbol)
      Returns whether symbol is final or effectively final.
    • getThrownExceptions

      public static com.google.common.collect.ImmutableSet<com.sun.tools.javac.code.Type> getThrownExceptions​(com.sun.source.tree.Tree tree, VisitorState state)
      Returns the exceptions thrown by tree.
    • getStartPosition

      public static int getStartPosition​(com.sun.source.tree.Tree tree)
      Returns the start position of the node.
    • createPrivateConstructor

      public static String createPrivateConstructor​(com.sun.source.tree.ClassTree classTree)
      Returns a no arg private constructor for the ClassTree.