com.android.tools.lint.checks
Class ApiLookup

java.lang.Object
  extended by com.android.tools.lint.checks.ApiLookup

public class ApiLookup
extends java.lang.Object

Database for API checking: Allows quick lookup of a given class, method or field to see which API level it was introduced in.

This class is optimized for quick bytecode lookup used in conjunction with the ASM library: It has lookup methods that take internal JVM signatures, and for a method call for example it processes the owner, name and description parameters separately the way they are provided from ASM.

The Api class provides access to the full Android API along with version information, initialized from an XML file. This lookup class adds a binary cache around the API to make initialization faster and to require fewer objects. It creates a binary cache data structure, which fits in a single byte array, which means that to open the database you can just read in the byte array and go. On one particular machine, this takes about 30-50 ms versus 600-800ms for the full parse. It also helps memory by placing everything in a compact byte array instead of needing separate strings (2 bytes per character in a char[] for the 25k method entries, 11k field entries and 6k class entries) - and it also avoids the same number of Map.Entry objects. When creating the memory data structure it performs a few other steps to help memory:


Method Summary
static ApiLookup get(com.android.tools.lint.client.api.LintClient client)
          Returns an instance of the API database
 int getCallDeprecatedIn(java.lang.String owner, java.lang.String name, java.lang.String desc)
          Returns the API version the given call was deprecated in, or -1 if the method is not deprecated.
 int getCallVersion(java.lang.String owner, java.lang.String name, java.lang.String desc)
          Returns the API version required by the given method call.
 int getClassDeprecatedIn(java.lang.String className)
          Returns the API version the given class was deprecated in, or -1 if the class is not deprecated.
 int getClassVersion(java.lang.String className)
          Returns the API version required by the given class reference, or -1 if this is not a known API class.
 int getFieldDeprecatedIn(java.lang.String owner, java.lang.String name)
          Returns the API version the given field was deprecated in, or -1 if the field is not deprecated.
 int getFieldVersion(java.lang.String owner, java.lang.String name)
          Returns the API version required to access the given field, or -1 if this is not a known API method.
 int getValidCastVersion(java.lang.String sourceClass, java.lang.String destinationClass)
          Returns the API version required to perform the given cast, or -1 if this is valid for all versions of the class (or, if these are not known classes or if the cast is not valid at all.)
 boolean isKnownClass(java.lang.String className)
          Returns true if the given owner class is known in the API database.
static boolean isRelevantOwner(java.lang.String owner)
          Returns true if the given owner (in VM format) is relevant to the database.
 boolean isValidJavaPackage(java.lang.String owner)
          Returns true if the given owner (in VM format) is a valid Java package supported in any version of Android.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

get

@Nullable
public static ApiLookup get(@NonNull
                                     com.android.tools.lint.client.api.LintClient client)
Returns an instance of the API database

Parameters:
client - the client to associate with this database - used only for logging. The database object may be shared among repeated invocations, and in that case client used will be the one originally passed in. In other words, this parameter may be ignored if the client created is not new.
Returns:
a (possibly shared) instance of the API database, or null if its data can't be found

getClassVersion

public int getClassVersion(@NonNull
                           java.lang.String className)
Returns the API version required by the given class reference, or -1 if this is not a known API class. Note that it may return -1 for classes introduced in version 1; internally the database only stores version data for version 2 and up.

Parameters:
className - the internal name of the class, e.g. its fully qualified name (as returned by Class.getName(), but with '.' replaced by '/'.
Returns:
the minimum API version the method is supported for, or -1 if it's unknown or version 1.

isKnownClass

public boolean isKnownClass(@NonNull
                            java.lang.String className)
Returns true if the given owner class is known in the API database.

Parameters:
className - the internal name of the class, e.g. its fully qualified name (as returned by Class.getName(), but with '.' replaced by '/' (and '$' for inner classes)
Returns:
true if this is a class found in the API database

getValidCastVersion

public int getValidCastVersion(@NonNull
                               java.lang.String sourceClass,
                               @NonNull
                               java.lang.String destinationClass)
Returns the API version required to perform the given cast, or -1 if this is valid for all versions of the class (or, if these are not known classes or if the cast is not valid at all.)

Note also that this method should only be called for interfaces that are actually implemented by this class or extending the given super class (check elsewhere); it doesn't distinguish between interfaces implemented in the initial version of the class and interfaces not implemented at all.

Parameters:
sourceClass - the internal name of the class, e.g. its fully qualified name (as returned by Class.getName(), but with '.' replaced by '/'.
destinationClass - the class to cast the sourceClass to
Returns:
the minimum API version the method is supported for, or 1 or -1 if it's unknown.

getClassDeprecatedIn

public int getClassDeprecatedIn(@NonNull
                                java.lang.String className)
Returns the API version the given class was deprecated in, or -1 if the class is not deprecated.

Parameters:
className - the internal name of the method's owner class, e.g. its fully qualified name (as returned by Class.getName(), but with '.' replaced by '/'.
Returns:
the API version the API was deprecated in, or -1 if it's unknown or version 0.

getCallVersion

public int getCallVersion(@NonNull
                          java.lang.String owner,
                          @NonNull
                          java.lang.String name,
                          @NonNull
                          java.lang.String desc)
Returns the API version required by the given method call. The method is referred to by its owner, name and desc fields. If the method is unknown it returns -1. Note that it may return -1 for classes introduced in version 1; internally the database only stores version data for version 2 and up.

Parameters:
owner - the internal name of the method's owner class, e.g. its fully qualified name (as returned by Class.getName(), but with '.' replaced by '/'.
name - the method's name
desc - the method's descriptor - see Type
Returns:
the minimum API version the method is supported for, or 1 or -1 if it's unknown.

getCallDeprecatedIn

public int getCallDeprecatedIn(@NonNull
                               java.lang.String owner,
                               @NonNull
                               java.lang.String name,
                               @NonNull
                               java.lang.String desc)
Returns the API version the given call was deprecated in, or -1 if the method is not deprecated.

Parameters:
owner - the internal name of the method's owner class, e.g. its fully qualified name (as returned by Class.getName(), but with '.' replaced by '/'.
name - the method's name
desc - the method's descriptor - see Type
Returns:
the API version the API was deprecated in, or 1 or -1 if it's unknown.

getFieldVersion

public int getFieldVersion(@NonNull
                           java.lang.String owner,
                           @NonNull
                           java.lang.String name)
Returns the API version required to access the given field, or -1 if this is not a known API method. Note that it may return -1 for classes introduced in version 1; internally the database only stores version data for version 2 and up.

Parameters:
owner - the internal name of the method's owner class, e.g. its fully qualified name (as returned by Class.getName(), but with '.' replaced by '/'.
name - the method's name
Returns:
the minimum API version the method is supported for, or 1 or -1 if it's unknown.

getFieldDeprecatedIn

public int getFieldDeprecatedIn(@NonNull
                                java.lang.String owner,
                                @NonNull
                                java.lang.String name)
Returns the API version the given field was deprecated in, or -1 if the field is not deprecated.

Parameters:
owner - the internal name of the method's owner class, e.g. its fully qualified name (as returned by Class.getName(), but with '.' replaced by '/'.
name - the method's name
Returns:
the API version the API was deprecated in, or 1 or -1 if it's unknown.

isRelevantOwner

public static boolean isRelevantOwner(@NonNull
                                      java.lang.String owner)
Returns true if the given owner (in VM format) is relevant to the database. This allows quick filtering out of owners that won't return any data for the various #getFieldVersion etc methods.

Parameters:
owner - the owner to look up
Returns:
true if the owner might be relevant to the API database

isValidJavaPackage

public boolean isValidJavaPackage(@NonNull
                                  java.lang.String owner)
Returns true if the given owner (in VM format) is a valid Java package supported in any version of Android.

Parameters:
owner - the package, in VM format
Returns:
true if the package is included in one or more versions of Android