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

java.lang.Object
  extended by com.android.tools.lint.detector.api.Context
      extended by com.android.tools.lint.detector.api.JavaContext

public class JavaContext
extends Context

A Context used when checking Java 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.


Field Summary
 
Fields inherited from class com.android.tools.lint.detector.api.Context
file, mDriver
 
Constructor Summary
JavaContext(LintDriver driver, Project project, Project main, java.io.File file, JavaParser parser)
          Constructs a JavaContext for running lint on the given file, with the given scope, in the given project reporting errors to the given client.
 
Method Summary
 Location.Handle createLocationHandle(lombok.ast.Node node)
           
 JavaParser.ResolvedClass findClass(java.lang.String fullyQualifiedName)
           
static lombok.ast.Node findNameNode(lombok.ast.Node node)
          Searches for a name node corresponding to the given node
static lombok.ast.ClassDeclaration findSurroundingClass(lombok.ast.Node scope)
           
static lombok.ast.Node findSurroundingMethod(lombok.ast.Node scope)
           
static lombok.ast.Node getArgumentNode(lombok.ast.MethodInvocation call, int index)
          Returns the given argument of the given call
 lombok.ast.Node getCompilationUnit()
           
 Location getLocation(lombok.ast.Node node)
          Returns a location for the given node
static java.lang.String getMethodName(lombok.ast.Node call)
           
 Location getNameLocation(lombok.ast.Node node)
          Returns a Location for the given node.
static
<T extends lombok.ast.Node>
T
getNextSiblingOfType(lombok.ast.Node sibling, java.lang.Class<T> clz)
          Returns the first sibling of the given node that is of the given class
static lombok.ast.Node getParameter(lombok.ast.Node call, int parameter)
           
static java.util.Iterator<lombok.ast.Expression> getParameters(lombok.ast.Node call)
           
static
<T extends lombok.ast.Node>
T
getParentOfType(lombok.ast.Node element, java.lang.Class<T> clz)
          Returns the first ancestor node of the given type
static
<T extends lombok.ast.Node>
T
getParentOfType(lombok.ast.Node element, java.lang.Class<T> clz, boolean strict)
          Returns the first ancestor node of the given type
static
<T extends lombok.ast.Node>
T
getParentOfType(lombok.ast.Node element, java.lang.Class<T> clz, boolean strict, java.lang.Class<? extends lombok.ast.Node>... terminators)
          Returns the first ancestor node of the given type, stopping at the given type
 JavaParser getParser()
           
 Location getRangeLocation(lombok.ast.Node from, int fromDelta, lombok.ast.Node to, int toDelta)
          Returns a location for the given node range (from the starting offset of the first node to the ending offset of the second node).
protected  java.lang.String getSuppressCommentPrefix()
          Returns the comment marker used in Studio to suppress statements for language, if any
 JavaParser.TypeDescriptor getType(lombok.ast.Node node)
           
 boolean isContextMethod(lombok.ast.MethodInvocation node)
          Returns true if the given method invocation node corresponds to a call on a android.content.Context
 boolean isSuppressedWithComment(lombok.ast.Node scope, Issue issue)
           
 void report(Issue issue, Location location, java.lang.String message)
          Reports an issue.
 void report(Issue issue, lombok.ast.Node scope, Location location, java.lang.String message)
          Reports an issue applicable to a given AST node.
 void report(Issue issue, lombok.ast.Node scope, Location location, java.lang.String message, java.lang.Object data)
          Deprecated. Use report(Issue, Node, Location, String) instead; this method is here for custom rule compatibility
 JavaParser.ResolvedNode resolve(lombok.ast.Node node)
           
 void setCompilationUnit(lombok.ast.Node compilationUnit)
          Sets the compilation result.
 
Methods inherited from class com.android.tools.lint.detector.api.Context
containsCommentSuppress, getClient, getConfiguration, getContents, getDriver, getMainProject, getPhase, getProject, getProperty, getScope, getSdkInfo, isEnabled, isSuppressedWithComment, log, report, requestRepeat, setProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavaContext

public JavaContext(@NonNull
                   LintDriver driver,
                   @NonNull
                   Project project,
                   @Nullable
                   Project main,
                   @NonNull
                   java.io.File file,
                   @NonNull
                   JavaParser parser)
Constructs a JavaContext for running lint on the given file, with the given scope, in the given project reporting errors to the given client.

Parameters:
driver - the driver running through the checks
project - the project to run lint on which contains the given file
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 to be analyzed
parser - the parser to use
Method Detail

getLocation

@NonNull
public Location getLocation(@NonNull
                                    lombok.ast.Node node)
Returns a location for the given node

Parameters:
node - the AST node to get a location for
Returns:
a location for the given node

getRangeLocation

@NonNull
public Location getRangeLocation(@NonNull
                                         lombok.ast.Node from,
                                         int fromDelta,
                                         @NonNull
                                         lombok.ast.Node to,
                                         int toDelta)
Returns a location for the given node range (from the starting offset of the first node to the ending offset of the second node).

Parameters:
from - the AST node to get a starting location from
fromDelta - Offset delta to apply to the starting offset
to - the AST node to get a ending location from
toDelta - Offset delta to apply to the ending offset
Returns:
a location for the given node

getNameLocation

@NonNull
public Location getNameLocation(@NonNull
                                        lombok.ast.Node node)
Returns a Location for the given node. This attempts to pick a shorter location range than the entire node; for a class or method for example, it picks the name node (if found). For statement constructs such as a switch statement it will highlight the keyword, etc.

Parameters:
node - the AST node to create a location for
Returns:
a location for the given node

getParser

@NonNull
public JavaParser getParser()

getCompilationUnit

@Nullable
public lombok.ast.Node getCompilationUnit()

setCompilationUnit

public void setCompilationUnit(@Nullable
                               lombok.ast.Node compilationUnit)
Sets the compilation result. Not intended for client usage; the lint infrastructure will set this when a context has been processed

Parameters:
compilationUnit - the parse tree

report

public void report(@NonNull
                   Issue issue,
                   @Nullable
                   Location location,
                   @NonNull
                   java.lang.String message)
Description copied from class: Context
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, com.android.tools.lint.detector.api.TextFormat)

Overrides:
report in class Context
Parameters:
issue - the issue to report
location - the location of the issue, or null if not known
message - the message for this warning

report

public void report(@NonNull
                   Issue issue,
                   @Nullable
                   lombok.ast.Node scope,
                   @Nullable
                   Location location,
                   @NonNull
                   java.lang.String message)
Reports an issue applicable to a given AST node. The AST node is used as the scope to check for suppress lint annotations.

Parameters:
issue - the issue to report
scope - the AST node scope the error applies to. The lint infrastructure will check whether there are suppress annotations on this node (or its enclosing nodes) and if so suppress the warning without involving the client.
location - the location of the issue, or null if not known
message - the message for this warning

report

@Deprecated
public void report(@NonNull
                              Issue issue,
                              @Nullable
                              lombok.ast.Node scope,
                              @Nullable
                              Location location,
                              @NonNull
                              java.lang.String message,
                              @Nullable
                              java.lang.Object data)
Deprecated. Use report(Issue, Node, Location, String) instead; this method is here for custom rule compatibility

Report an error. Like report(Issue, Node, Location, String) but with a now-unused data parameter at the end.


findSurroundingMethod

@Nullable
public static lombok.ast.Node findSurroundingMethod(lombok.ast.Node scope)

findSurroundingClass

@Nullable
public static lombok.ast.ClassDeclaration findSurroundingClass(@Nullable
                                                                        lombok.ast.Node scope)

getSuppressCommentPrefix

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

Overrides:
getSuppressCommentPrefix in class Context

isSuppressedWithComment

public boolean isSuppressedWithComment(@NonNull
                                       lombok.ast.Node scope,
                                       @NonNull
                                       Issue issue)

createLocationHandle

@NonNull
public Location.Handle createLocationHandle(@NonNull
                                                    lombok.ast.Node node)

resolve

@Nullable
public JavaParser.ResolvedNode resolve(@NonNull
                                                lombok.ast.Node node)

findClass

@Nullable
public JavaParser.ResolvedClass findClass(@NonNull
                                                   java.lang.String fullyQualifiedName)

getType

@Nullable
public JavaParser.TypeDescriptor getType(@NonNull
                                                  lombok.ast.Node node)

getMethodName

@Nullable
public static java.lang.String getMethodName(@NonNull
                                                      lombok.ast.Node call)

findNameNode

@Nullable
public static lombok.ast.Node findNameNode(@NonNull
                                                    lombok.ast.Node node)
Searches for a name node corresponding to the given node

Returns:
the name node to use, if applicable

getParameters

@NonNull
public static java.util.Iterator<lombok.ast.Expression> getParameters(@NonNull
                                                                              lombok.ast.Node call)

getParameter

@Nullable
public static lombok.ast.Node getParameter(@NonNull
                                                    lombok.ast.Node call,
                                                    int parameter)

isContextMethod

public boolean isContextMethod(@NonNull
                               lombok.ast.MethodInvocation node)
Returns true if the given method invocation node corresponds to a call on a android.content.Context

Parameters:
node - the method call node
Returns:
true iff the method call is on a class extending context

getParentOfType

@Nullable
public static <T extends lombok.ast.Node> T getParentOfType(@Nullable
                                                                     lombok.ast.Node element,
                                                                     @NonNull
                                                                     java.lang.Class<T> clz)
Returns the first ancestor node of the given type

Type Parameters:
T - the target node type
Parameters:
element - the element to search from
clz - the target node type
Returns:
the nearest ancestor node in the parent chain, or null if not found

getParentOfType

@Nullable
public static <T extends lombok.ast.Node> T getParentOfType(@Nullable
                                                                     lombok.ast.Node element,
                                                                     @NonNull
                                                                     java.lang.Class<T> clz,
                                                                     boolean strict)
Returns the first ancestor node of the given type

Type Parameters:
T - the target node type
Parameters:
element - the element to search from
clz - the target node type
strict - if true, do not consider the element itself, only its parents
Returns:
the nearest ancestor node in the parent chain, or null if not found

getParentOfType

@Nullable
public static <T extends lombok.ast.Node> T getParentOfType(@Nullable
                                                                     lombok.ast.Node element,
                                                                     @NonNull
                                                                     java.lang.Class<T> clz,
                                                                     boolean strict,
                                                                     @NonNull
                                                                     java.lang.Class<? extends lombok.ast.Node>... terminators)
Returns the first ancestor node of the given type, stopping at the given type

Type Parameters:
T - the target node type
Parameters:
element - the element to search from
clz - the target node type
strict - if true, do not consider the element itself, only its parents
terminators - optional node types to terminate the search at
Returns:
the nearest ancestor node in the parent chain, or null if not found

getNextSiblingOfType

@Nullable
public static <T extends lombok.ast.Node> T getNextSiblingOfType(@Nullable
                                                                          lombok.ast.Node sibling,
                                                                          @NonNull
                                                                          java.lang.Class<T> clz)
Returns the first sibling of the given node that is of the given class

Type Parameters:
T - the type
Parameters:
sibling - the sibling to search from
clz - the type to look for
Returns:
the first sibling of the given type, or null

getArgumentNode

@NonNull
public static lombok.ast.Node getArgumentNode(@NonNull
                                                      lombok.ast.MethodInvocation call,
                                                      int index)
Returns the given argument of the given call

Parameters:
call - the call containing arguments
index - the index of the target argument
Returns:
the argument at the given index
Throws:
java.lang.IllegalArgumentException - if index is outside the valid range