com.android.tools.lint.checks
Class ApiDetector

java.lang.Object
  extended by com.android.tools.lint.detector.api.Detector
      extended by com.android.tools.lint.detector.api.ResourceXmlDetector
          extended by com.android.tools.lint.checks.ApiDetector
All Implemented Interfaces:
com.android.tools.lint.detector.api.Detector.ClassScanner, com.android.tools.lint.detector.api.Detector.JavaScanner, com.android.tools.lint.detector.api.Detector.XmlScanner

public class ApiDetector
extends com.android.tools.lint.detector.api.ResourceXmlDetector
implements com.android.tools.lint.detector.api.Detector.ClassScanner, com.android.tools.lint.detector.api.Detector.JavaScanner

Looks for usages of APIs that are not supported in all the versions targeted by this application (according to its minimum API requirement in the manifest).


Nested Class Summary
 
Nested classes/interfaces inherited from class com.android.tools.lint.detector.api.Detector
com.android.tools.lint.detector.api.Detector.BinaryResourceScanner, com.android.tools.lint.detector.api.Detector.ClassScanner, com.android.tools.lint.detector.api.Detector.GradleScanner, com.android.tools.lint.detector.api.Detector.JavaScanner, com.android.tools.lint.detector.api.Detector.OtherFileScanner, com.android.tools.lint.detector.api.Detector.ResourceFolderScanner, com.android.tools.lint.detector.api.Detector.XmlScanner
 
Field Summary
static java.lang.String ENUM_SWITCH_PREFIX
           
static com.android.tools.lint.detector.api.Issue INLINED
          Accessing an inlined API on older platforms
protected  ApiLookup mApiDatabase
           
static com.android.tools.lint.detector.api.Issue OVERRIDE
          Accessing an unsupported API
static com.android.tools.lint.detector.api.Issue UNSUPPORTED
          Accessing an unsupported API
static com.android.tools.lint.detector.api.Issue UNUSED
          Accessing an inlined API on older platforms
 
Fields inherited from interface com.android.tools.lint.detector.api.Detector.XmlScanner
ALL
 
Constructor Summary
ApiDetector()
          Constructs a new API check
 
Method Summary
 void afterCheckProject(com.android.tools.lint.detector.api.Context context)
           
 boolean appliesTo(com.android.resources.ResourceFolderType folderType)
           
 void beforeCheckProject(com.android.tools.lint.detector.api.Context context)
           
 void checkClass(com.android.tools.lint.detector.api.ClassContext context, org.objectweb.asm.tree.ClassNode classNode)
           
 lombok.ast.AstVisitor createJavaVisitor(com.android.tools.lint.detector.api.JavaContext context)
           
 java.util.Collection<java.lang.String> getApplicableAttributes()
           
 java.util.Collection<java.lang.String> getApplicableElements()
           
 java.util.List<java.lang.Class<? extends lombok.ast.Node>> getApplicableNodeTypes()
           
protected  int getMinSdk(com.android.tools.lint.detector.api.Context context)
           
static int getRequiredVersion(com.android.tools.lint.detector.api.Issue issue, java.lang.String errorMessage, com.android.tools.lint.detector.api.TextFormat format)
           
 com.android.tools.lint.detector.api.Speed getSpeed()
           
static int getTargetApi(lombok.ast.Modifiers modifiers)
          Returns the API level for the given AST node if specified with an @TargetApi annotation.
static boolean isBenignConstantUsage(lombok.ast.Node node, java.lang.String name, java.lang.String owner)
          Checks whether the given instruction is a benign usage of a constant defined in a later version of Android than the application's minSdkVersion.
static boolean isBenignUnusedAttribute(java.lang.String name)
          Is the given attribute a "benign" unused attribute, one we probably don't need to flag to the user as not applicable on all versions? These are typically attributes which add some nice platform behavior when available, but that are not critical and developers would not typically need to be aware of to try to implement workarounds on older platforms.
 void visitAttribute(com.android.tools.lint.detector.api.XmlContext context, org.w3c.dom.Attr attribute)
           
 void visitElement(com.android.tools.lint.detector.api.XmlContext context, org.w3c.dom.Element element)
           
 
Methods inherited from class com.android.tools.lint.detector.api.ResourceXmlDetector
appliesTo, run
 
Methods inherited from class com.android.tools.lint.detector.api.Detector
afterCheckFile, afterCheckLibraryProject, applicableSuperClasses, appliesToFolder, appliesToResourceRefs, beforeCheckFile, beforeCheckLibraryProject, checkBinaryResource, checkCall, checkClass, checkFolder, checkInstruction, getApplicableAsmNodeTypes, getApplicableCallNames, getApplicableCallOwners, getApplicableConstructorTypes, getApplicableFiles, getApplicableMethodNames, getSpeed, visitBuildScript, visitConstructor, visitDocument, visitElementAfter, visitMethod, visitResourceReference
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.android.tools.lint.detector.api.Detector.ClassScanner
checkCall, checkInstruction, getApplicableAsmNodeTypes, getApplicableCallNames, getApplicableCallOwners
 
Methods inherited from interface com.android.tools.lint.detector.api.Detector.JavaScanner
applicableSuperClasses, appliesToResourceRefs, checkClass, getApplicableConstructorTypes, getApplicableMethodNames, visitConstructor, visitMethod, visitResourceReference
 
Methods inherited from interface com.android.tools.lint.detector.api.Detector.XmlScanner
visitDocument, visitElementAfter
 

Field Detail

UNSUPPORTED

public static final com.android.tools.lint.detector.api.Issue UNSUPPORTED
Accessing an unsupported API


INLINED

public static final com.android.tools.lint.detector.api.Issue INLINED
Accessing an inlined API on older platforms


OVERRIDE

public static final com.android.tools.lint.detector.api.Issue OVERRIDE
Accessing an unsupported API


UNUSED

public static final com.android.tools.lint.detector.api.Issue UNUSED
Accessing an inlined API on older platforms


ENUM_SWITCH_PREFIX

public static final java.lang.String ENUM_SWITCH_PREFIX
See Also:
Constant Field Values

mApiDatabase

protected ApiLookup mApiDatabase
Constructor Detail

ApiDetector

public ApiDetector()
Constructs a new API check

Method Detail

getSpeed

@NonNull
public com.android.tools.lint.detector.api.Speed getSpeed()
Overrides:
getSpeed in class com.android.tools.lint.detector.api.Detector

beforeCheckProject

public void beforeCheckProject(@NonNull
                               com.android.tools.lint.detector.api.Context context)
Overrides:
beforeCheckProject in class com.android.tools.lint.detector.api.Detector

appliesTo

public boolean appliesTo(@NonNull
                         com.android.resources.ResourceFolderType folderType)
Overrides:
appliesTo in class com.android.tools.lint.detector.api.ResourceXmlDetector

getApplicableElements

public java.util.Collection<java.lang.String> getApplicableElements()
Specified by:
getApplicableElements in interface com.android.tools.lint.detector.api.Detector.XmlScanner
Overrides:
getApplicableElements in class com.android.tools.lint.detector.api.Detector

getApplicableAttributes

public java.util.Collection<java.lang.String> getApplicableAttributes()
Specified by:
getApplicableAttributes in interface com.android.tools.lint.detector.api.Detector.XmlScanner
Overrides:
getApplicableAttributes in class com.android.tools.lint.detector.api.Detector

visitAttribute

public void visitAttribute(@NonNull
                           com.android.tools.lint.detector.api.XmlContext context,
                           @NonNull
                           org.w3c.dom.Attr attribute)
Specified by:
visitAttribute in interface com.android.tools.lint.detector.api.Detector.XmlScanner
Overrides:
visitAttribute in class com.android.tools.lint.detector.api.Detector

isBenignUnusedAttribute

public static boolean isBenignUnusedAttribute(@NonNull
                                              java.lang.String name)
Is the given attribute a "benign" unused attribute, one we probably don't need to flag to the user as not applicable on all versions? These are typically attributes which add some nice platform behavior when available, but that are not critical and developers would not typically need to be aware of to try to implement workarounds on older platforms.


visitElement

public void visitElement(@NonNull
                         com.android.tools.lint.detector.api.XmlContext context,
                         @NonNull
                         org.w3c.dom.Element element)
Specified by:
visitElement in interface com.android.tools.lint.detector.api.Detector.XmlScanner
Overrides:
visitElement in class com.android.tools.lint.detector.api.Detector

getMinSdk

protected int getMinSdk(com.android.tools.lint.detector.api.Context context)

checkClass

public void checkClass(@NonNull
                       com.android.tools.lint.detector.api.ClassContext context,
                       @NonNull
                       org.objectweb.asm.tree.ClassNode classNode)
Specified by:
checkClass in interface com.android.tools.lint.detector.api.Detector.ClassScanner
Overrides:
checkClass in class com.android.tools.lint.detector.api.Detector

afterCheckProject

public void afterCheckProject(@NonNull
                              com.android.tools.lint.detector.api.Context context)
Overrides:
afterCheckProject in class com.android.tools.lint.detector.api.Detector

createJavaVisitor

@Nullable
public lombok.ast.AstVisitor createJavaVisitor(@NonNull
                                                        com.android.tools.lint.detector.api.JavaContext context)
Specified by:
createJavaVisitor in interface com.android.tools.lint.detector.api.Detector.JavaScanner
Overrides:
createJavaVisitor in class com.android.tools.lint.detector.api.Detector

getApplicableNodeTypes

@Nullable
public java.util.List<java.lang.Class<? extends lombok.ast.Node>> getApplicableNodeTypes()
Specified by:
getApplicableNodeTypes in interface com.android.tools.lint.detector.api.Detector.JavaScanner
Overrides:
getApplicableNodeTypes in class com.android.tools.lint.detector.api.Detector

isBenignConstantUsage

public static boolean isBenignConstantUsage(@Nullable
                                            lombok.ast.Node node,
                                            @NonNull
                                            java.lang.String name,
                                            @NonNull
                                            java.lang.String owner)
Checks whether the given instruction is a benign usage of a constant defined in a later version of Android than the application's minSdkVersion.

Parameters:
node - the instruction to check
name - the name of the constant
owner - the field owner
Returns:
true if the given usage is safe on older versions than the introduction level of the constant

getTargetApi

public static int getTargetApi(@Nullable
                               lombok.ast.Modifiers modifiers)
Returns the API level for the given AST node if specified with an @TargetApi annotation.

Parameters:
modifiers - the modifier to check
Returns:
the target API level, or -1 if not specified

getRequiredVersion

public static int getRequiredVersion(@NonNull
                                     com.android.tools.lint.detector.api.Issue issue,
                                     @NonNull
                                     java.lang.String errorMessage,
                                     @NonNull
                                     com.android.tools.lint.detector.api.TextFormat format)