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

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

@Beta
public class ClassContext
extends Context

A Context used when checking .class 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
ClassContext(LintDriver driver, Project project, Project main, java.io.File file, java.io.File jarFile, java.io.File binDir, byte[] bytes, org.objectweb.asm.tree.ClassNode classNode, boolean fromLibrary, java.lang.String sourceContents)
          Construct a new ClassContext
 
Method Summary
static java.lang.String createSignature(java.lang.String owner, java.lang.String name, java.lang.String desc)
          Computes a user-readable type signature from the given class owner, name and description.
static int findLineNumber(org.objectweb.asm.tree.AbstractInsnNode node)
          Finds the line number closest to the given node
static int findLineNumber(org.objectweb.asm.tree.ClassNode node)
          Finds the line number closest to the given class declaration
static int findLineNumber(org.objectweb.asm.tree.MethodNode node)
          Finds the line number closest to the given method declaration
 byte[] getBytecode()
          Returns the raw bytecode data for this class file
 org.objectweb.asm.tree.ClassNode getClassNode()
          Returns the bytecode object model
static java.lang.String getFqcn(java.lang.String owner)
          Converts from a VM owner name (such as foo/bar/Foo$Baz) to a fully qualified class name (such as foo.bar.Foo.Baz).
static java.lang.String getInternalName(java.lang.String fqcn)
          Computes the internal class name of the given fully qualified class name.
 java.io.File getJarFile()
          Returns the jar file, if any.
 Location getLocation(org.objectweb.asm.tree.AbstractInsnNode instruction)
          Returns a location for the given AbstractInsnNode.
 Location getLocation(org.objectweb.asm.tree.ClassNode classNode)
          Returns a location for the given ClassNode, where class node is either the top level class, or an inner class, in the current context.
 Location getLocation(org.objectweb.asm.tree.MethodNode methodNode, org.objectweb.asm.tree.ClassNode classNode)
          Returns a location for the given MethodNode.
 Location getLocationForLine(int line, java.lang.String patternStart, java.lang.String patternEnd, Location.SearchHints hints)
          Returns a location for the given source line number in this class file's source file, if available.
 java.lang.String getSourceContents()
          Returns the contents of the source file for this class file, if found.
 java.lang.String getSourceContents(boolean read)
          Returns the contents of the source file for this class file, if found.
 java.io.File getSourceFile()
          Returns the source file for this class file, if possible.
 boolean isFromClassLibrary()
          Returns whether this class is part of a library (not this project).
 void report(Issue issue, org.objectweb.asm.tree.FieldNode field, Location location, java.lang.String message)
          Reports an issue applicable to a given method node.
 void report(Issue issue, org.objectweb.asm.tree.FieldNode field, Location location, java.lang.String message, java.lang.Object data)
          Deprecated. Use report(Issue, FieldNode, Location, String) instead; this method is here for custom rule compatibility
 void report(Issue issue, Location location, java.lang.String message)
          Reports an issue.
 void report(Issue issue, org.objectweb.asm.tree.MethodNode method, org.objectweb.asm.tree.AbstractInsnNode instruction, Location location, java.lang.String message)
          Reports an issue applicable to a given method node.
 void report(Issue issue, org.objectweb.asm.tree.MethodNode method, org.objectweb.asm.tree.AbstractInsnNode instruction, Location location, java.lang.String message, java.lang.Object data)
          Deprecated. Use report(Issue, FieldNode, Location, String) instead; this method is here for custom rule compatibility
 
Methods inherited from class com.android.tools.lint.detector.api.Context
containsCommentSuppress, getClient, getConfiguration, getContents, getDriver, getMainProject, getPhase, getProject, getProperty, getScope, getSdkInfo, getSuppressCommentPrefix, 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

ClassContext

public ClassContext(@NonNull
                    LintDriver driver,
                    @NonNull
                    Project project,
                    @Nullable
                    Project main,
                    @NonNull
                    java.io.File file,
                    @Nullable
                    java.io.File jarFile,
                    @NonNull
                    java.io.File binDir,
                    @NonNull
                    byte[] bytes,
                    @NonNull
                    org.objectweb.asm.tree.ClassNode classNode,
                    boolean fromLibrary,
                    @Nullable
                    java.lang.String sourceContents)
Construct a new ClassContext

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
jarFile - If the file is a relative path within a jar file, this is the jar file, otherwise null
binDir - the root binary directory containing this .class file.
bytes - the bytecode raw data
classNode - the bytecode object model
fromLibrary - whether this class is from a library rather than part of this project
sourceContents - initial contents of the Java source, if known, or null
Method Detail

getBytecode

@NonNull
public byte[] getBytecode()
Returns the raw bytecode data for this class file

Returns:
the byte array containing the bytecode data

getClassNode

@NonNull
public org.objectweb.asm.tree.ClassNode getClassNode()
Returns the bytecode object model

Returns:
the bytecode object model, never null

getJarFile

@Nullable
public java.io.File getJarFile()
Returns the jar file, if any. If this is null, the .class file is a real file on disk, otherwise it represents a relative path within the jar file.

Returns:
the jar file, or null

isFromClassLibrary

public boolean isFromClassLibrary()
Returns whether this class is part of a library (not this project).

Returns:
true if this class is part of a library

getSourceFile

@Nullable
public java.io.File getSourceFile()
Returns the source file for this class file, if possible.

Returns:
the source file, or null

getSourceContents

@NonNull
public java.lang.String getSourceContents()
Returns the contents of the source file for this class file, if found.

Returns:
the source contents, or ""

getSourceContents

@Nullable
public java.lang.String getSourceContents(boolean read)
Returns the contents of the source file for this class file, if found. If read is false, do not read the source contents if it has not already been read. (This is primarily intended for the lint infrastructure; most client code would call getSourceContents() .)

Parameters:
read - whether to read the source contents if it has not already been initialized
Returns:
the source contents, which will never be null if read is true, or null if read is false and the source contents hasn't already been read.

getLocationForLine

@NonNull
public Location getLocationForLine(int line,
                                           @Nullable
                                           java.lang.String patternStart,
                                           @Nullable
                                           java.lang.String patternEnd,
                                           @Nullable
                                           Location.SearchHints hints)
Returns a location for the given source line number in this class file's source file, if available.

Parameters:
line - the line number (1-based, which is what ASM uses)
patternStart - optional pattern to search for in the source for range start
patternEnd - optional pattern to search for in the source for range end
hints - additional hints about the pattern search (provided patternStart is non null)
Returns:
a location, never null

report

public void report(@NonNull
                   Issue issue,
                   @Nullable
                   Location location,
                   @NonNull
                   java.lang.String message)
Reports an issue.

Detectors should only call this method if an error applies to the whole class scope and there is no specific method or field that applies to the error. If so, use report(Issue, org.objectweb.asm.tree.MethodNode, org.objectweb.asm.tree.AbstractInsnNode, Location, String) or report(Issue, org.objectweb.asm.tree.FieldNode, Location, String), such that suppress annotations are checked.

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
                   org.objectweb.asm.tree.MethodNode method,
                   @Nullable
                   org.objectweb.asm.tree.AbstractInsnNode instruction,
                   @Nullable
                   Location location,
                   @NonNull
                   java.lang.String message)
Reports an issue applicable to a given method node.

Parameters:
issue - the issue to report
method - the method scope the error applies to. The lint infrastructure will check whether there are suppress annotations on this method (or its enclosing class) and if so suppress the warning without involving the client.
instruction - the instruction within the method the error applies to. You cannot place annotations on individual method instructions (for example, annotations on local variables are allowed, but are not kept in the .class file). However, this instruction is needed to handle suppressing errors on field initializations; in that case, the errors may be reported in the <clinit> method, but the annotation is found not on that method but for the FieldNode's.
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
                   org.objectweb.asm.tree.FieldNode field,
                   @Nullable
                   Location location,
                   @NonNull
                   java.lang.String message)
Reports an issue applicable to a given method node.

Parameters:
issue - the issue to report
field - the scope the error applies to. The lint infrastructure will check whether there are suppress annotations on this field (or its enclosing class) 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
                              org.objectweb.asm.tree.MethodNode method,
                              @Nullable
                              org.objectweb.asm.tree.AbstractInsnNode instruction,
                              @Nullable
                              Location location,
                              @NonNull
                              java.lang.String message,
                              @Nullable
                              java.lang.Object data)
Deprecated. Use report(Issue, FieldNode, Location, String) instead; this method is here for custom rule compatibility

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


report

@Deprecated
public void report(@NonNull
                              Issue issue,
                              @Nullable
                              org.objectweb.asm.tree.FieldNode field,
                              @Nullable
                              Location location,
                              @NonNull
                              java.lang.String message,
                              @Nullable
                              java.lang.Object data)
Deprecated. Use report(Issue, FieldNode, Location, String) instead; this method is here for custom rule compatibility

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


findLineNumber

public static int findLineNumber(@NonNull
                                 org.objectweb.asm.tree.AbstractInsnNode node)
Finds the line number closest to the given node

Parameters:
node - the instruction node to get a line number for
Returns:
the closest line number, or -1 if not known

findLineNumber

public static int findLineNumber(@NonNull
                                 org.objectweb.asm.tree.MethodNode node)
Finds the line number closest to the given method declaration

Parameters:
node - the method node to get a line number for
Returns:
the closest line number, or -1 if not known

findLineNumber

public static int findLineNumber(@NonNull
                                 org.objectweb.asm.tree.ClassNode node)
Finds the line number closest to the given class declaration

Parameters:
node - the method node to get a line number for
Returns:
the closest line number, or -1 if not known

getLocation

@NonNull
public Location getLocation(@NonNull
                                    org.objectweb.asm.tree.ClassNode classNode)
Returns a location for the given ClassNode, where class node is either the top level class, or an inner class, in the current context.

Parameters:
classNode - the class in the current context
Returns:
a location pointing to the class declaration, or as close to it as possible

getLocation

@NonNull
public Location getLocation(@NonNull
                                    org.objectweb.asm.tree.MethodNode methodNode,
                                    @NonNull
                                    org.objectweb.asm.tree.ClassNode classNode)
Returns a location for the given MethodNode.

Parameters:
methodNode - the class in the current context
classNode - the class containing the method
Returns:
a location pointing to the class declaration, or as close to it as possible

getLocation

@NonNull
public Location getLocation(@NonNull
                                    org.objectweb.asm.tree.AbstractInsnNode instruction)
Returns a location for the given AbstractInsnNode.

Parameters:
instruction - the instruction to look up the location for
Returns:
a location pointing to the instruction, or as close to it as possible

getFqcn

@NonNull
public static java.lang.String getFqcn(@NonNull
                                               java.lang.String owner)
Converts from a VM owner name (such as foo/bar/Foo$Baz) to a fully qualified class name (such as foo.bar.Foo.Baz).

Parameters:
owner - the owner name to convert
Returns:
the corresponding fully qualified class name

createSignature

public static java.lang.String createSignature(java.lang.String owner,
                                               java.lang.String name,
                                               java.lang.String desc)
Computes a user-readable type signature from the given class owner, name and description. For example, for owner="foo/bar/Foo$Baz", name="foo", description="(I)V", it returns "void foo.bar.Foo.Bar#foo(int)".

Parameters:
owner - the class name
name - the method name
desc - the method description
Returns:
a user-readable string

getInternalName

@NonNull
public static java.lang.String getInternalName(@NonNull
                                                       java.lang.String fqcn)
Computes the internal class name of the given fully qualified class name. For example, it converts foo.bar.Foo.Bar into foo/bar/Foo$Bar

Parameters:
fqcn - the fully qualified class name
Returns:
the internal class name