com.android.tools.lint.detector.api
Class Detector

java.lang.Object
  extended by com.android.tools.lint.detector.api.Detector
Direct Known Subclasses:
ResourceXmlDetector

@Beta
public abstract class Detector
extends java.lang.Object

A detector is able to find a particular problem (or a set of related problems). Each problem type is uniquely identified as an Issue.

Detectors will be called in a predefined order:

  1. Manifest file
  2. Resource files, in alphabetical order by resource type (therefore, "layout" is checked before "values", "values-de" is checked before "values-en" but after "values", and so on.
  3. Java sources
  4. Java classes
  5. Gradle files
  6. Generic files
  7. Proguard files
  8. Property files
If a detector needs information when processing a file type that comes from a type of file later in the order above, they can request a second phase; see LintDriver.requestRepeat(com.android.tools.lint.detector.api.Detector, java.util.EnumSet). NOTE: This is not a public or final API; if you rely on this be prepared to adjust your code for the next tools release.


Nested Class Summary
static interface Detector.BinaryResourceScanner
          Specialized interface for detectors that scan binary resource files
static interface Detector.ClassScanner
          Specialized interface for detectors that scan Java class files
static interface Detector.GradleScanner
          Specialized interface for detectors that scan Gradle files
static interface Detector.JavaScanner
          Specialized interface for detectors that scan Java source file parse trees
static interface Detector.OtherFileScanner
          Specialized interface for detectors that scan other files
static interface Detector.ResourceFolderScanner
          Specialized interface for detectors that scan resource folders (the folder directory itself, not the individual files within it
static interface Detector.XmlScanner
          Specialized interface for detectors that scan XML files
 
Constructor Summary
Detector()
           
 
Method Summary
 void afterCheckFile(Context context)
          Analysis has just been finished for a specific file, perform any cleanup or report issues found
 void afterCheckLibraryProject(Context context)
          Analysis has just been finished for the given library project, perform any cleanup or report issues that require library-project-wide analysis.
 void afterCheckProject(Context context)
          Analysis has just been finished for the whole project, perform any cleanup or report issues that require project-wide analysis.
 java.util.List<java.lang.String> applicableSuperClasses()
           
 boolean appliesTo(Context context, java.io.File file)
          Returns true if this detector applies to the given file
 boolean appliesTo(com.android.resources.ResourceFolderType folderType)
           
 boolean appliesToFolder(Scope scope, com.android.resources.ResourceFolderType folderType)
           
 boolean appliesToResourceRefs()
           
 void beforeCheckFile(Context context)
          Analysis is about to be performed on a specific file, perform any setup steps.
 void beforeCheckLibraryProject(Context context)
          Analysis is about to begin for the given library project, perform any setup steps.
 void beforeCheckProject(Context context)
          Analysis is about to begin, perform any setup steps.
 void checkBinaryResource(ResourceContext context)
           
 void checkCall(ClassContext context, org.objectweb.asm.tree.ClassNode classNode, org.objectweb.asm.tree.MethodNode method, org.objectweb.asm.tree.MethodInsnNode call)
           
 void checkClass(ClassContext context, org.objectweb.asm.tree.ClassNode classNode)
           
 void checkClass(JavaContext context, lombok.ast.ClassDeclaration declaration, lombok.ast.Node node, JavaParser.ResolvedClass resolvedClass)
           
 void checkFolder(ResourceContext context, java.lang.String folderName)
           
 void checkInstruction(ClassContext context, org.objectweb.asm.tree.ClassNode classNode, org.objectweb.asm.tree.MethodNode method, org.objectweb.asm.tree.AbstractInsnNode instruction)
           
 lombok.ast.AstVisitor createJavaVisitor(JavaContext context)
           
 int[] getApplicableAsmNodeTypes()
           
 java.util.Collection<java.lang.String> getApplicableAttributes()
           
 java.util.List<java.lang.String> getApplicableCallNames()
           
 java.util.List<java.lang.String> getApplicableCallOwners()
           
 java.util.List<java.lang.String> getApplicableConstructorTypes()
           
 java.util.Collection<java.lang.String> getApplicableElements()
           
 java.util.EnumSet<Scope> getApplicableFiles()
           
 java.util.List<java.lang.String> getApplicableMethodNames()
           
 java.util.List<java.lang.Class<? extends lombok.ast.Node>> getApplicableNodeTypes()
           
 Speed getSpeed()
          Returns the expected speed of this detector
 Speed getSpeed(Issue issue)
          Returns the expected speed of this detector.
 void run(Context context)
          Runs the detector.
 void visitAttribute(XmlContext context, org.w3c.dom.Attr attribute)
           
 void visitBuildScript(Context context, java.util.Map<java.lang.String,java.lang.Object> sharedData)
           
 void visitConstructor(JavaContext context, lombok.ast.AstVisitor visitor, lombok.ast.ConstructorInvocation node, JavaParser.ResolvedMethod constructor)
           
 void visitDocument(XmlContext context, org.w3c.dom.Document document)
           
 void visitElement(XmlContext context, org.w3c.dom.Element element)
           
 void visitElementAfter(XmlContext context, org.w3c.dom.Element element)
           
 void visitMethod(JavaContext context, lombok.ast.AstVisitor visitor, lombok.ast.MethodInvocation node)
           
 void visitResourceReference(JavaContext context, lombok.ast.AstVisitor visitor, lombok.ast.Node node, java.lang.String type, java.lang.String name, boolean isFramework)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Detector

public Detector()
Method Detail

run

public void run(@NonNull
                Context context)
Runs the detector. This method will not be called for certain specialized detectors, such as Detector.XmlScanner and Detector.JavaScanner, where there are specialized analysis methods instead such as Detector.XmlScanner.visitElement(XmlContext, Element).

Parameters:
context - the context describing the work to be done

appliesTo

public boolean appliesTo(@NonNull
                         Context context,
                         @NonNull
                         java.io.File file)
Returns true if this detector applies to the given file

Parameters:
context - the context to check
file - the file in the context to check
Returns:
true if this detector applies to the given context and file

beforeCheckProject

public void beforeCheckProject(@NonNull
                               Context context)
Analysis is about to begin, perform any setup steps.

Parameters:
context - the context for the check referencing the project, lint client, etc

afterCheckProject

public void afterCheckProject(@NonNull
                              Context context)
Analysis has just been finished for the whole project, perform any cleanup or report issues that require project-wide analysis.

Parameters:
context - the context for the check referencing the project, lint client, etc

beforeCheckLibraryProject

public void beforeCheckLibraryProject(@NonNull
                                      Context context)
Analysis is about to begin for the given library project, perform any setup steps.

Parameters:
context - the context for the check referencing the project, lint client, etc

afterCheckLibraryProject

public void afterCheckLibraryProject(@NonNull
                                     Context context)
Analysis has just been finished for the given library project, perform any cleanup or report issues that require library-project-wide analysis.

Parameters:
context - the context for the check referencing the project, lint client, etc

beforeCheckFile

public void beforeCheckFile(@NonNull
                            Context context)
Analysis is about to be performed on a specific file, perform any setup steps.

Note: When this method is called at the beginning of checking an XML file, the context is guaranteed to be an instance of XmlContext, and similarly for a Java source file, the context will be a JavaContext and so on.

Parameters:
context - the context for the check referencing the file to be checked, the project, etc.

afterCheckFile

public void afterCheckFile(@NonNull
                           Context context)
Analysis has just been finished for a specific file, perform any cleanup or report issues found

Note: When this method is called at the end of checking an XML file, the context is guaranteed to be an instance of XmlContext, and similarly for a Java source file, the context will be a JavaContext and so on.

Parameters:
context - the context for the check referencing the file to be checked, the project, etc.

getSpeed

@NonNull
public Speed getSpeed()
Returns the expected speed of this detector

Returns:
the expected speed of this detector

getSpeed

@NonNull
public Speed getSpeed(@NonNull
                              Issue issue)
Returns the expected speed of this detector. The issue parameter is made available for subclasses which analyze multiple issues and which need to distinguish implementation cost by issue. If the detector does not analyze multiple issues or does not vary in speed by issue type, just override getSpeed() instead.

Parameters:
issue - the issue to look up the analysis speed for
Returns:
the expected speed of this detector

visitDocument

public void visitDocument(@NonNull
                          XmlContext context,
                          @NonNull
                          org.w3c.dom.Document document)

visitElement

public void visitElement(@NonNull
                         XmlContext context,
                         @NonNull
                         org.w3c.dom.Element element)

visitElementAfter

public void visitElementAfter(@NonNull
                              XmlContext context,
                              @NonNull
                              org.w3c.dom.Element element)

visitAttribute

public void visitAttribute(@NonNull
                           XmlContext context,
                           @NonNull
                           org.w3c.dom.Attr attribute)

getApplicableElements

@Nullable
public java.util.Collection<java.lang.String> getApplicableElements()

getApplicableAttributes

@Nullable
public java.util.Collection<java.lang.String> getApplicableAttributes()

getApplicableMethodNames

@Nullable
public java.util.List<java.lang.String> getApplicableMethodNames()

createJavaVisitor

@Nullable
public lombok.ast.AstVisitor createJavaVisitor(@NonNull
                                                        JavaContext context)

getApplicableNodeTypes

@Nullable
public java.util.List<java.lang.Class<? extends lombok.ast.Node>> getApplicableNodeTypes()

visitMethod

public void visitMethod(@NonNull
                        JavaContext context,
                        @Nullable
                        lombok.ast.AstVisitor visitor,
                        @NonNull
                        lombok.ast.MethodInvocation node)

appliesToResourceRefs

public boolean appliesToResourceRefs()

visitResourceReference

public 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)

applicableSuperClasses

@Nullable
public java.util.List<java.lang.String> applicableSuperClasses()

checkClass

public void checkClass(@NonNull
                       JavaContext context,
                       @Nullable
                       lombok.ast.ClassDeclaration declaration,
                       @NonNull
                       lombok.ast.Node node,
                       @NonNull
                       JavaParser.ResolvedClass resolvedClass)

getApplicableConstructorTypes

@Nullable
public java.util.List<java.lang.String> getApplicableConstructorTypes()

visitConstructor

public void visitConstructor(@NonNull
                             JavaContext context,
                             @Nullable
                             lombok.ast.AstVisitor visitor,
                             @NonNull
                             lombok.ast.ConstructorInvocation node,
                             @NonNull
                             JavaParser.ResolvedMethod constructor)

checkClass

public void checkClass(@NonNull
                       ClassContext context,
                       @NonNull
                       org.objectweb.asm.tree.ClassNode classNode)

getApplicableCallNames

@Nullable
public java.util.List<java.lang.String> getApplicableCallNames()

getApplicableCallOwners

@Nullable
public java.util.List<java.lang.String> getApplicableCallOwners()

checkCall

public void checkCall(@NonNull
                      ClassContext context,
                      @NonNull
                      org.objectweb.asm.tree.ClassNode classNode,
                      @NonNull
                      org.objectweb.asm.tree.MethodNode method,
                      @NonNull
                      org.objectweb.asm.tree.MethodInsnNode call)

getApplicableAsmNodeTypes

@Nullable
public int[] getApplicableAsmNodeTypes()

checkInstruction

public void checkInstruction(@NonNull
                             ClassContext context,
                             @NonNull
                             org.objectweb.asm.tree.ClassNode classNode,
                             @NonNull
                             org.objectweb.asm.tree.MethodNode method,
                             @NonNull
                             org.objectweb.asm.tree.AbstractInsnNode instruction)

appliesToFolder

public boolean appliesToFolder(@NonNull
                               Scope scope,
                               @Nullable
                               com.android.resources.ResourceFolderType folderType)

getApplicableFiles

@NonNull
public java.util.EnumSet<Scope> getApplicableFiles()

visitBuildScript

public void visitBuildScript(@NonNull
                             Context context,
                             java.util.Map<java.lang.String,java.lang.Object> sharedData)

checkFolder

public void checkFolder(@NonNull
                        ResourceContext context,
                        @NonNull
                        java.lang.String folderName)

checkBinaryResource

public void checkBinaryResource(@NonNull
                                ResourceContext context)

appliesTo

public boolean appliesTo(@NonNull
                         com.android.resources.ResourceFolderType folderType)