Class VisitorState

java.lang.Object
com.google.errorprone.VisitorState

public class VisitorState extends Object
Author:
alexeagle@google.com (Alex Eagle)
  • Field Details

    • context

      public final com.sun.tools.javac.util.Context context
  • Constructor Details

  • Method Details

    • createForUtilityPurposes

      public static VisitorState createForUtilityPurposes(com.sun.tools.javac.util.Context context)
      Return a VisitorState that has no Error Prone configuration, and can't report results.

      If using this method, consider moving to using utility methods not needing VisitorSate

    • createForCustomFindingCollection

      public static VisitorState createForCustomFindingCollection(com.sun.tools.javac.util.Context context, DescriptionListener listener)
      Return a VisitorState that has no Error Prone configuration, but can report findings to listener.
    • createConfiguredForCompilation

      public static VisitorState createConfiguredForCompilation(com.sun.tools.javac.util.Context context, DescriptionListener listener, Map<String,BugPattern.SeverityLevel> severityMap, ErrorProneOptions errorProneOptions)
      Return a VisitorState configured for a new compilation, including Error Prone configuration.
    • withPath

      public VisitorState withPath(com.sun.source.util.TreePath path)
    • withSuppression

      public VisitorState withSuppression(SuppressionInfo.SuppressedState suppressedState)
    • getPath

      public com.sun.source.util.TreePath getPath()
    • getTreeMaker

      public com.sun.tools.javac.tree.TreeMaker getTreeMaker()
    • getTypes

      public com.sun.tools.javac.code.Types getTypes()
    • getElements

      public Elements getElements()
    • getSymtab

      public com.sun.tools.javac.code.Symtab getSymtab()
    • getNames

      public com.sun.tools.javac.util.Names getNames()
    • getNullnessAnalysis

      public NullnessAnalysis getNullnessAnalysis()
    • errorProneOptions

      public ErrorProneOptions errorProneOptions()
    • severityMap

      public Map<String,BugPattern.SeverityLevel> severityMap()
    • reportMatch

      public void reportMatch(Description description)
    • incrementCounter

      public void incrementCounter(BugChecker bugChecker, String key)
      Increment the counter for a combination of bugChecker's canonical name and key by 1.

      e.g.: a key of foo becomes FooChecker-foo.

    • incrementCounter

      public void incrementCounter(BugChecker bugChecker, String key, int count)
      Increment the counter for a combination of bugChecker's canonical name and key by count.

      e.g.: a key of foo becomes FooChecker-foo.

    • counters

      public com.google.common.collect.ImmutableMultiset<String> counters()
      Returns a copy of all of the counters previously added to this VisitorState with incrementCounter(com.google.errorprone.bugpatterns.BugChecker, java.lang.String).
    • getName

      public com.sun.tools.javac.util.Name getName(String nameStr)
    • getTypeFromString

      public @Nullable com.sun.tools.javac.code.Type getTypeFromString(String typeStr)
      Given the binary name of a class, returns the Type.

      Prefer not to use this method for constant strings, or strings otherwise known at compile time. Instead, save the result of Suppliers.typeFromString(java.lang.String) as a class constant, and use its Supplier.get(com.google.errorprone.VisitorState) method to look up the Type when needed. This lookup will be faster, improving Error Prone's analysis time.

      If this method returns null, the compiler doesn't have access to this type, which means that if you are comparing other types to this for equality or the subtype relation, your result would always be false even if it could create the type. Thus it might be best to bail out early in your matcher if this method returns null on your type of interest.

      Parameters:
      typeStr - the JLS 13.1 binary name of the class, e.g. "java.util.Map$Entry"
      Returns:
      the Type, or null if it cannot be found
    • getSymbolFromString

      public @Nullable com.sun.tools.javac.code.Symbol getSymbolFromString(String symStr)
      Parameters:
      symStr - the string representation of a symbol
      Returns:
      the Symbol object, or null if it cannot be found
    • binaryNameFromClassname

      public com.sun.tools.javac.util.Name binaryNameFromClassname(String className)
      Returns the Name object corresponding to the named class, converting it to binary form along the way if necessary (i.e., replacing Foo.Bar with Foo$Bar). To get the Name corresponding to some string that is not a class name, see the more general getName(String).
    • getSymbolFromName

      public @Nullable com.sun.tools.javac.code.Symbol.ClassSymbol getSymbolFromName(com.sun.tools.javac.util.Name name)
      Look up the class symbol for a given Name.
      Parameters:
      name - the name to look up, which must be in binary form (i.e. with $ for nested classes).
    • getSymbolFromString

      public @Nullable com.sun.tools.javac.code.Symbol.ClassSymbol getSymbolFromString(com.sun.tools.javac.code.Symbol.ModuleSymbol msym, com.sun.tools.javac.util.Name name)
    • getType

      public com.sun.tools.javac.code.Type getType(com.sun.tools.javac.code.Type baseType, boolean isArray, List<com.sun.tools.javac.code.Type> typeParams)
      Build an instance of a Type.
    • arrayTypeForType

      public com.sun.tools.javac.code.Type arrayTypeForType(com.sun.tools.javac.code.Type baseType)
      Build an Array Type from another Type
    • findPathToEnclosing

      @SafeVarargs public final @Nullable com.sun.source.util.TreePath findPathToEnclosing(Class<? extends com.sun.source.tree.Tree>... classes)
      Returns the TreePath to the nearest tree node of one of the given types. To instead retrieve the element directly, use findEnclosing(Class...).
      Returns:
      the path, or null if there is no match
    • findEnclosing

      @SafeVarargs public final <T extends com.sun.source.tree.Tree> @Nullable T findEnclosing(Class<? extends T>... classes)
      Find the first enclosing tree node of one of the given types.
      Returns:
      the node, or null if there is no match
    • getSourceCode

      public @Nullable CharSequence getSourceCode()
      Gets the current source file.
      Returns:
      the source file as a sequence of characters, or null if it is not available
    • getSourceForNode

      public @Nullable String getSourceForNode(com.sun.source.tree.Tree tree)
      Gets the original source code that represents the given node.

      Note that this may be different from what is returned by calling .toString() on the node. This returns exactly what is in the source code, whereas .toString() pretty-prints the node from its AST representation.

      Returns:
      the source code that represents the node, or null if the source code is unavailable (e.g. for generated or desugared AST nodes)
    • getTokensForNode

      public List<ErrorProneToken> getTokensForNode(com.sun.source.tree.Tree tree)
      Returns the list of Tokens.Tokens for the given JCTree.

      This is moderately expensive (the source of the node has to be re-lexed), so it should only be used if a fix is already going to be emitted.

    • getOffsetTokensForNode

      public List<ErrorProneToken> getOffsetTokensForNode(com.sun.source.tree.Tree tree)
      Returns the list of Tokens.Tokens for the given JCTree, offset by the start position of the tree within the overall source.

      This is moderately expensive (the source of the node has to be re-lexed), so it should only be used if a fix is already going to be emitted.

    • getOffsetTokens

      public List<ErrorProneToken> getOffsetTokens(int start, int end)
      Returns the list of Tokens.Tokens for source code between the given positions, offset by the start position.

      This is moderately expensive (the source of the node has to be re-lexed), so it should only be used if a fix is already going to be emitted.

    • getEndPosition

      public int getEndPosition(com.sun.source.tree.Tree node)
      Returns the end position of the node, or -1 if it is not available.
    • isAndroidCompatible

      public boolean isAndroidCompatible()
      Returns true if the compilation is targeting Android.
    • timingSpan

      public AutoCloseable timingSpan(Suppressible suppressible)
      Returns a timing span for the given Suppressible.
    • memoize

      public static <T> Supplier<T> memoize(Supplier<T> f)
      Produces a cache for a function that is expected to return the same result throughout a compilation, but requires a VisitorState to compute that result.

      Note: Do not use this method for a function that depends on the varying state of a VisitorState (e.g. getPath()—including the compilation unit itself!).

    • getConstantExpression

      public String getConstantExpression(Object value)
      Returns the Java source code for a constant expression representing the given constant value. Like Elements.getConstantExpression(java.lang.Object), but doesn't over-escape single quotes in strings.