mardi 12 avril 2022

Using reflection on singleton object kotlin?

I have a use case where I need to use reflection to call method of my singleton class

Singleton class is as below

object Singleton {

fun calledFromReflection(){
    println("Successfull")
}
}

This is how I am using the reflection

val adsUtiltyCls: Class<*>? = Class.forName("com.xia.Singleton")
        val adsUtilityObj: Any? = adsUtiltyCls?.newInstance()
        if (adsUtilityObj != null) {
            val method: Method
            try {
                method = adsUtiltyCls.getDeclaredMethod("calledFromReflection")
                val value=method.invoke(adsUtilityObj)
                println("value   $value")
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

I get the following error

java.lang.IllegalAccessException: void com.com.xia.Singleton.<init>() is not accessible from java.lang.Class<com.xia.RetargetActivity>




Aucun commentaire:

Enregistrer un commentaire