mardi 8 août 2017

Simple way to find out if a class is anonymous/object and get the concrete class name from the instance in Kotlin

When implementing toString, etc. I sometimes print/log the concrete class name. Is there an easy/provided way to get this even for anonymous classes? It looks like there are less methods in the Kotlin reflection library than java. Currently, I'm doing it this way:

    instance::class.qualifiedName?: 
      instance::class::superclasses.get()[0].qualifiedName)

which is really ugly, and I don't know if it's even guaranteed to be correct every time unless the ordering of the list of superclasses is.

Is there a method for kotlin reflection like in java.lang.reflect where it's as easy as typing someClass.isAnonymous()?





Aucun commentaire:

Enregistrer un commentaire