Create custom Annotation using Kotlin — tutorial

Agenda:

  1. BkThread — the annotation declaration
  2. AnnotationUtility.kt — The helper class that checks for the thread and generates a warning.
  3. ClassA.kt — the class which have the annotated methods
  4. MainActivity .kt — calls the method in UI thread which generates a warning and then calls from a background thread shows no warning.

BkThread.kt

package com.example.testcustomannotation@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class BkThread{
}

AnnotationsUtility.kt

package com.example.testcustomannotationimport android.os.Looper
import android.util.Log
import java.lang.Exception
import java.lang.reflect.Method
class AnnotationUtility { companion object{fun checkThreadWarnings(method: Method?){
if (method != null) {
try {
val bkThreadAnnotation = BkThread::class.java
val annotation = method.getAnnotation(bkThreadAnnotation)

if (annotation != null && Looper.myLooper() == Looper.getMainLooper()) {
Log.d("WARNING", method.name+" should be called from a background thread.")
}
}catch (e: Exception){
Log.d("Exception", e.toString())
}
}
}
}
}

ClassA.kt

package com.example.testcustomannotationclass ClassA { // A class in your own framework.    @BkThread
fun operation1() {
AnnotationUtility.checkThreadWarnings(object{}.javaClass.enclosingMethod)
// do something
}
}

MainActivity.kt

package com.example.testcustomannotationimport androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import org.jetbrains.anko.doAsync
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val obj = ClassA()
obj.operation1() // Will generate Warning.
doAsync {
obj.operation1() // called righly, no warning
}
}
}
implementation 'org.jetbrains.anko:anko-common:0.9'

Output:

2020-04-03 20:44:21.194 6342-6342/com.example.testcustomannotation D/WARNING: operation1 should be called from a background thread.

--

--

--

works @Twin Health

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Expanding Jupyter Notebook Functionality with nbextensions

Why you must Switch from Java to Kotlin for Android Development?

Data Reconciliation

Mail from Slack by OpenFaaS

Migrating to EKS with zero downtime

CSS — Putting Stuff Side By Side

Ansible in Organisation

Rabin-Karp , The String Matching Algorithm.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ankur Srivastava

Ankur Srivastava

works @Twin Health

More from Medium

How to create a Kotlin back-end server (ktor + exposed + flyway + jwt)

Server/ Client Two Way Communication(WebSocket)

Server-Sent Event (SSE) Chat Application using Spring Boot and React Js

Kotlin Spring Data MongoRepository