@Beta
public abstract class LintClient
extends java.lang.Object
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.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
LintClient.ClassPathInfo
Information about class paths (sources, class files and libraries)
usually associated with a project.
|
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
CLIENT_CLI
The client name returned by
getClientName() when running in
the CLI (command line interface) version of lint, lint |
static java.lang.String |
CLIENT_GRADLE
The client name returned by
getClientName() when running in
Gradle |
static java.lang.String |
CLIENT_STUDIO
The client name returned by
getClientName() when running in
Android Studio/IntelliJ IDEA |
static java.lang.String |
CLIENT_UNKNOWN
The client name returned by
getClientName() when running in
some unknown client |
protected java.util.Map<java.io.File,Project> |
dirToProject
A map from directory to existing projects, or null.
|
protected static com.android.utils.Pair<java.io.File,org.w3c.dom.Node> |
NOT_FOUND |
protected java.util.Set<java.io.File> |
projectDirs |
protected com.android.sdklib.repository.AndroidSdkHandler |
sdk |
protected java.util.IdentityHashMap<org.w3c.dom.Node,com.android.utils.Pair<java.io.File,org.w3c.dom.Node>> |
sourceNodeCache
Cache used by
findManifestSourceNode(Node) |
protected com.android.sdklib.IAndroidTarget[] |
targets |
| Modifier | Constructor and Description |
|---|---|
protected |
LintClient() |
protected |
LintClient(java.lang.String clientName) |
| Modifier and Type | Method and Description |
|---|---|
IssueRegistry |
addCustomLintRules(IssueRegistry registry)
Adds in any custom lint rules and returns the result as a new issue registry,
or the same one if no custom rules were found
|
boolean |
checkForSuppressComments()
Returns whether lint should look for suppress comments.
|
void |
closeConnection(java.net.URLConnection connection)
Closes a connection previously returned by
openConnection(URL) |
protected Project |
createProject(java.io.File dir,
java.io.File referenceDir)
Create a project for the given directory
|
Location.Handle |
createResourceItemHandle(com.android.ide.common.res2.ResourceItem item)
For a lint client which supports resource items (via
supportsProjectResources())
return a handle for a resource item |
java.util.Map<java.lang.String,java.lang.String> |
createSuperClassMap(Project project)
Creates a super class map for the given project.
|
java.lang.ClassLoader |
createUrlClassLoader(java.net.URL[] urls,
java.lang.ClassLoader parent)
Creates a
ClassLoader which can load in a set of Jar files. |
protected void |
disposeProjects(java.util.Collection<Project> knownProjects)
Perform any post-analysis cleaninup of the full set of projects that lint was
run on, if necessary.
|
java.util.List<java.io.File> |
findGlobalRuleJars()
Finds any custom lint rule jars that should be included for analysis,
regardless of project.
|
Location |
findManifestSourceLocation(org.w3c.dom.Node mergedNode)
Returns the location for a given node from a merged manifest file.
|
com.android.utils.Pair<java.io.File,org.w3c.dom.Node> |
findManifestSourceNode(org.w3c.dom.Node mergedNode)
For the given node from a merged manifest, find the corresponding
source manifest node, if possible
|
java.io.File |
findResource(java.lang.String relativePath)
Locates an SDK resource (relative to the SDK root directory).
|
java.util.List<java.io.File> |
findRuleJars(Project project)
Finds any custom lint rule jars that should be included for analysis
in the given project
|
java.util.List<java.io.File> |
getAssetFolders(Project project)
Returns the asset folders.
|
com.android.sdklib.BuildToolInfo |
getBuildTools(Project project)
Returns the specific version of the build tools being used for the given project, if known
|
java.io.File |
getCacheDir(boolean create)
Deprecated.
Use
getCacheDir(String, boolean) instead |
java.io.File |
getCacheDir(java.lang.String name,
boolean create)
Returns a suitable location for storing cache files of a given named
type.
|
protected LintClient.ClassPathInfo |
getClassPath(Project project)
Considers the given project as an Eclipse project and returns class path
information for the project - the source folder(s), the output folder and
any libraries.
|
static java.lang.String |
getClientName()
Returns the name of the embedding client.
|
java.lang.String |
getClientRevision()
Returns the version number of this lint client, if known
|
com.android.sdklib.IAndroidTarget |
getCompileTarget(Project project)
Returns the compile target to use for the given project
|
Configuration |
getConfiguration(Project project,
LintDriver driver)
Returns a configuration for use by the given project.
|
java.lang.String |
getDisplayPath(java.io.File file)
Formats the given path
|
java.util.List<java.io.File> |
getGeneratedSourceFolders(Project project)
Returns the list of generated source folders
|
int |
getHighestKnownApiLevel()
Returns the highest known API level.
|
java.util.List<java.io.File> |
getJavaClassFolders(Project project)
Returns the list of output folders for class files
|
java.util.List<java.io.File> |
getJavaLibraries(Project project,
boolean includeProvided)
Returns the list of Java libraries
|
abstract JavaParser |
getJavaParser(Project project)
Returns a
JavaParser to use to parse Java |
java.util.List<java.io.File> |
getJavaSourceFolders(Project project)
Returns the list of source folders for Java source files
|
java.util.Collection<Project> |
getKnownProjects()
Returns the list of known projects (projects registered via
getProject(File, File) |
org.w3c.dom.Document |
getMergedManifest(Project project)
Returns the merged manifest of the given project.
|
Project |
getProject(java.io.File dir,
java.io.File referenceDir)
Returns a project for the given directory.
|
java.lang.String |
getProjectName(Project project)
Returns the name of the given project
|
com.android.ide.common.res2.AbstractResourceRepository |
getProjectResources(Project project,
boolean includeDependencies)
Deprecated.
|
com.android.repository.api.ProgressIndicator |
getRepositoryLogger()
Returns a repository logger used by this client.
|
java.util.List<java.io.File> |
getResourceFolders(Project project)
Returns the resource folders.
|
com.android.ide.common.res2.AbstractResourceRepository |
getResourceRepository(Project project,
boolean includeModuleDependencies,
boolean includeLibraries)
Returns the project resources, if available
|
com.android.ide.common.repository.ResourceVisibilityLookup.Provider |
getResourceVisibilityProvider()
Returns a shared
ResourceVisibilityLookup.Provider |
com.android.sdklib.repository.AndroidSdkHandler |
getSdk()
Returns the SDK installation (used to look up platforms etc)
|
java.io.File |
getSdkHome()
Returns the File pointing to the user's SDK install area.
|
SdkInfo |
getSdkInfo(Project project)
Returns the
SdkInfo to use for the given project. |
java.lang.String |
getSuperClass(Project project,
java.lang.String name)
Returns the super class for the given class name, which should be in VM
format (e.g.
|
com.android.sdklib.IAndroidTarget[] |
getTargets()
Returns all the
IAndroidTarget versions installed in the user's SDK install
area. |
java.util.List<java.io.File> |
getTestLibraries(Project project)
Returns the list of libraries needed to compile the test source files
|
java.util.List<java.io.File> |
getTestSourceFolders(Project project)
Returns the list of source folders for test source files
|
abstract UastParser |
getUastParser(Project project)
Returns a
JavaParser to use to parse Java |
abstract XmlParser |
getXmlParser()
Returns a
XmlParser to use to parse XML |
protected void |
initializeProjects(java.util.Collection<Project> knownProjects)
Perform any startup initialization of the full set of projects that lint will be
run on, if necessary.
|
static boolean |
isGradle()
Returns true if the embedding client currently running lint is Gradle
|
boolean |
isGradleProject(Project project)
Returns true if this project is a Gradle-based Android project
|
boolean |
isMergeManifestNode(org.w3c.dom.Node node)
Returns true if the given node is part of a merged manifest document
(already configured via
resolveMergeManifestSources(Document, Object)) |
boolean |
isProjectDirectory(java.io.File dir)
Returns true if the given directory is a lint project directory.
|
static boolean |
isStudio()
Returns true if the embedding client currently running lint is Android Studio
(or IntelliJ IDEA)
|
java.lang.Boolean |
isSubclassOf(Project project,
java.lang.String name,
java.lang.String superClassName)
Checks whether the given name is a subclass of the given super class.
|
abstract void |
log(Severity severity,
java.lang.Throwable exception,
java.lang.String format,
java.lang.Object... args)
Send an exception or error message to the log
|
void |
log(java.lang.Throwable exception,
java.lang.String format,
java.lang.Object... args)
Send an exception or error message (with warning severity) to the log
|
java.net.URLConnection |
openConnection(java.net.URL url)
Opens a URL connection.
|
java.net.URLConnection |
openConnection(java.net.URL url,
int timeout)
Opens a URL connection.
|
byte[] |
readBytes(java.io.File file)
Reads the given binary file and returns the content as a byte array.
|
abstract java.lang.CharSequence |
readFile(java.io.File file)
Reads the given text file and returns the content as a string
|
void |
registerProject(java.io.File dir,
Project project)
Registers the given project for the given directory.
|
java.lang.Class<? extends Detector> |
replaceDetector(java.lang.Class<? extends Detector> detectorClass)
Returns an optimal detector, if applicable.
|
abstract void |
report(Context context,
Issue issue,
Severity severity,
Location location,
java.lang.String message,
TextFormat format,
LintFix fix)
Report the given issue.
|
void |
resolveMergeManifestSources(org.w3c.dom.Document mergedManifest,
java.lang.Object reportFile)
Record that the given document corresponds to a merged manifest file;
locations from this document should attempt to resolve back to the original
source location
|
void |
runReadAction(java.lang.Runnable runnable)
Runs the given runnable under a readlock such that it can access the PSI
|
boolean |
supportsProjectResources()
Returns true if this client supports project resource repository lookup via
getResourceRepository(Project, boolean, boolean) |
protected java.util.Map<java.io.File,Project> dirToProject
protected java.util.Set<java.io.File> projectDirs
protected com.android.sdklib.IAndroidTarget[] targets
protected com.android.sdklib.repository.AndroidSdkHandler sdk
@Nullable protected java.util.IdentityHashMap<org.w3c.dom.Node,com.android.utils.Pair<java.io.File,org.w3c.dom.Node>> sourceNodeCache
findManifestSourceNode(Node)protected static final com.android.utils.Pair<java.io.File,org.w3c.dom.Node> NOT_FOUND
public static final java.lang.String CLIENT_STUDIO
getClientName() when running in
Android Studio/IntelliJ IDEApublic static final java.lang.String CLIENT_GRADLE
getClientName() when running in
Gradlepublic static final java.lang.String CLIENT_CLI
getClientName() when running in
the CLI (command line interface) version of lint, lintpublic static final java.lang.String CLIENT_UNKNOWN
getClientName() when running in
some unknown clientprotected LintClient(@NonNull
java.lang.String clientName)
protected LintClient()
@NonNull public Configuration getConfiguration(@NonNull Project project, @Nullable LintDriver driver)
By default this method returns a DefaultConfiguration.
project - the project to obtain a configuration fordriver - the current driver, if anypublic abstract void report(@NonNull
Context context,
@NonNull
Issue issue,
@NonNull
Severity severity,
@NonNull
Location location,
@NonNull
java.lang.String message,
@NonNull
TextFormat format,
@Nullable
LintFix fix)
getConfiguration(Project, LintDriver) has reported the corresponding
issue as enabled and has not filtered out the issue with its
Configuration.ignore(Context, Issue, Location, String) method.
context - the context used by the detector when the issue was foundissue - the issue that was foundseverity - the severity of the issuelocation - the location of the issuemessage - the associated user messageformat - the format of the description and location descriptionsfix - an optional set of extra data provided by the detector for this issue; this
is intended to pass metadata to the IDE to help construct quickfixes without
having to parse error messages (which is brittle) or worse having to include
information in the error message (for later parsing) which is required by the
quickfix but not really helpful in the error message itself (such as the
maxVersion for a permission tag to be added to thepublic void log(@Nullable
java.lang.Throwable exception,
@Nullable
java.lang.String format,
@Nullable
java.lang.Object... args)
exception - the exception, possibly nullformat - the error message using String.format(java.lang.String, java.lang.Object...) syntax, possibly null
(though in that case the exception should not be null)args - any arguments for the format stringpublic abstract void log(@NonNull
Severity severity,
@Nullable
java.lang.Throwable exception,
@Nullable
java.lang.String format,
@Nullable
java.lang.Object... args)
severity - the severity of the warningexception - the exception, possibly nullformat - the error message using String.format(java.lang.String, java.lang.Object...) syntax, possibly null
(though in that case the exception should not be null)args - any arguments for the format string@NonNull public abstract XmlParser getXmlParser()
XmlParser to use to parse XMLXmlParser, or null if this client does not support
XML analysis@Nullable public abstract JavaParser getJavaParser(@Nullable Project project)
JavaParser to use to parse Javaproject - the project to parse, if known (this can be used to look up
the class path for type attribution etc, and it can also be used
to more efficiently process a set of files, for example to
perform type attribution for multiple units in a single pass)JavaParser, or null if this client does not
support Java analysis@Nullable public abstract UastParser getUastParser(@Nullable Project project)
JavaParser to use to parse Javaproject - the project to parse, if known (this can be used to look up
the class path for type attribution etc, and it can also be used
to more efficiently process a set of files, for example to
perform type attribution for multiple units in a single pass)JavaParser, or null if this client does not
support Java analysis@NonNull public java.lang.Class<? extends Detector> replaceDetector(@NonNull java.lang.Class<? extends Detector> detectorClass)
detectorClass - the class of the detector to be replaced@NonNull
public abstract java.lang.CharSequence readFile(@NonNull
java.io.File file)
file - the file to read@NonNull
public byte[] readBytes(@NonNull
java.io.File file)
throws java.io.IOException
file - the file to readjava.io.IOException - if the file does not exist, or if the file cannot be
read for some reason@NonNull
public java.util.List<java.io.File> getJavaSourceFolders(@NonNull
Project project)
project - the project to look up Java source file locations for@NonNull
public java.util.List<java.io.File> getGeneratedSourceFolders(@NonNull
Project project)
project - the project to look up generated source file locations for@NonNull
public java.util.List<java.io.File> getJavaClassFolders(@NonNull
Project project)
project - the project to look up class file locations for@NonNull
public java.util.List<java.io.File> getJavaLibraries(@NonNull
Project project,
boolean includeProvided)
project - the project to look up jar dependencies forincludeProvided - If true, included provided libraries too (libraries that are not
packaged with the app, but are provided for compilation purposes and
are assumed to be present in the running environment)@NonNull
public java.util.List<java.io.File> getTestSourceFolders(@NonNull
Project project)
project - the project to look up test source file locations for@NonNull
public java.util.List<java.io.File> getTestLibraries(@NonNull
Project project)
project - the project to look up test source file locations for@NonNull
public java.util.List<java.io.File> getResourceFolders(@NonNull
Project project)
project - the project to look up the resource folder for@NonNull
public java.util.List<java.io.File> getAssetFolders(@NonNull
Project project)
project - the project to look up the asset folder for@NonNull public SdkInfo getSdkInfo(@NonNull Project project)
SdkInfo to use for the given project.@Deprecated @Nullable public java.io.File getCacheDir(boolean create)
getCacheDir(String, boolean) instead$ANDROID_SDK_CACHE_DIR (though note that specific
lint integrations may not honor that environment variable; for example,
in Gradle the cache directory will always be build/intermediates/lint-cache/.)create - if true, attempt to create the cache dir if it does not
exist@Nullable
public java.io.File getCacheDir(@Nullable
java.lang.String name,
boolean create)
Note that in some cases lint may interpret that name to provide an alternate cache. For example, when lint runs in the IDE it normally uses the same cache as lint on the command line (~/.android/cache), but specifically for the cache for maven.google.com repository versions, it will instead point to the same cache directory as the IDE is already using for non-lint purposes, in order to share data that may already exist there.
Note that the cache directory may not exist. You can override the default location
using $ANDROID_SDK_CACHE_DIR (though note that specific
lint integrations may not honor that environment variable; for example,
in Gradle the cache directory will always be build/intermediates/lint-cache/.)
create - if true, attempt to create the cache dir if it does not
exist@Nullable public java.io.File getSdkHome()
@Nullable
public java.io.File findResource(@NonNull
java.lang.String relativePath)
TODO: Consider switching to a URL return type instead.
relativePath - A relative path (using File.separator to
separate path components) to the given resourceFile pointing to the resource, or null if it does not
existpublic boolean isGradleProject(Project project)
project - the project to check@NonNull protected LintClient.ClassPathInfo getClassPath(@NonNull Project project)
Callers will not cache calls to this method, so if it's expensive to compute the classpath info, this method should perform its own caching.
project - the project to look up class path info for@NonNull public Project getProject(@NonNull java.io.File dir, @NonNull java.io.File referenceDir)
dir - the directory containing the projectreferenceDir - See Project.getReferenceDir().public java.util.Collection<Project> getKnownProjects()
getProject(File, File)public void registerProject(@NonNull
java.io.File dir,
@NonNull
Project project)
dir - the directory of the project, which must be uniqueproject - the project@NonNull protected Project createProject(@NonNull java.io.File dir, @NonNull java.io.File referenceDir)
dir - the root directory of the projectreferenceDir - See Project.getReferenceDir().protected void initializeProjects(@NonNull
java.util.Collection<Project> knownProjects)
knownProjects - the list of projectsprotected void disposeProjects(@NonNull
java.util.Collection<Project> knownProjects)
knownProjects - the list of projects@NonNull
public java.lang.String getProjectName(@NonNull
Project project)
project - the project to look up@NonNull public com.android.sdklib.IAndroidTarget[] getTargets()
IAndroidTarget versions installed in the user's SDK install
area.@Nullable public com.android.sdklib.repository.AndroidSdkHandler getSdk()
@Nullable
public com.android.sdklib.IAndroidTarget getCompileTarget(@NonNull
Project project)
project - the project in questionpublic int getHighestKnownApiLevel()
@Nullable
public com.android.sdklib.BuildToolInfo getBuildTools(@NonNull
Project project)
project - the project in question@Nullable
public java.lang.String getSuperClass(@NonNull
Project project,
@NonNull
java.lang.String name)
This is typically not necessary, since lint analyzes all the available classes. However, if this lint client is invoking lint in an incremental context (for example, an IDE offering incremental analysis of a single source file), then lint may not see all the classes, and the client can provide its own super class lookup.
project - the project containing the classname - the fully qualified class name@NonNull
public java.util.Map<java.lang.String,java.lang.String> createSuperClassMap(@NonNull
Project project)
project - the project to initialize the super class with; this will include
local classes as well as any local .jar libraries; not transitive
dependencies@Nullable
public java.lang.Boolean isSubclassOf(@NonNull
Project project,
@NonNull
java.lang.String name,
@NonNull
java.lang.String superClassName)
Boolean.TRUE or Boolean.FALSE.
Note that the class names are in internal VM format (java/lang/Integer, not java.lang.Integer, and using $ rather than . for inner classes).
project - the project context to look up the class inname - the name of the class to be checkedsuperClassName - the name of the super class to compare to@NonNull public java.util.List<java.io.File> findGlobalRuleJars()
The default implementation locates custom lint jars in ~/.android/lint/ and in $ANDROID_LINT_JARS
@NonNull
public java.util.List<java.io.File> findRuleJars(@NonNull
Project project)
project - the project to look up rule jars from@Nullable
public java.net.URLConnection openConnection(@NonNull
java.net.URL url)
throws java.io.IOException
url - the URL to readURLConnection or nulljava.io.IOException - if any kind of IO exception occurs@Nullable
public java.net.URLConnection openConnection(@NonNull
java.net.URL url,
int timeout)
throws java.io.IOException
url - the URL to readtimeout - the timeout to apply for HTTP connections (or 0 to wait indefinitely)URLConnection or nulljava.io.IOException - if any kind of IO exception occurs including timeoutspublic void closeConnection(@NonNull
java.net.URLConnection connection)
throws java.io.IOException
openConnection(URL)java.io.IOExceptionpublic boolean isProjectDirectory(@NonNull
java.io.File dir)
dir - the directory to checkpublic boolean checkForSuppressComments()
public IssueRegistry addCustomLintRules(@NonNull IssueRegistry registry)
registry - the main registry to add rules topublic java.lang.ClassLoader createUrlClassLoader(@NonNull
java.net.URL[] urls,
@NonNull
java.lang.ClassLoader parent)
ClassLoader which can load in a set of Jar files.urls - the URLsparent - the parent class loader@Nullable
public org.w3c.dom.Document getMergedManifest(@NonNull
Project project)
public void resolveMergeManifestSources(@NonNull
org.w3c.dom.Document mergedManifest,
@NonNull
java.lang.Object reportFile)
mergedManifest - the document for the merged manifestreportFile - the manifest merger report file, or the report itselfpublic boolean isMergeManifestNode(@NonNull
org.w3c.dom.Node node)
resolveMergeManifestSources(Document, Object))node - the node to look up@Nullable
public com.android.utils.Pair<java.io.File,org.w3c.dom.Node> findManifestSourceNode(@NonNull
org.w3c.dom.Node mergedNode)
mergedNode - the node from the merged manifest@Nullable public Location findManifestSourceLocation(@NonNull org.w3c.dom.Node mergedNode)
findManifestSourceNode(Node) and
XmlParser.getLocation(File, Node)@NonNull public java.lang.String getDisplayPath(java.io.File file)
file - the path to compute a display name forTextFormat.RAW text format (e.g.
with backslashes, asterisks etc escaped)public boolean supportsProjectResources()
getResourceRepository(Project, boolean, boolean)@Deprecated @Nullable public com.android.ide.common.res2.AbstractResourceRepository getProjectResources(Project project, boolean includeDependencies)
getResourceRepository(com.android.tools.lint.detector.api.Project, boolean, boolean) insteadincludeDependencies - if true, include merged view of all dependencies@Nullable public com.android.ide.common.res2.AbstractResourceRepository getResourceRepository(Project project, boolean includeModuleDependencies, boolean includeLibraries)
includeModuleDependencies - if true, include merged view of all module dependenciesincludeLibraries - if true, include merged view of all library dependencies
(this also requires all module dependencies)@NonNull public Location.Handle createResourceItemHandle(@NonNull com.android.ide.common.res2.ResourceItem item)
supportsProjectResources())
return a handle for a resource itemitem - the resource item to look up a location handle for@NonNull public com.android.ide.common.repository.ResourceVisibilityLookup.Provider getResourceVisibilityProvider()
ResourceVisibilityLookup.Provider@NonNull public static java.lang.String getClientName()
CLIENT_STUDIO, CLIENT_GRADLE, CLIENT_CLI
etc but other values too as lint is integrated in other embedding contexts.@Nullable public java.lang.String getClientRevision()
public static boolean isStudio()
public static boolean isGradle()
public void runReadAction(@NonNull
java.lang.Runnable runnable)
runnable - the runnable to be run@NonNull public com.android.repository.api.ProgressIndicator getRepositoryLogger()