Class VisitorState
- Author:
- alexeagle@google.com (Alex Eagle)
-
Field Summary
-
Constructor Summary
ConstructorDescriptionVisitorState
(com.sun.tools.javac.util.Context context) Deprecated.VisitorState
(com.sun.tools.javac.util.Context context, DescriptionListener listener) Deprecated.Use the equivalent factory methodcreateForCustomFindingCollection(com.sun.tools.javac.util.Context, com.google.errorprone.DescriptionListener)
.VisitorState
(com.sun.tools.javac.util.Context context, DescriptionListener listener, Map<String, BugPattern.SeverityLevel> severityMap, ErrorProneOptions errorProneOptions) -
Method Summary
Modifier and TypeMethodDescriptioncom.sun.tools.javac.code.Type
arrayTypeForType
(com.sun.tools.javac.code.Type baseType) Build an Array Type from another Typecom.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).com.google.common.collect.ImmutableMultiset
<String> counters()
Returns a copy of all of the counters previously added to this VisitorState withincrementCounter(com.google.errorprone.bugpatterns.BugChecker, java.lang.String)
.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.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 tolistener
.static VisitorState
createForUtilityPurposes
(com.sun.tools.javac.util.Context context) Return a VisitorState that has no Error Prone configuration, and can't report results.final <T extends com.sun.source.tree.Tree>
@Nullable TfindEnclosing
(Class<? extends T>... classes) Find the first enclosing tree node of one of the given types.final @Nullable com.sun.source.util.TreePath
findPathToEnclosing
(Class<? extends com.sun.source.tree.Tree>... classes) Returns theTreePath
to the nearest tree node of one of the given types.getConstantExpression
(Object value) Returns the Java source code for a constant expression representing the given constant value.int
getEndPosition
(com.sun.source.tree.Tree node) Returns the end position of the node, or -1 if it is not available.com.sun.tools.javac.util.Name
com.sun.tools.javac.util.Names
getNames()
getOffsetTokens
(int start, int end) Returns the list ofTokens.Token
s for source code between the given positions, offset by the start position.getOffsetTokensForNode
(com.sun.source.tree.Tree tree) Returns the list ofTokens.Token
s for the givenJCTree
, offset by the start position of the tree within the overall source.com.sun.source.util.TreePath
getPath()
@Nullable CharSequence
Gets the current source file.@Nullable String
getSourceForNode
(com.sun.source.tree.Tree tree) Gets the original source code that represents the given node.@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.@Nullable com.sun.tools.javac.code.Symbol.ClassSymbol
getSymbolFromString
(com.sun.tools.javac.code.Symbol.ModuleSymbol msym, com.sun.tools.javac.util.Name name) @Nullable com.sun.tools.javac.code.Symbol
getSymbolFromString
(String symStr) com.sun.tools.javac.code.Symtab
getTokensForNode
(com.sun.source.tree.Tree tree) Returns the list ofTokens.Token
s for the givenJCTree
.com.sun.tools.javac.tree.TreeMaker
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.@Nullable com.sun.tools.javac.code.Type
getTypeFromString
(String typeStr) Given the binary name of a class, returns theType
.com.sun.tools.javac.code.Types
getTypes()
void
incrementCounter
(BugChecker bugChecker, String key) Increment the counter for a combination ofbugChecker
's canonical name andkey
by 1.void
incrementCounter
(BugChecker bugChecker, String key, int count) Increment the counter for a combination ofbugChecker
's canonical name andkey
bycount
.boolean
Returns true if the compilation is targeting Android.static <T> Supplier
<T> Produces a cache for a function that is expected to return the same result throughout a compilation, but requires aVisitorState
to compute that result.void
reportMatch
(Description description) timingSpan
(Suppressible suppressible) Returns a timing span for the givenSuppressible
.withPath
(com.sun.source.util.TreePath path) withSuppression
(SuppressionInfo.SuppressedState suppressedState)
-
Field Details
-
context
public final com.sun.tools.javac.util.Context context
-
-
Constructor Details
-
VisitorState
Deprecated.If VisitorState is needed, usecreateForUtilityPurposes(com.sun.tools.javac.util.Context)
, otherwise just use utility methods in ASTHelpers that don't need VisitorSate.Return a VisitorState that has no Error Prone configuration, and can't report results. -
VisitorState
@Deprecated public VisitorState(com.sun.tools.javac.util.Context context, DescriptionListener listener) Deprecated.Use the equivalent factory methodcreateForCustomFindingCollection(com.sun.tools.javac.util.Context, com.google.errorprone.DescriptionListener)
.Return a VisitorState that has no Error Prone configuration, but can report findings tolistener
. -
VisitorState
@Deprecated public VisitorState(com.sun.tools.javac.util.Context context, DescriptionListener listener, Map<String, BugPattern.SeverityLevel> severityMap, ErrorProneOptions errorProneOptions) Deprecated.Return a VisitorState configured for a new compilation, including Error Prone configuration.
-
-
Method Details
-
createForUtilityPurposes
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 tolistener
. -
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
-
withSuppression
-
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
-
getSymtab
public com.sun.tools.javac.code.Symtab getSymtab() -
getNames
public com.sun.tools.javac.util.Names getNames() -
getNullnessAnalysis
-
errorProneOptions
-
severityMap
-
reportMatch
-
incrementCounter
Increment the counter for a combination ofbugChecker
's canonical name andkey
by 1.e.g.: a key of
foo
becomesFooChecker-foo
. -
incrementCounter
Increment the counter for a combination ofbugChecker
's canonical name andkey
bycount
.e.g.: a key of
foo
becomesFooChecker-foo
. -
counters
Returns a copy of all of the counters previously added to this VisitorState withincrementCounter(com.google.errorprone.bugpatterns.BugChecker, java.lang.String)
. -
getName
-
getTypeFromString
Given the binary name of a class, returns theType
.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 itsSupplier.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
- Parameters:
symStr
- the string representation of a symbol- Returns:
- the Symbol object, or null if it cannot be found
-
binaryNameFromClassname
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 generalgetName(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 theTreePath
to the nearest tree node of one of the given types. To instead retrieve the element directly, usefindEnclosing(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
Gets the current source file.- Returns:
- the source file as a sequence of characters, or null if it is not available
-
getSourceForNode
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
Returns the list ofTokens.Token
s for the givenJCTree
.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
Returns the list ofTokens.Token
s for the givenJCTree
, 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
Returns the list ofTokens.Token
s 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
Returns a timing span for the givenSuppressible
. -
memoize
Produces a cache for a function that is expected to return the same result throughout a compilation, but requires aVisitorState
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
Returns the Java source code for a constant expression representing the given constant value. LikeElements.getConstantExpression(java.lang.Object)
, but doesn't over-escape single quotes in strings.
-
createForUtilityPurposes(com.sun.tools.javac.util.Context)
, otherwise just use utility methods in ASTHelpers that don't need VisitorSate.