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. It might also be thought of as enforcing a rule, but "rule" is a bit overloaded in ADT terminology since ViewRules are used in the Rules API to allow views to specify designtime behavior in the graphical layout editor.

Each detector provides information about the issues it can find, such as an explanation of how to fix the issue, the priority, the category, etc. It also has an id which is used to persistently identify a particular type of error.

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. Proguard 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: Detectors might be constructed just once and shared between lint runs, so any per-detector state should be initialized and reset via the before/after methods.

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.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.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.
 boolean appliesTo(Context context, java.io.File file)
          Returns true if this detector applies to the given file
 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 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 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.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
 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 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

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)

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)