Get name of current function in Kotlin

I found one of the way:-

val name = object : Any() {

}.javaClass.enclosingMethod.name

Above code can also be refine as –

val name = object{}.javaClass.enclosingMethod.name

Edit because incorrect duplicate flag prevents a new answer:

A more Java way is this:

Thread.currentThread().stackTrace[1].methodName

but it takes ~47ms on my system compared with ~13ms for the object() based one: nearly 4 times slower.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)