|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public static interface Detector.JavaScanner
Specialized interface for detectors that scan Java source file parse trees
Method Summary | |
---|---|
java.util.List<java.lang.String> |
applicableSuperClasses()
Returns a list of fully qualified names for super classes that this detector cares about. |
boolean |
appliesToResourceRefs()
Returns whether this detector cares about Android resource references (such as R.layout.main or R.string.app_name ). |
void |
checkClass(JavaContext context,
lombok.ast.ClassDeclaration declaration,
lombok.ast.Node node,
JavaParser.ResolvedClass resolvedClass)
Called for each class that extends one of the super classes specified with applicableSuperClasses() |
lombok.ast.AstVisitor |
createJavaVisitor(JavaContext context)
Create a parse tree visitor to process the parse tree. |
java.util.List<java.lang.String> |
getApplicableConstructorTypes()
Return the list of constructor types this detector is interested in, or null. |
java.util.List<java.lang.String> |
getApplicableMethodNames()
Return the list of method names this detector is interested in, or null. |
java.util.List<java.lang.Class<? extends lombok.ast.Node>> |
getApplicableNodeTypes()
Return the types of AST nodes that the visitor returned from createJavaVisitor(JavaContext) should visit. |
void |
visitConstructor(JavaContext context,
lombok.ast.AstVisitor visitor,
lombok.ast.ConstructorInvocation node,
JavaParser.ResolvedMethod constructor)
Method invoked for any constructor calls found that matches any names returned by getApplicableConstructorTypes() . |
void |
visitMethod(JavaContext context,
lombok.ast.AstVisitor visitor,
lombok.ast.MethodInvocation node)
Method invoked for any method calls found that matches any names returned by getApplicableMethodNames() . |
void |
visitResourceReference(JavaContext context,
lombok.ast.AstVisitor visitor,
lombok.ast.Node node,
java.lang.String type,
java.lang.String name,
boolean isFramework)
Called for any resource references (such as R.layout.main
found in Java code, provided this detector returned true from
appliesToResourceRefs() . |
Method Detail |
---|
@Nullable lombok.ast.AstVisitor createJavaVisitor(@NonNull JavaContext context)
Detector.JavaScanner
detectors must provide a visitor, unless they
either return true from appliesToResourceRefs()
or return
non null from getApplicableMethodNames()
.
If you return specific AST node types from
getApplicableNodeTypes()
, then the visitor will only
be called for the specific requested node types. This is more
efficient, since it allows many detectors that apply to only a small
part of the AST (such as method call nodes) to share iteration of the
majority of the parse tree.
If you return null from getApplicableNodeTypes()
, then your
visitor will be called from the top and all node types visited.
Note that a new visitor is created for each separate compilation unit, so you can store per file state in the visitor.
context
- the Context
for the file being analyzed
@Nullable java.util.List<java.lang.Class<? extends lombok.ast.Node>> getApplicableNodeTypes()
createJavaVisitor(JavaContext)
should visit. See the
documentation for createJavaVisitor(JavaContext)
for details
on how the shared visitor is used.
If you return null from this method, then the visitor will process the full tree instead.
Note that for the shared visitor, the return codes from the visit methods are ignored: returning true will not prune iteration of the subtree, since there may be other node types interested in the children. If you need to ensure that your visitor only processes a part of the tree, use a full visitor instead. See the OverdrawDetector implementation for an example of this.
@Nullable java.util.List<java.lang.String> getApplicableMethodNames()
visitMethod(JavaContext, AstVisitor, MethodInvocation)
method for processing. The visitor created by
createJavaVisitor(JavaContext)
is also passed to that
method, although it can be null.
This makes it easy to write detectors that focus on some fixed calls.
For example, the StringFormatDetector uses this mechanism to look for
"format" calls, and when found it looks around (using the AST's
Node.getParent()
method) to see if it's called on
a String class instance, and if so do its normal processing. Note
that since it doesn't need to do any other AST processing, that
detector does not actually supply a visitor.
void visitMethod(@NonNull JavaContext context, @Nullable lombok.ast.AstVisitor visitor, @NonNull lombok.ast.MethodInvocation node)
getApplicableMethodNames()
. This also passes
back the visitor that was created by
createJavaVisitor(JavaContext)
, but a visitor is not
required. It is intended for detectors that need to do additional AST
processing, but also want the convenience of not having to look for
method names on their own.
context
- the context of the lint requestvisitor
- the visitor created from
createJavaVisitor(JavaContext)
, or nullnode
- the MethodInvocation
node for the invoked method@Nullable java.util.List<java.lang.String> getApplicableConstructorTypes()
#visitConstructor(JavaContext, AstVisitor, ConstructorInvocation, ResolvedMethod)
method for processing. The visitor created by
createJavaVisitor(JavaContext)
is also passed to that
method, although it can be null.
This makes it easy to write detectors that focus on some fixed constructors.
void visitConstructor(@NonNull JavaContext context, @Nullable lombok.ast.AstVisitor visitor, @NonNull lombok.ast.ConstructorInvocation node, @NonNull JavaParser.ResolvedMethod constructor)
getApplicableConstructorTypes()
. This also passes
back the visitor that was created by
createJavaVisitor(JavaContext)
, but a visitor is not
required. It is intended for detectors that need to do additional AST
processing, but also want the convenience of not having to look for
method names on their own.
context
- the context of the lint requestvisitor
- the visitor created from
createJavaVisitor(JavaContext)
, or nullnode
- the ConstructorInvocation
node for the invoked methodconstructor
- the resolved constructor method with type informationboolean appliesToResourceRefs()
R.layout.main
or R.string.app_name
). If it
does, then the visitor will look for these patterns, and if found, it
will invoke visitResourceReference(com.android.tools.lint.detector.api.JavaContext, lombok.ast.AstVisitor, lombok.ast.Node, java.lang.String, java.lang.String, boolean)
passing the resource type
and resource name. It also passes the visitor, if any, that was
created by createJavaVisitor(JavaContext)
, such that a
detector can do more than just look for resources.
void visitResourceReference(@NonNull JavaContext context, @Nullable lombok.ast.AstVisitor visitor, @NonNull lombok.ast.Node node, @NonNull java.lang.String type, @NonNull java.lang.String name, boolean isFramework)
R.layout.main
found in Java code, provided this detector returned true
from
appliesToResourceRefs()
.
context
- the lint scanning contextvisitor
- the visitor created from
createJavaVisitor(JavaContext)
, or nullnode
- the variable reference for the resourcetype
- the resource type, such as "layout" or "string"name
- the resource name, such as "main" from
R.layout.main
isFramework
- whether the resource is a framework resource
(android.R) or a local project resource (R)@Nullable java.util.List<java.lang.String> applicableSuperClasses()
void checkClass(@NonNull JavaContext context, @Nullable lombok.ast.ClassDeclaration declaration, @NonNull lombok.ast.Node node, @NonNull JavaParser.ResolvedClass resolvedClass)
applicableSuperClasses()
context
- the lint scanning contextdeclaration
- the class declaration node, or null for anonymous classesnode
- the class declaration node or the anonymous class construction noderesolvedClass
- the resolved class
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |