com.android.tools.lint.detector.api
Interface Detector.JavaScanner

Enclosing class:
Detector

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

createJavaVisitor

@Nullable
lombok.ast.AstVisitor createJavaVisitor(@NonNull
                                                 JavaContext context)
Create a parse tree visitor to process the parse tree. All 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.

Parameters:
context - the Context for the file being analyzed
Returns:
a visitor, or null.

getApplicableNodeTypes

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

Returns:
the list of applicable node types (AST node classes), or null

getApplicableMethodNames

@Nullable
java.util.List<java.lang.String> getApplicableMethodNames()
Return the list of method names this detector is interested in, or null. If this method returns non-null, then any AST nodes that match a method call in the list will be passed to the 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.

Returns:
a set of applicable method names, or null.

visitMethod

void visitMethod(@NonNull
                 JavaContext context,
                 @Nullable
                 lombok.ast.AstVisitor visitor,
                 @NonNull
                 lombok.ast.MethodInvocation node)
Method invoked for any method calls found that matches any names returned by 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.

Parameters:
context - the context of the lint request
visitor - the visitor created from createJavaVisitor(JavaContext), or null
node - the MethodInvocation node for the invoked method

getApplicableConstructorTypes

@Nullable
java.util.List<java.lang.String> getApplicableConstructorTypes()
Return the list of constructor types this detector is interested in, or null. If this method returns non-null, then any AST nodes that match a constructor call in the list will be passed to the #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.

Returns:
a set of applicable fully qualified types, or null.

visitConstructor

void visitConstructor(@NonNull
                      JavaContext context,
                      @Nullable
                      lombok.ast.AstVisitor visitor,
                      @NonNull
                      lombok.ast.ConstructorInvocation node,
                      @NonNull
                      JavaParser.ResolvedMethod constructor)
Method invoked for any constructor calls found that matches any names returned by 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.

Parameters:
context - the context of the lint request
visitor - the visitor created from createJavaVisitor(JavaContext), or null
node - the ConstructorInvocation node for the invoked method
constructor - the resolved constructor method with type information

appliesToResourceRefs

boolean appliesToResourceRefs()
Returns whether this detector cares about Android resource references (such as 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.

Returns:
true if this detector wants to be notified of R resource identifiers found in the code.

visitResourceReference

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)
Called for any resource references (such as R.layout.main found in Java code, provided this detector returned true from appliesToResourceRefs().

Parameters:
context - the lint scanning context
visitor - the visitor created from createJavaVisitor(JavaContext), or null
node - the variable reference for the resource
type - 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)

applicableSuperClasses

@Nullable
java.util.List<java.lang.String> applicableSuperClasses()
Returns a list of fully qualified names for super classes that this detector cares about. If not null, this detector will *only* be called if the current class is a subclass of one of the specified superclasses.

Returns:
a list of fully qualified names

checkClass

void checkClass(@NonNull
                JavaContext context,
                @Nullable
                lombok.ast.ClassDeclaration declaration,
                @NonNull
                lombok.ast.Node node,
                @NonNull
                JavaParser.ResolvedClass resolvedClass)
Called for each class that extends one of the super classes specified with applicableSuperClasses()

Parameters:
context - the lint scanning context
declaration - the class declaration node, or null for anonymous classes
node - the class declaration node or the anonymous class construction node
resolvedClass - the resolved class