转载自https://blog.mindorks.com/improve-your-android-coding-through-annotations-26b3273c137a
Annotations are Metadata.
And Metadata is a set of data that gives information about other data.
There are many ways in which annotations can be used. But, here we will talk about how the annotations can be used to improve our android coding.
Officially Android already provides the support annotations which you can include through the dependency as below:
compile ‘com.android.support:support-annotations:x.x.x’
Everybody wants to be a good programmer. I am also trying to improve my coding day by day.
Anyone can write code that a computer can understand. Good programmers write code that humans can understand — Martin Fowler
Let’s explore the few useful annotations:
Nullness annotations
@Nullable and @NonNull
annotations are used to check the nullness of a given variable, parameter, or even the return value.
@Nullable
: It indicates a variable, parameter, or return value that can a null.
@NonNUll
: It indicates a variable, parameter, or return value that cannot be null.
Example:
@NonNull
public View getView(@Nullable String s1, @NonNull String s2) {
// s1 can be null
// s2 should not be null
// it must return non null view}```
So, when you call this method like below:
```View view = getView("Amit", null);```
During the code inspection, the Android Studio will show you the warning that s2 should not be a null value.
######Resource annotations
As we know that Android references to resources, such as drawable and string resources, are passed as integers so we must validate the resource types. 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 an R.string resource.
public void setText(@StringRes int resId) {
// resId must be string resources
// resId should not be a normal int
}```
So, when you call this method like below:
textView.setText(56);
During the code inspection, the annotation generates a warning if a R.string
reference is not passed in the parameter.
Thread annotations
Thread annotations check if a method is called from a specific type of thread from which it is intended to be called.
Supported annotations are
@MainThread
@UiThread
@WorkerThread
@BinderThread
@AnyThread
If you put something like below:
@WorkerThread
public void doSomething(){
// this method must be called from the worker thread
}
So if you call this method from the thread other than the worker thread, it will show you the warning.
Value constraint annotations
Sometimes, we have to put some constraints on the parameters, so use the @IntRange, @FloatRange, and @Size annotations to validate the values of passed parameters.These are useful when the caller of the method are likely to pass the wrong value(out of the specified range).
Here, in the below example, the @IntRange annotation ensures that an integer value which will be passed must be in a range of 0 to 255.
public void setAlpha(@IntRange(from=0,to=255) int alpha) {}
Permission annotations
Use the @RequiresPermission annotation to validate the permissions of the caller of a method.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;
If you call this method without having required permission in the manifest, it will show you the warning.
Recommended further reading:
**How to create your own custom annotations?, read **here.
Thanks for reading this article. Be sure to click ❤ below to recommend this article if you found it helpful. It means a lot to me.
For more about programming, follow me and Mindorks , so you’ll get notified when we write new posts.
Join The Mindorks Community And Learn From Each Other.