Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting simpleName() after getClass() in Kotlin

In java, I am able to use getClass() and then retrieve simpleName from that class object without any issues.

String tag = someObject.getClass().getSimpleName(); // java code

But when converting to Kotlin, this causes warnings

Call uses reflection API which is not found in compilation classpath. Make sure you have kotlin-reflect.jar in the classpath

The kotlin code is

someObject::class.simpleName!! // kotlin code

What is the proper way of avoiding

kotlin.jvm.KotlinReflectionNotSupportedError ? Needs additional dependency to kotlin-reflect.jar. Maybe would be better use ::class.java.simpleName

like image 672
portfoliobuilder Avatar asked Jun 20 '19 17:06

portfoliobuilder


People also ask

What is Getclass () getSimpleName () in Java?

The getSimpleName() method of java. lang. Class class is used to get the simple name of this class, as given in the sourcecode. The method returns the simple name of this class in the form of String. If this class is anonymous, then this method returns empty string.

How do I get an instance of a class in Kotlin?

here getInstance() method is called whenever we request for the Instance where we can add code if we want to do something when the instance is called from any where everytime.

How do I compare classes in Kotlin?

Structural Equality ('==')== operator in Kotlin only compares the data or variables, whereas in Java or other languages == is generally used to compare the references. The negated counterpart of == in Kotlin is != which is used to compare if both the values are not equal to each other.


1 Answers

Use someObject::class.java.simpleName.

like image 133
CommonsWare Avatar answered Sep 22 '22 19:09

CommonsWare