Issue
Update
How can I get function name which is currently being execute using Kotlin?
I'm trying to get the function name of the function which is currently being execute as below but it's always coming as null
val funName = Object().`class`.enclosingMethod?.name;
Solution
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.
Answered By - Suryakant Sharma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.