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 final class 
    Scanner for determining what types are thrown by a tree.
    static class 
    An expression's target type, see targetType(com.google.errorprone.VisitorState).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
     
  • 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 EnumSet<com.sun.tools.javac.code.Flags.Flag>
    asFlagSet(long flags)
     
    static boolean
    canBeRemoved(com.sun.tools.javac.code.Symbol.ClassSymbol symbol)
    static boolean
    canBeRemoved(com.sun.tools.javac.code.Symbol.VarSymbol symbol)
    static boolean
    canBeRemoved(com.sun.tools.javac.code.Symbol symbol, VisitorState state)
    Returns whether this symbol is safe to remove.
    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 Stream<com.sun.tools.javac.code.Symbol>
    enclosingElements(com.sun.tools.javac.code.Symbol sym)
    Returns a stream of the owner hierarchy starting from sym, as described by Symbol.owner.
    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.
    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 com.sun.source.tree.MethodTree
    Finds the enclosing MethodTree.
    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 com.google.common.collect.ImmutableSet<com.sun.tools.javac.code.Symbol.MethodSymbol>
    findMatchingMethods(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 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)
    Finds supermethods of methodSymbol, not including methodSymbol itself, and including interfaces.
    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.
    getAnnotationMirror(com.sun.source.tree.AnnotationTree annotationTree)
    Return a mirror of this annotation.
    static String
    getAnnotationName(com.sun.source.tree.AnnotationTree tree)
     
    static List<? extends com.sun.source.tree.AnnotationTree>
    getAnnotations(com.sun.source.tree.Tree tree)
    Returns the annotations of the given tree, or an empty list.
    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 Stream<? extends com.sun.source.tree.ExpressionTree>
    getCaseExpressions(com.sun.source.tree.CaseTree caseTree)
    Retrieves a stream containing all case expressions, in order, for a given CaseTree.
    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 List<com.sun.tools.javac.code.Symbol>
    getEnclosedElements(com.sun.tools.javac.code.Symbol symbol)
    A wrapper for Symbol.getEnclosedElements() to avoid binary compatibility issues for covariant overrides in subtypes of Symbol.
    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
    Extract the filename from the URI, with special handling for jar files.
    static com.google.common.collect.ImmutableSet<String>
    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 Generated annotations, 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 Optional<? extends com.sun.source.tree.CaseTree>
    getSwitchDefault(com.sun.source.tree.SwitchTree switchTree)
     
    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.google.common.collect.ImmutableListMultimap<com.sun.tools.javac.code.Symbol.TypeVariableSymbol,com.sun.tools.javac.code.Type>
    getTypeSubstitution(com.sun.tools.javac.code.Type type, com.sun.tools.javac.code.Symbol sym)
    Returns the mapping between type variables and their instantiations in the given type.
    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)
    Deprecated.
    prefer hasAnnotation(Symbol, String, VisitorState) to avoid needing a runtime dependency on the annotation class, and to prevent issues if there is skew between the definition of the annotation on the runtime and compile-time classpaths
    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)
    Deprecated.
    prefer hasAnnotation(Symbol, String, VisitorState) to avoid needing a runtime dependency on the annotation class, and to prevent issues if there is skew between the definition of the annotation on the runtime and compile-time classpaths
    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.MethodSymbol sym, String simpleName)
     
    static boolean
    hasDirectAnnotationWithSimpleName(com.sun.tools.javac.code.Symbol.VarSymbol sym, String simpleName)
     
    static boolean
    hasDirectAnnotationWithSimpleName(com.sun.tools.javac.code.Symbol sym, String simpleName)
    Check for the presence of an annotation with the given simple name directly on this symbol or its type.
    static boolean
    hasExplicitSource(com.sun.source.tree.Tree tree, VisitorState state)
    Returns whether the given tree has an explicit source code representation.
    static boolean
    hasImplicitType(com.sun.source.tree.VariableTree tree, VisitorState state)
    Returns whether this is a var or a lambda parameter that has no explicit type.
    static boolean
    hasNoExplicitType(com.sun.source.tree.VariableTree tree, VisitorState state)
    static boolean
    hasOverloadWithOnlyOneParameter(com.sun.tools.javac.code.Symbol.MethodSymbol existingMethod, com.sun.tools.javac.util.Name targetMethodName, com.sun.tools.javac.code.Type onlyParameterType, VisitorState state)
    Returns whether existingMethod has an overload (or "nearly" an overload) with the given targetMethodName, and only a single parameter of type onlyParameterType.
    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
    isAbstract(com.sun.tools.javac.code.Symbol.MethodSymbol method)
    Returns true if the given method symbol is abstract.
    static boolean
    Returns true if the code is in a BugChecker class.
    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
    isEffectivelyPrivate(com.sun.tools.javac.code.Symbol symbol)
    Returns whether this symbol or any of its owners are private.
    static boolean
    isGeneratedConstructor(com.sun.source.tree.MethodTree tree)
    Returns true if the given tree is a generated constructor.
    static boolean
    Attempts to detect whether we're in a static-initializer-like context: that includes direct assignments to static fields, assignments to enum fields, being contained within an expression which is ultimately assigned to a static field.
    static boolean
    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
    isKotlin(com.sun.tools.javac.code.Symbol symbol, VisitorState state)
    Returns true if this symbol was declared in Kotlin source.
    static boolean
    isLocal(com.sun.tools.javac.code.Symbol symbol)
    Returns true if the symbol is directly or indirectly local to a method or variable initializer; see Symbol#isLocal or Symbol#isDirectlyOrIndirectlyLocal.
    static boolean
    isRecord(com.sun.tools.javac.code.Symbol symbol)
    Returns whether the given Symbol is a record, a record's canonical constructor or a member that is part of a record's state vector.
    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
    isStatic(com.sun.tools.javac.code.Symbol symbol)
    Returns true if the symbol is static.
    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
    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 boolean
    methodIsPublicAndNotAnOverride(com.sun.tools.javac.code.Symbol.MethodSymbol method, VisitorState state)
    Returns true if the given method symbol is public (both the method and the enclosing class) and does not have a super-method (i.e., it is not an @Override).
    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)
    Checks whether an expression requires parentheses.
    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.
    scope(com.sun.tools.javac.code.Scope scope)
    Returns a compatibility adapter around Scope.
    static boolean
    shouldKeep(com.sun.source.tree.Tree tree)
    Returns true if any of the given tree is a declaration annotated with an annotation with the simple name @UsedReflectively or @Keep, or any annotations meta-annotated with an annotation with that simple name.
    static Stream<com.sun.source.tree.ExpressionTree>
    streamReceivers(com.sun.source.tree.ExpressionTree tree)
    Returns a Stream of ExpressionTrees resulting from calling getReceiver(ExpressionTree) repeatedly until no receiver exists.
    static Stream<com.sun.tools.javac.code.Symbol.MethodSymbol>
    streamSuperMethods(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol, com.sun.tools.javac.code.Types types)
    static boolean
    stringContainsComments(CharSequence source, com.sun.tools.javac.util.Context context)
    Returns true if the given source code contains comments.
    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.
    Returns the target type of the tree at the given VisitorState's path, or else null.
    static boolean
    variableIsStaticFinal(com.sun.tools.javac.code.Symbol.VarSymbol var)
    Whether the variable is (or should be regarded as) static final.

    Methods inherited from class java.lang.Object

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

  • 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

      @Nullable 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

      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

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

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

      public static boolean canBeRemoved(com.sun.tools.javac.code.Symbol symbol, VisitorState state)
      Returns whether this symbol is safe to remove. That is, if it cannot be accessed from outside its own compilation unit.

      For variables this just means that one of the enclosing elements is private; for methods, it also means that this symbol is not an override.

    • canBeRemoved

      public static boolean canBeRemoved(com.sun.tools.javac.code.Symbol.VarSymbol symbol)
    • canBeRemoved

      public static boolean canBeRemoved(com.sun.tools.javac.code.Symbol.ClassSymbol symbol)
    • isEffectivelyPrivate

      public static boolean isEffectivelyPrivate(com.sun.tools.javac.code.Symbol symbol)
      Returns whether this symbol or any of its owners are private.
    • requiresParentheses

      public static boolean requiresParentheses(com.sun.source.tree.ExpressionTree expression, VisitorState state)
      Checks whether an expression requires parentheses.
    • 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.
    • enclosingElements

      public static Stream<com.sun.tools.javac.code.Symbol> enclosingElements(com.sun.tools.javac.code.Symbol sym)
      Returns a stream of the owner hierarchy starting from sym, as described by Symbol.owner. Returns sym itself first, followed by its owners, closest first, up to the owning package and possibly module.
    • 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.
    • findEnclosingMethod

      @Nullable public static com.sun.source.tree.MethodTree findEnclosingMethod(VisitorState state)
      Finds the enclosing MethodTree. Returns null if no such node 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 it's 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
       
    • streamReceivers

      public static Stream<com.sun.source.tree.ExpressionTree> streamReceivers(com.sun.source.tree.ExpressionTree tree)
      Returns a Stream of ExpressionTrees resulting from calling getReceiver(ExpressionTree) repeatedly until no receiver exists.

      For example, give foo().bar().baz(), returns a stream of [foo().bar(), foo()].

      This can be more convenient than manually traversing up a tree, as it handles the termination condition automatically. Typical uses cases would include traversing fluent call chains.

    • 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)
      Finds supermethods of methodSymbol, not including methodSymbol itself, and including interfaces.
    • streamSuperMethods

      public static Stream<com.sun.tools.javac.code.Symbol.MethodSymbol> streamSuperMethods(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 com.google.common.collect.ImmutableSet<com.sun.tools.javac.code.Symbol.MethodSymbol> findMatchingMethods(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) 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.
    • isRecord

      public static boolean isRecord(com.sun.tools.javac.code.Symbol symbol)
      Returns whether the given Symbol is a record, a record's canonical constructor or a member that is part of a record's state vector.
    • 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.
    • hasAnnotation

      @InlineMe(replacement="ASTHelpers.hasAnnotation(sym, annotationClass.getName(), state)", imports="com.google.errorprone.util.ASTHelpers") @Deprecated public static boolean hasAnnotation(com.sun.tools.javac.code.Symbol sym, Class<? extends Annotation> annotationClass, VisitorState state)
      Deprecated.
      prefer hasAnnotation(Symbol, String, VisitorState) to avoid needing a runtime dependency on the annotation class, and to prevent issues if there is skew between the definition of the annotation on the runtime and compile-time classpaths
      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

      @InlineMe(replacement="ASTHelpers.hasAnnotation(tree, annotationClass.getName(), state)", imports="com.google.errorprone.util.ASTHelpers") @Deprecated public static boolean hasAnnotation(com.sun.source.tree.Tree tree, Class<? extends Annotation> annotationClass, VisitorState state)
      Deprecated.
      prefer hasAnnotation(Symbol, String, VisitorState) to avoid needing a runtime dependency on the annotation class, and to prevent issues if there is skew between the definition of the annotation on the runtime and compile-time classpaths
      Check for the presence of an annotation, considering annotation inheritance.
      Returns:
      true if the tree 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.
    • hasDirectAnnotationWithSimpleName

      public static boolean hasDirectAnnotationWithSimpleName(com.sun.tools.javac.code.Symbol sym, String simpleName)
      Check for the presence of an annotation with the given simple name directly on this symbol or its type. (If the given symbol is a method symbol, the type searched for annotations is its return type.)

      This method looks only a annotations that are directly present. It does not consider annotation inheritance (see JLS 9.6.4.3).

    • hasDirectAnnotationWithSimpleName

      public static boolean hasDirectAnnotationWithSimpleName(com.sun.tools.javac.code.Symbol.MethodSymbol sym, String simpleName)
    • hasDirectAnnotationWithSimpleName

      public static boolean hasDirectAnnotationWithSimpleName(com.sun.tools.javac.code.Symbol.VarSymbol sym, String simpleName)
    • 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"
    • shouldKeep

      public static boolean shouldKeep(com.sun.source.tree.Tree tree)
      Returns true if any of the given tree is a declaration annotated with an annotation with the simple name @UsedReflectively or @Keep, or any annotations meta-annotated with an annotation with that simple name.

      This indicates the annotated element is used (e.g. by reflection, or referenced by generated code) and should not be removed.

    • 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 getSymbol(tree).getAnnotationMirrors() to meta-syntactically inspect the annotation. Note that this method (and the getSymbol-based replacement suggested above) looks for annotations not just on the given tree (such as a MethodTree) but also on the symbol referred to by the given tree (such as on the Symbol.MethodSymbol that is being called by the given MethodInvocationTree). If you want to examine annotations only on the given tree, then use getAnnotations(com.sun.source.tree.Tree) (or a direct call to a getAnnotations method declared on a specific Tree subclass) instead.
      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).
    • getEnclosedElements

      public static List<com.sun.tools.javac.code.Symbol> getEnclosedElements(com.sun.tools.javac.code.Symbol symbol)
      A wrapper for Symbol.getEnclosedElements() to avoid binary compatibility issues for covariant overrides in subtypes of Symbol.
    • 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

      @Nullable 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

      @Nullable 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.

      Prefer this to Symbol.packge(), which throws a NullPointerException for symbols that are not contained by a package: https://bugs.openjdk.java.net/browse/JDK-8231911

    • 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.
    • getAnnotations

      public static List<? extends com.sun.source.tree.AnnotationTree> getAnnotations(com.sun.source.tree.Tree tree)
      Returns the annotations of the given tree, or an empty list.
    • 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 Generated annotations, if present. If the annotation doesn't have values set, returns the string name of the annotation itself.
    • isSuper

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

      public static boolean isInStaticInitializer(VisitorState state)
      Attempts to detect whether we're in a static-initializer-like context: that includes direct assignments to static fields, assignments to enum fields, being contained within an expression which is ultimately assigned to a static field.

      This is very much a heuristic, and not fool-proof.

    • variableIsStaticFinal

      public static boolean variableIsStaticFinal(com.sun.tools.javac.code.Symbol.VarSymbol var)
      Whether the variable is (or should be regarded as) static final.

      We regard instance fields within enums as "static final", as they will only have a finite number of instances tied to an (effectively) static final enum value.

    • 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.
    • isBugCheckerCode

      public static boolean isBugCheckerCode(VisitorState state)
      Returns true if the code is in a BugChecker class.
    • isLocal

      public static boolean isLocal(com.sun.tools.javac.code.Symbol symbol)
      Returns true if the symbol is directly or indirectly local to a method or variable initializer; see Symbol#isLocal or Symbol#isDirectlyOrIndirectlyLocal.
    • isStatic

      public static boolean isStatic(com.sun.tools.javac.code.Symbol symbol)
      Returns true if the symbol is static. Returns false for module symbols.
    • methodIsPublicAndNotAnOverride

      public static boolean methodIsPublicAndNotAnOverride(com.sun.tools.javac.code.Symbol.MethodSymbol method, VisitorState state)
      Returns true if the given method symbol is public (both the method and the enclosing class) and does not have a super-method (i.e., it is not an @Override).

      This method is useful (in part) for determining whether to suggest API improvements or not.

    • isAbstract

      public static boolean isAbstract(com.sun.tools.javac.code.Symbol.MethodSymbol method)
      Returns true if the given method symbol is abstract.

      Note: this API does not consider interface default methods to be abstract.

    • scope

      public static ErrorProneScope scope(com.sun.tools.javac.code.Scope scope)
      Returns a compatibility adapter around Scope.
    • asFlagSet

      public static EnumSet<com.sun.tools.javac.code.Flags.Flag> asFlagSet(long flags)
    • stringContainsComments

      public static boolean stringContainsComments(CharSequence source, com.sun.tools.javac.util.Context context)
      Returns true if the given source code contains comments.
    • getTypeSubstitution

      public static com.google.common.collect.ImmutableListMultimap<com.sun.tools.javac.code.Symbol.TypeVariableSymbol,com.sun.tools.javac.code.Type> getTypeSubstitution(com.sun.tools.javac.code.Type type, com.sun.tools.javac.code.Symbol sym)
      Returns the mapping between type variables and their instantiations in the given type. For example, the instantiation of Map<K, V> as Map<String, Integer> would be represented as a TypeSubstitution from [K, V] to [String, Integer].
    • hasNoExplicitType

      @Deprecated public static boolean hasNoExplicitType(com.sun.source.tree.VariableTree tree, VisitorState state)
    • hasImplicitType

      public static boolean hasImplicitType(com.sun.source.tree.VariableTree tree, VisitorState state)
      Returns whether this is a var or a lambda parameter that has no explicit type.
    • hasExplicitSource

      public static boolean hasExplicitSource(com.sun.source.tree.Tree tree, VisitorState state)
      Returns whether the given tree has an explicit source code representation.
    • isKotlin

      public static boolean isKotlin(com.sun.tools.javac.code.Symbol symbol, VisitorState state)
      Returns true if this symbol was declared in Kotlin source.
    • hasOverloadWithOnlyOneParameter

      public static boolean hasOverloadWithOnlyOneParameter(com.sun.tools.javac.code.Symbol.MethodSymbol existingMethod, com.sun.tools.javac.util.Name targetMethodName, com.sun.tools.javac.code.Type onlyParameterType, VisitorState state)
      Returns whether existingMethod has an overload (or "nearly" an overload) with the given targetMethodName, and only a single parameter of type onlyParameterType.
    • getSwitchDefault

      public static Optional<? extends com.sun.source.tree.CaseTree> getSwitchDefault(com.sun.source.tree.SwitchTree switchTree)
    • getCaseExpressions

      public static Stream<? extends com.sun.source.tree.ExpressionTree> getCaseExpressions(com.sun.source.tree.CaseTree caseTree)
      Retrieves a stream containing all case expressions, in order, for a given CaseTree. This method acts as a facade to the CaseTree.getExpressions() API, falling back to legacy APIs when necessary.