From 9ef94efaea82bcd534499c3ca9389f475cfd26f3 Mon Sep 17 00:00:00 2001
From: Rich Slogar In this document
@@ -70,6 +75,10 @@ values in your code, for example:
R.
resource. @UiThread
For a complete list of the supported annotations, either examine the contents of the @@ -85,11 +94,14 @@ development tools.
To add annotations to your code, first add a dependency to the
{@link android.support.annotation Support-Annotations} library. In Android Studio,
-add the dependency to your build.gradle
file.
build.gradle
file. The following example shows how to add the
+{@link android.support.annotation Support-Annotations} library dependency in the
+build.gradle
file:
dependencies { - compile 'com.android.support:support-annotations:22.0.0' + compile 'com.android.support:support-annotations:22.2.0' }@@ -143,18 +155,20 @@ inferring nullability in Android Studio, see
Add {@link android.support.annotation.StringRes @StringRes} annotations to check that -a resource parameter contains a +
Validating resource types can be useful as Android references to resources, such as
+Drawables
and
+R.string
resources, are
+passed as integers. Code that expects a parameter to reference a specific type of resource, for
+example Drawables
,
+can be passed the expected reference type of int
, but actually reference a different
+type of resource, such as a R.string
resource.
For example, add {@link android.support.annotation.StringRes @StringRes} annotations to check
+that a resource parameter contains a
R.string
reference. During code inspection, the annotation generates a warning if a R.string
reference is not passed in the parameter.
Validating resource types can be useful as Android references to
-Drawables
and
-R.string
resources are both
-passed as integers. Code that expects a parameter to reference a Drawable
can be passed
-the expected reference type of int, but actually reference a R.string
resource.
This example attaches the {@link android.support.annotation.StringRes @StringRes}
annotation to the resId
parameter to validate that it is really a string resource.
Annotations for the other resource types, such as {@link android.support.annotation.DrawableRes @DrawableRes}, +{@link android.support.annotation.DimenRes @DimenRes}, {@link android.support.annotation.ColorRes @ColorRes}, and {@link android.support.annotation.InterpolatorRes @InterpolatorRes} can be added using the same annotation format and run during the code inspection.
+ + +Thread annotations check if a method is called from a specific type of +thread. The following thread +annotations are supported:
+@UiThread
@MainThread
@WorkerThread
@BinderThread
+Note: The @MainThread
+and the @UiThread
annotations are interchangeable so
+methods calls from either thread type are allowed for these annotations.
If all methods in a class share the same threading requirement, you can add a single +thread +annotation to the class to verify that all methods in the class are called from the same type of +thread.
+ +A common use of the thread +annotation is to validate method overrides in the +AsyncTask class as this class performs +background operations and publishes results only on the UI +thread.
+ + + +Use the @IntRange
,
+@FloatRange
, and
+@Size
annotations to validate the values of passed
+parameters.
The @IntRange
annotation validates that the parameter
+value is within a specified range. The following example ensures that the alpha
+parameter contains an integer value from 0 to 255:
+public void setAlpha(@IntRange(from=0,to=255) int alpha) { … } ++ +
The @FloatRange
annotation checks that the parameter
+value is within a specified range of floating point values. The following example ensures that the
+alpha
parameter contains a float value from 0.0 to 1.0:
+public void setAlpha(@FloatRange(from=0.0, to=1.0) float alpha) {...} ++ +
The @Size
annotation checks the size of a collection or
+array, as well as the length of a string. For example, use the @Size(min=1)
+annotation to check if a collection is not empty, and the @Size(2)
annotation to
+validate that an array contains exactly two values. The following example ensures that the
+location
array contains at least one element:
+int[] location = new int[3]; +button.getLocationOnScreen(@Size(min=1) location); ++ + +
Use the @RequiresPermission
annotation to
+validate the permissions of the caller of a method. To check for a single permission from a
+list the valid permissions, use the anyOf
attribute. To check for a set of
+permissions, use the allOf
attribute. The following example annotates the
+setWallpaper
method to ensure that the caller of the method has the
+permission.SET_WALLPAPERS
permission.
+@RequiresPermission(Manifest.permission.SET_WALLPAPER) +public abstract void setWallpaper(Bitmap bitmap) throws IOException; ++ +
This example requires the caller of the
+updateVisitedHistory
method to have both read and write bookmark history permissions.
+@RequiresPermission(allOf = { + Manifest.permission.READ_HISTORY_BOOKMARKS, + Manifest.permission.WRITE_HISTORY_BOOKMARKS}) +public static final void updateVisitedHistory(ContentResolver cr, String url, boolean real) { + ... +} ++ + +
Use the @CheckResults
annotation to
+validate that a method's result or return value is actually used. The following example annotates
+the checkPermissions
method to ensure the return value of the method is actually
+referenced. It also names the
+enforcePermission
+method as a method to be suggested to the developer as a replacement.
+@CheckResult(suggest="#enforcePermission(String,int,int,String)") +public abstract int checkPermission(@NonNull String permission, int pid, int uid); ++ +{@link android.support.annotation.StringDef @StringDef} + + +
Use the @CallSuper
annotation to validate that an
+overriding method calls the super implementation of the method. The following example annotates
+the onCreate
method to ensure that any overriding method implementations call
+super.onCreate()
.
+@CallSuper +protected void onCreate(Bundle savedInstanceState) { +} ++ + +
Use the {@link android.support.annotation.IntDef @IntDef} and {@link android.support.annotation.StringDef @StringDef} annotations -- cgit v1.1