Improve Your Android Coding Through Annotations

轉(zhuǎn)載自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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末奏黑,一起剝皮案震驚了整個濱河市愕把,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌永高,老刑警劉巖恋拍,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件垛孔,死亡現(xiàn)場離奇詭異,居然都是意外死亡施敢,警方通過查閱死者的電腦和手機周荐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來僵娃,“玉大人概作,你說我怎么就攤上這事∶跣恚” “怎么了仆嗦?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長先壕。 經(jīng)常有香客問我瘩扼,道長谆甜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任集绰,我火速辦了婚禮规辱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘栽燕。我一直安慰自己罕袋,他們只是感情好,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布碍岔。 她就那樣靜靜地躺著浴讯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蔼啦。 梳的紋絲不亂的頭發(fā)上榆纽,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天,我揣著相機與錄音捏肢,去河邊找鬼奈籽。 笑死,一個胖子當著我的面吹牛鸵赫,可吹牛的內(nèi)容都是我干的衣屏。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼辩棒,長吁一口氣:“原來是場噩夢啊……” “哼狼忱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起盗温,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤藕赞,失蹤者是張志新(化名)和其女友劉穎成肘,沒想到半個月后卖局,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡双霍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年砚偶,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片洒闸。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡染坯,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出丘逸,到底是詐尸還是另有隱情单鹿,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布深纲,位于F島的核電站仲锄,受9級特大地震影響劲妙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜儒喊,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一镣奋、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧怀愧,春花似錦侨颈、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至扛拨,卻和暖如春温赔,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背鬼癣。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工陶贼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人待秃。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓拜秧,卻偏偏與公主長得像,于是被迫代替她去往敵國和親章郁。 傳聞我的和親對象是個殘疾皇子枉氮,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容