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

java.lang.Object
  extended by com.android.tools.lint.detector.api.Context
Direct Known Subclasses:
ClassContext, JavaContext, XmlContext

@Beta
public class Context
extends java.lang.Object

Context passed to the detectors during an analysis run. It provides information about the file being analyzed, it allows shared properties (so the detectors can share results), etc.

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.


Field Summary
 java.io.File file
          The file being checked.
protected  LintDriver mDriver
          The driver running through the checks
 
Constructor Summary
Context(LintDriver driver, Project project, Project main, java.io.File file)
          Construct a new Context
 
Method Summary
 boolean containsCommentSuppress()
          Returns whether this file contains any suppress comment markers
 LintClient getClient()
          Returns the lint client requesting the lint check
 Configuration getConfiguration()
          Returns the configuration for this project.
 java.lang.String getContents()
          Returns the contents of the file.
 LintDriver getDriver()
          Returns the driver running through the lint checks
 int getFolderVersion()
          Returns the folder version.
static int getFolderVersion(java.io.File file)
          Returns the folder version of the given file.
 Project getMainProject()
          Returns the main project if this project is a library project, or self if this is not a library project.
 int getPhase()
          Returns the current phase number.
 Project getProject()
          Returns the project containing the file being checked
 java.lang.Object getProperty(java.lang.String name)
          Returns the value of the given named property, or null.
 java.util.EnumSet<Scope> getScope()
          Returns the scope for the lint job
 SdkInfo getSdkInfo()
          Gets the SDK info for the current project.
protected  java.lang.String getSuppressCommentPrefix()
          Returns the comment marker used in Studio to suppress statements for language, if any
 boolean isEnabled(Issue issue)
          Returns false if the given issue has been disabled.
 boolean isSuppressedWithComment(int startOffset, Issue issue)
          Returns true if the given issue is suppressed at the given character offset in the file's contents
 void log(java.lang.Throwable exception, java.lang.String format, java.lang.Object... args)
          Send an exception to the log.
 void report(Issue issue, Location location, java.lang.String message, java.lang.Object data)
          Reports an issue.
 void requestRepeat(Detector detector, java.util.EnumSet<Scope> scope)
          Requests another pass through the data for the given detector.
 void setProperty(java.lang.String name, java.lang.Object value)
          Sets the value of the given named property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

file

public final java.io.File file
The file being checked. Note that this may not always be to a concrete file. For example, in the Detector.beforeCheckProject(Context) method, the context file is the directory of the project.


mDriver

protected final LintDriver mDriver
The driver running through the checks

Constructor Detail

Context

public Context(@NonNull
               LintDriver driver,
               @NonNull
               Project project,
               @Nullable
               Project main,
               @NonNull
               java.io.File file)
Construct a new Context

Parameters:
driver - the driver running through the checks
project - the project containing the file being checked
main - the main project if this project is a library project, or null if this is not a library project. The main project is the root project of all library projects, not necessarily the directly including project.
file - the file being checked
Method Detail

getScope

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

Returns:
the scope, never null

getConfiguration

@NonNull
public Configuration getConfiguration()
Returns the configuration for this project.

Returns:
the configuration, never null

getProject

@NonNull
public Project getProject()
Returns the project containing the file being checked

Returns:
the project, never null

getMainProject

@NonNull
public Project getMainProject()
Returns the main project if this project is a library project, or self if this is not a library project. The main project is the root project of all library projects, not necessarily the directly including project.

Returns:
the main project, never null

getClient

@NonNull
public LintClient getClient()
Returns the lint client requesting the lint check

Returns:
the client, never null

getDriver

@NonNull
public LintDriver getDriver()
Returns the driver running through the lint checks

Returns:
the driver

getContents

@Nullable
public java.lang.String getContents()
Returns the contents of the file. This may not be the contents of the file on disk, since it delegates to the LintClient, which in turn may decide to return the current edited contents of the file open in an editor.

Returns:
the contents of the given file, or null if an error occurs.

getProperty

@Nullable
public java.lang.Object getProperty(java.lang.String name)
Returns the value of the given named property, or null.

Parameters:
name - the name of the property
Returns:
the corresponding value, or null

setProperty

public void setProperty(@NonNull
                        java.lang.String name,
                        @Nullable
                        java.lang.Object value)
Sets the value of the given named property.

Parameters:
name - the name of the property
value - the corresponding value

getSdkInfo

@NonNull
public SdkInfo getSdkInfo()
Gets the SDK info for the current project.

Returns:
the SDK info for the current project, never null

isEnabled

public boolean isEnabled(@NonNull
                         Issue issue)
Returns false if the given issue has been disabled. Convenience wrapper around Configuration.getSeverity(Issue).

Parameters:
issue - the issue to check
Returns:
false if the issue has been disabled

report

public void report(@NonNull
                   Issue issue,
                   @Nullable
                   Location location,
                   @NonNull
                   java.lang.String message,
                   @Nullable
                   java.lang.Object data)
Reports an issue. Convenience wrapper around LintClient.report(com.android.tools.lint.detector.api.Context, com.android.tools.lint.detector.api.Issue, com.android.tools.lint.detector.api.Severity, com.android.tools.lint.detector.api.Location, java.lang.String, java.lang.Object)

Parameters:
issue - the issue to report
location - the location of the issue, or null if not known
message - the message for this warning
data - any associated data, or null

log

public void log(@Nullable
                java.lang.Throwable exception,
                @Nullable
                java.lang.String format,
                @Nullable
                java.lang.Object... args)
Send an exception to the log. Convenience wrapper around LintClient.log(java.lang.Throwable, java.lang.String, java.lang.Object...).

Parameters:
exception - the exception, possibly null
format - the error message using String.format(java.lang.String, java.lang.Object...) syntax, possibly null
args - any arguments for the format string

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

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".

getFolderVersion

public int getFolderVersion()
Returns the folder version. For example, for the file values-v14/foo.xml, it returns 14.

Returns:
the folder version, or -1 if no specific version was specified

getFolderVersion

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

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

getSuppressCommentPrefix

@Nullable
protected java.lang.String getSuppressCommentPrefix()
Returns the comment marker used in Studio to suppress statements for language, if any


containsCommentSuppress

public boolean containsCommentSuppress()
Returns whether this file contains any suppress comment markers


isSuppressedWithComment

public boolean isSuppressedWithComment(int startOffset,
                                       @NonNull
                                       Issue issue)
Returns true if the given issue is suppressed at the given character offset in the file's contents