public class VisitorState extends Object
Modifier and Type | Field and Description |
---|---|
com.sun.tools.javac.util.Context |
context |
Constructor and Description |
---|
VisitorState(com.sun.tools.javac.util.Context context)
Deprecated.
If VisitorState is needed, use
createForUtilityPurposes(com.sun.tools.javac.util.Context) , otherwise just
use utility methods in ASTHelpers that don't need VisitorSate. |
VisitorState(com.sun.tools.javac.util.Context context,
DescriptionListener listener)
Deprecated.
Use the equivalent factory method
createForCustomFindingCollection(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)
|
Modifier and Type | Method and Description |
---|---|
com.sun.tools.javac.code.Type |
arrayTypeForType(com.sun.tools.javac.code.Type baseType)
Build an Array Type from another Type
|
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).
|
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) . |
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 to
listener . |
static VisitorState |
createForUtilityPurposes(com.sun.tools.javac.util.Context context)
Return a VisitorState that has no Error Prone configuration, and can't report results.
|
ErrorProneOptions |
errorProneOptions() |
<T extends com.sun.source.tree.Tree> |
findEnclosing(Class<? extends T>... classes)
Find the first enclosing tree node of one of the given types.
|
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. |
String |
getConstantExpression(Object value)
Returns the Java source code for a constant expression representing the given constant value.
|
Elements |
getElements() |
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 |
getName(String nameStr) |
com.sun.tools.javac.util.Names |
getNames() |
NullnessAnalysis |
getNullnessAnalysis() |
List<ErrorProneToken> |
getOffsetTokens(int start,
int end)
Returns the list of
Tokens.Token s for source code between the given positions, offset by the
start position. |
List<ErrorProneToken> |
getOffsetTokensForNode(com.sun.source.tree.Tree tree)
Returns the list of
Tokens.Token s for the given JCTree , offset by the start position
of the tree within the overall source. |
com.sun.source.util.TreePath |
getPath() |
CharSequence |
getSourceCode()
Gets the current source file.
|
String |
getSourceForNode(com.sun.source.tree.Tree tree)
Gets the original source code that represents the given node.
|
com.sun.tools.javac.code.Symbol.ClassSymbol |
getSymbolFromName(com.sun.tools.javac.util.Name name)
Look up the class symbol for a given Name.
|
com.sun.tools.javac.code.Symbol |
getSymbolFromString(String symStr) |
com.sun.tools.javac.code.Symbol.ClassSymbol |
getSymbolFromString(com.sun.tools.javac.code.Symbol.ModuleSymbol msym,
com.sun.tools.javac.util.Name name) |
com.sun.tools.javac.code.Symtab |
getSymtab() |
List<ErrorProneToken> |
getTokensForNode(com.sun.source.tree.Tree tree)
Returns the list of
Tokens.Token s for the given JCTree . |
com.sun.tools.javac.tree.TreeMaker |
getTreeMaker() |
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.
|
com.sun.tools.javac.code.Type |
getTypeFromString(String typeStr)
Given the binary name of a class, returns the
Type . |
com.sun.tools.javac.code.Types |
getTypes() |
void |
incrementCounter(BugChecker bugChecker,
String key)
Increment the counter for a combination of
bugChecker 's canonical name and key
by 1. |
void |
incrementCounter(BugChecker bugChecker,
String key,
int count)
Increment the counter for a combination of
bugChecker 's canonical name and key
by count . |
boolean |
isAndroidCompatible()
Returns true if the compilation is targeting Android.
|
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.
|
void |
reportMatch(Description description) |
AutoCloseable |
timingSpan(Suppressible suppressible)
Returns a timing span for the given
Suppressible . |
VisitorState |
withPath(com.sun.source.util.TreePath path) |
VisitorState |
withSuppression(SuppressionInfo.SuppressedState suppressedState) |
@Deprecated public VisitorState(com.sun.tools.javac.util.Context context)
createForUtilityPurposes(com.sun.tools.javac.util.Context)
, otherwise just
use utility methods in ASTHelpers that don't need VisitorSate.@Deprecated public VisitorState(com.sun.tools.javac.util.Context context, DescriptionListener listener)
createForCustomFindingCollection(com.sun.tools.javac.util.Context, com.google.errorprone.DescriptionListener)
.listener
.@Deprecated public VisitorState(com.sun.tools.javac.util.Context context, DescriptionListener listener, Map<String,BugPattern.SeverityLevel> severityMap, ErrorProneOptions errorProneOptions)
createConfiguredForCompilation(com.sun.tools.javac.util.Context, com.google.errorprone.DescriptionListener, java.util.Map<java.lang.String, com.google.errorprone.BugPattern.SeverityLevel>, com.google.errorprone.ErrorProneOptions)
.public static VisitorState createForUtilityPurposes(com.sun.tools.javac.util.Context context)
If using this method, consider moving to using utility methods not needing VisitorSate
public static VisitorState createForCustomFindingCollection(com.sun.tools.javac.util.Context context, DescriptionListener listener)
listener
.public static VisitorState createConfiguredForCompilation(com.sun.tools.javac.util.Context context, DescriptionListener listener, Map<String,BugPattern.SeverityLevel> severityMap, ErrorProneOptions errorProneOptions)
public VisitorState withPath(com.sun.source.util.TreePath path)
public VisitorState withSuppression(SuppressionInfo.SuppressedState suppressedState)
public com.sun.source.util.TreePath getPath()
public com.sun.tools.javac.tree.TreeMaker getTreeMaker()
public com.sun.tools.javac.code.Types getTypes()
public Elements getElements()
public com.sun.tools.javac.code.Symtab getSymtab()
public com.sun.tools.javac.util.Names getNames()
public NullnessAnalysis getNullnessAnalysis()
public ErrorProneOptions errorProneOptions()
public void reportMatch(Description description)
public void incrementCounter(BugChecker bugChecker, String key)
bugChecker
's canonical name and key
by 1.
e.g.: a key of foo
becomes FooChecker-foo
.
public void incrementCounter(BugChecker bugChecker, String key, int count)
bugChecker
's canonical name and key
by count
.
e.g.: a key of foo
becomes FooChecker-foo
.
public com.google.common.collect.ImmutableMultiset<String> counters()
incrementCounter(com.google.errorprone.bugpatterns.BugChecker, java.lang.String)
.public com.sun.tools.javac.util.Name getName(String nameStr)
@Nullable public com.sun.tools.javac.code.Type getTypeFromString(String typeStr)
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.
typeStr
- the JLS 13.1 binary name of the class, e.g. "java.util.Map$Entry"
Type
, or null if it cannot be found@Nullable public com.sun.tools.javac.code.Symbol getSymbolFromString(String symStr)
symStr
- the string representation of a symbolpublic com.sun.tools.javac.util.Name binaryNameFromClassname(String className)
getName(String)
.@Nullable public com.sun.tools.javac.code.Symbol.ClassSymbol getSymbolFromName(com.sun.tools.javac.util.Name name)
name
- the name to look up, which must be in binary form (i.e. with $ for nested classes).@Nullable public com.sun.tools.javac.code.Symbol.ClassSymbol getSymbolFromString(com.sun.tools.javac.code.Symbol.ModuleSymbol msym, com.sun.tools.javac.util.Name name)
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)
public com.sun.tools.javac.code.Type arrayTypeForType(com.sun.tools.javac.code.Type baseType)
@Nullable @SafeVarargs public final com.sun.source.util.TreePath findPathToEnclosing(Class<? extends com.sun.source.tree.Tree>... classes)
TreePath
to the nearest tree node of one of the given types. To instead
retrieve the element directly, use findEnclosing(Class...)
.null
if there is no match@Nullable @SafeVarargs public final <T extends com.sun.source.tree.Tree> T findEnclosing(Class<? extends T>... classes)
null
if there is no match@Nullable public CharSequence getSourceCode()
@Nullable public String getSourceForNode(com.sun.source.tree.Tree tree)
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.
null
if the source code is
unavailable (e.g. for generated or desugared AST nodes)public List<ErrorProneToken> getTokensForNode(com.sun.source.tree.Tree tree)
Tokens.Token
s 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.
public List<ErrorProneToken> getOffsetTokensForNode(com.sun.source.tree.Tree tree)
Tokens.Token
s 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.
public List<ErrorProneToken> getOffsetTokens(int start, int end)
Tokens.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.
public int getEndPosition(com.sun.source.tree.Tree node)
public boolean isAndroidCompatible()
public AutoCloseable timingSpan(Suppressible suppressible)
Suppressible
.public static <T> Supplier<T> memoize(Supplier<T> f)
getPath()
.public String getConstantExpression(Object value)
Elements.getConstantExpression(java.lang.Object)
, but doesn't over-escape single quotes in strings.Copyright © 2021 Google LLC. All rights reserved.