com.android.tools.lint.client.api
Class LintDriver

java.lang.Object
  extended by com.android.tools.lint.client.api.LintDriver

@Beta
public class LintDriver
extends java.lang.Object

Analyzes Android projects and files

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.


Constructor Summary
LintDriver(IssueRegistry registry, LintClient client)
          Creates a new LintDriver
 
Method Summary
 void addLintListener(LintListener listener)
          Adds a listener to be notified of lint progress
 void analyze(LintRequest request)
          Analyze the given files (which can point to Android projects or directories containing Android projects).
 void analyze(java.util.List<java.io.File> files, java.util.EnumSet<Scope> scope)
          Deprecated. use instead
 void cancel()
          Cancels the current lint run as soon as possible
 org.objectweb.asm.tree.ClassNode findClass(ClassContext context, java.lang.String type, int flags)
          Returns the ClassNode corresponding to the given type, if possible, or null
 Project findProjectFor(java.io.File file)
          Returns the project containing a given file, or null if not found.
 LintClient getClient()
          Returns the lint client requesting the lint check.
 org.objectweb.asm.tree.ClassNode getOuterClassNode(org.objectweb.asm.tree.ClassNode classNode)
          Returns the outer class node of the given class node
 int getPhase()
          Returns the current phase number.
 java.util.List<Project> getProjects()
          Returns the projects being analyzed
 java.lang.Object getProperty(java.lang.Object key)
          Returns the property previously stored with the given key, or null
 IssueRegistry getRegistry()
          Returns the current IssueRegistry.
 LintRequest getRequest()
          Returns the current request, which points to the original files to be checked, the original scope, the original LintClient, as well as the release mode.
 int getResourceFolderVersion(java.io.File resourceFile)
          Returns the folder version of the given file.
 java.util.EnumSet<Scope> getScope()
          Returns the scope for the lint job
 java.lang.String getSuperClass(java.lang.String name)
          Returns the super class for the given class name, which should be in VM format (e.g.
 boolean hasParserErrors()
          Returns whether lint has encountered any files with fatal parser errors (e.g.
 boolean isAbbreviating()
          Returns whether lint should abbreviate output when appropriate.
 boolean isCustomIssue(Issue issue)
          Returns true if the given issue is an issue that was loaded as a custom rule (e.g.
 boolean isSubclassOf(org.objectweb.asm.tree.ClassNode classNode, java.lang.String superClassName)
          Returns true if the given class is a subclass of the given super class.
 boolean isSuppressed(Issue issue, org.objectweb.asm.tree.ClassNode classNode)
          Returns whether the given issue is suppressed in the given class.
 boolean isSuppressed(Issue issue, org.objectweb.asm.tree.ClassNode classNode, org.objectweb.asm.tree.MethodNode method, org.objectweb.asm.tree.AbstractInsnNode instruction)
          Returns whether the given issue is suppressed in the given method.
 boolean isSuppressed(Issue issue, org.objectweb.asm.tree.FieldNode field)
          Returns whether the given issue is suppressed for the given field.
 boolean isSuppressed(JavaContext context, Issue issue, lombok.ast.Node scope)
          Returns whether the given issue is suppressed in the given parse tree node.
 boolean isSuppressed(XmlContext context, Issue issue, org.w3c.dom.Node node)
          Returns whether the given issue is suppressed in the given XML DOM node.
 void putProperty(java.lang.Object key, java.lang.Object value)
          Records a property for later retrieval by getProperty(Object)
 void removeLintListener(LintListener listener)
          Removes a listener such that it is no longer notified of progress
 void requestRepeat(Detector detector, java.util.EnumSet<Scope> scope)
          Requests another pass through the data for the given detector.
 void setAbbreviating(boolean abbreviating)
          Sets whether lint should abbreviate output when appropriate.
 void setHasParserErrors(boolean hasErrors)
          Sets whether lint has encountered files with fatal parser errors.
 void setScope(java.util.EnumSet<Scope> scope)
          Sets the scope for the lint job
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LintDriver

public LintDriver(@NonNull
                  IssueRegistry registry,
                  @NonNull
                  LintClient client)
Creates a new LintDriver

Parameters:
registry - The registry containing issues to be checked
client - the tool wrapping the analyzer, such as an IDE or a CLI
Method Detail

cancel

public void cancel()
Cancels the current lint run as soon as possible


getScope

@NonNull
public java.util.EnumSet<Scope> getScope()
Returns the scope for the lint job

Returns:
the scope, never null

setScope

public void setScope(@NonNull
                     java.util.EnumSet<Scope> scope)
Sets the scope for the lint job

Parameters:
scope - the scope to use

getClient

@NonNull
public LintClient getClient()
Returns the lint client requesting the lint check. This may not be the same instance as the one passed in to this driver; lint uses a wrapper which performs additional validation to ensure that for example badly behaved detectors which report issues that have been disabled will get muted without the real lint client getting notified. Thus, this LintClient is suitable for use by detectors to look up a client to for example get location handles from, but tool handling code should never try to cast this client back to their original lint client. For the original lint client, use LintRequest instead.

Returns:
the client, never null

getRequest

@NonNull
public LintRequest getRequest()
Returns the current request, which points to the original files to be checked, the original scope, the original LintClient, as well as the release mode.

Returns:
the request

putProperty

public void putProperty(@NonNull
                        java.lang.Object key,
                        @Nullable
                        java.lang.Object value)
Records a property for later retrieval by getProperty(Object)

Parameters:
key - the key to associate the value with
value - the value, or null to remove a previous binding

getProperty

@Nullable
public java.lang.Object getProperty(@NonNull
                                             java.lang.Object key)
Returns the property previously stored with the given key, or null

Parameters:
key - the key
Returns:
the value or null if not found

getPhase

public int getPhase()
Returns the current phase number. The first pass is numbered 1. Only one pass will be performed, unless a Detector calls requestRepeat(com.android.tools.lint.detector.api.Detector, java.util.EnumSet).

Returns:
the current phase, usually 1

getRegistry

@NonNull
public IssueRegistry getRegistry()
Returns the current IssueRegistry.

Returns:
the current IssueRegistry

findProjectFor

@Nullable
public Project findProjectFor(@NonNull
                                       java.io.File file)
Returns the project containing a given file, or null if not found. This searches only among the currently checked project and its library projects, not among all possible projects being scanned sequentially.

Parameters:
file - the file to be checked
Returns:
the corresponding project, or null if not found

setAbbreviating

public void setAbbreviating(boolean abbreviating)
Sets whether lint should abbreviate output when appropriate.

Parameters:
abbreviating - true to abbreviate output, false to include everything

isAbbreviating

public boolean isAbbreviating()
Returns whether lint should abbreviate output when appropriate.

Returns:
true if lint should abbreviate output, false when including everything

hasParserErrors

public boolean hasParserErrors()
Returns whether lint has encountered any files with fatal parser errors (e.g. broken source code, or even broken parsers)

This is useful for checks that need to make sure they've seen all data in order to be conclusive (such as an unused resource check).

Returns:
true if any files were not properly processed because they contained parser errors

setHasParserErrors

public void setHasParserErrors(boolean hasErrors)
Sets whether lint has encountered files with fatal parser errors.

Parameters:
hasErrors - whether parser errors have been encountered
See Also:
hasParserErrors()

getProjects

@NonNull
public java.util.List<Project> getProjects()
Returns the projects being analyzed

Returns:
the projects being analyzed

analyze

@Deprecated
public void analyze(@NonNull
                               java.util.List<java.io.File> files,
                               @Nullable
                               java.util.EnumSet<Scope> scope)
Deprecated. use instead

Analyze the given file (which can point to an Android project). Issues found are reported to the associated LintClient.

Parameters:
files - the files and directories to be analyzed
scope - the scope of the analysis; detectors with a wider scope will not be run. If null, the scope will be inferred from the files.

analyze

public void analyze(@NonNull
                    LintRequest request)
Analyze the given files (which can point to Android projects or directories containing Android projects). Issues found are reported to the associated LintClient.

Note that the LintDriver is not multi thread safe or re-entrant; if you want to run potentially overlapping lint jobs, create a separate driver for each job.

Parameters:
request - the files and directories to be analyzed

isCustomIssue

public boolean isCustomIssue(@NonNull
                             Issue issue)
Returns true if the given issue is an issue that was loaded as a custom rule (e.g. a 3rd-party library provided the detector, it's not built in)

Parameters:
issue - the issue to be looked up
Returns:
true if this is a custom (non-builtin) check

getSuperClass

@Nullable
public java.lang.String getSuperClass(@NonNull
                                               java.lang.String name)
Returns the super class for the given class name, which should be in VM format (e.g. java/lang/Integer, not java.lang.Integer). If the super class is not known, returns null. This can happen if the given class is not a known class according to the project or its libraries, for example because it refers to one of the core libraries which are not analyzed by lint.

Parameters:
name - the fully qualified class name
Returns:
the corresponding super class name (in VM format), or null if not known

isSubclassOf

public boolean isSubclassOf(@NonNull
                            org.objectweb.asm.tree.ClassNode classNode,
                            @NonNull
                            java.lang.String superClassName)
Returns true if the given class is a subclass of the given super class.

Parameters:
classNode - the class to check whether it is a subclass of the given super class name
superClassName - the fully qualified super class name (in VM format, e.g. java/lang/Integer, not java.lang.Integer.
Returns:
true if the given class is a subclass of the given super class

getOuterClassNode

public org.objectweb.asm.tree.ClassNode getOuterClassNode(@NonNull
                                                          org.objectweb.asm.tree.ClassNode classNode)
Returns the outer class node of the given class node

Parameters:
classNode - the inner class node
Returns:
the outer class node

findClass

@Nullable
public org.objectweb.asm.tree.ClassNode findClass(@NonNull
                                                           ClassContext context,
                                                           @NonNull
                                                           java.lang.String type,
                                                           int flags)
Returns the ClassNode corresponding to the given type, if possible, or null

Parameters:
type - the fully qualified type, using JVM signatures (/ and $, not . as path separators)
flags - the ASM flags to pass to the ClassReader, normally 0 but can for example be ClassReader.SKIP_CODE and/oor ClassReader.SKIP_DEBUG
Returns:
the class node for the type, or null

addLintListener

public void addLintListener(@NonNull
                            LintListener listener)
Adds a listener to be notified of lint progress

Parameters:
listener - the listener to be added

removeLintListener

public void removeLintListener(@NonNull
                               LintListener listener)
Removes a listener such that it is no longer notified of progress

Parameters:
listener - the listener to be removed

requestRepeat

public void requestRepeat(@NonNull
                          Detector detector,
                          @Nullable
                          java.util.EnumSet<Scope> scope)
Requests another pass through the data for the given detector. This is typically done when a detector needs to do more expensive computation, but it only wants to do this once it knows that an error is present, or once it knows more specifically what to check for.

Parameters:
detector - the detector that should be included in the next pass. Note that the lint runner may refuse to run more than a couple of runs.
scope - the scope to be revisited. This must be a subset of the current scope (getScope(), and it is just a performance hint; in particular, the detector should be prepared to be called on other scopes as well (since they may have been requested by other detectors). You can pall null to indicate "all".

isSuppressed

public boolean isSuppressed(@Nullable
                            Issue issue,
                            @NonNull
                            org.objectweb.asm.tree.ClassNode classNode,
                            @NonNull
                            org.objectweb.asm.tree.MethodNode method,
                            @Nullable
                            org.objectweb.asm.tree.AbstractInsnNode instruction)
Returns whether the given issue is suppressed in the given method.

Parameters:
issue - the issue to be checked, or null to just check for "all"
classNode - the class containing the issue
method - the method containing the issue
instruction - the instruction within the method, if any
Returns:
true if there is a suppress annotation covering the specific issue on this method

isSuppressed

public boolean isSuppressed(@Nullable
                            Issue issue,
                            @NonNull
                            org.objectweb.asm.tree.FieldNode field)
Returns whether the given issue is suppressed for the given field.

Parameters:
issue - the issue to be checked, or null to just check for "all"
field - the field potentially annotated with a suppress annotation
Returns:
true if there is a suppress annotation covering the specific issue on this field

isSuppressed

public boolean isSuppressed(@Nullable
                            Issue issue,
                            @NonNull
                            org.objectweb.asm.tree.ClassNode classNode)
Returns whether the given issue is suppressed in the given class.

Parameters:
issue - the issue to be checked, or null to just check for "all"
classNode - the class containing the issue
Returns:
true if there is a suppress annotation covering the specific issue in this class

isSuppressed

public boolean isSuppressed(@Nullable
                            JavaContext context,
                            @NonNull
                            Issue issue,
                            @Nullable
                            lombok.ast.Node scope)
Returns whether the given issue is suppressed in the given parse tree node.

Parameters:
context - the context for the source being scanned
issue - the issue to be checked, or null to just check for "all"
scope - the AST node containing the issue
Returns:
true if there is a suppress annotation covering the specific issue in this class

isSuppressed

public boolean isSuppressed(@Nullable
                            XmlContext context,
                            @NonNull
                            Issue issue,
                            @Nullable
                            org.w3c.dom.Node node)
Returns whether the given issue is suppressed in the given XML DOM node.

Parameters:
issue - the issue to be checked, or null to just check for "all"
node - the DOM node containing the issue
Returns:
true if there is a suppress annotation covering the specific issue in this class

getResourceFolderVersion

public int getResourceFolderVersion(@NonNull
                                    java.io.File resourceFile)
Returns the folder version of the given file. For example, for the file values-v14/foo.xml, it returns 14.

Parameters:
resourceFile - the file to be checked
Returns:
the folder version, or -1 if no specific version was specified