How to convert a string to class type in Kotlin?

How to convert a string to class type in Kotlin?



I want to convert this string into AppDatabase type, I tried this Class.forName() but this is not working properly.


AppDatabase


Class.forName()


val str = "com.crypto.wallet.data.AppDatabase_Impl@14731a42"



my class path is "com.crypto.wallet.data.AppDatabase"



I am new to Kotlin environment, please guide me.



Foot Note:



I need to pass the values to Main.kt to activityTwo.kt so here i use ShardPref for handling the data. Above the values are not String type so i need to convert this to string values and pass to ActivityTwo.kt class. Important: ActivityTwo.kt file have a one method, it will support args types are ClassA,ClassB, and ClassC e.g for MyFunctionSample(ClassA,ClassB,ClassC) but i have a values in String Type so i need to convert the type to String to ClassA type. Here is i struck.






Possible duplicate of Is there a way to instantiate a class by name in Java?.

– Tim Biegeleisen
Sep 17 '18 at 6:26







@Tim Same error nothing works, i got thus error " Caused by: java.lang.ClassNotFoundException: Invalid name: com.crypto.wallet.data.AppDatabase_Impl@1056420" i tried ths way " val kClass = Class.forName(appDatabase1) val object1 = kClass.newInstance() as AppDatabase"

– Karthikeyan
Sep 17 '18 at 6:34






You would need to use just the fully qualified class name, that is com.crypto.wallet.data.AppDatabase_Impl.

– Tim Biegeleisen
Sep 17 '18 at 6:39


com.crypto.wallet.data.AppDatabase_Impl






my fully qualtfied class path name is "com.crypto.wallet.data.AppDatabase" here where i can pass my string value for converting my string value to class type??

– Karthikeyan
Sep 17 '18 at 6:45






what are you trying to accomplish exactly?

– Roland
Sep 17 '18 at 10:49




2 Answers
2



You can use this:


val classType = Class.forName("packageName.class")



in your case


val classType = Class.forName("com.crypto.wallet.data.AppDatabase_Impl")






my class name is "com.crypto.wallet.data.AppDatabase'i tried this but not wkkroing i got error found Class<*> any but required Appdatabase

– Karthikeyan
Sep 17 '18 at 6:42



val str = "com.crypto.wallet.data.AppDatabase_Impl@14731a42"



For my common understanding to Java (yes, Java, not Kotlin), this implies:


com.crypto.wallet.data.AppDatabase


com.crypto.wallet.data.AppDatabase_Impl


implements


com.crypto.wallet.data.AppDatabase


com.crypto.wallet.data.AppDatabase_Impl


14731a42


@Override


toString()


toString()


"com.crypto.wallet.data.AppDatabase_Impl@14731a42"



This results to the val str mentioned in the original question.


val str



There maybe some important content inside this instance, however it is missing if you call toString() directly.


toString()



To achieve your target, you should better implement a serializer and deserializer for your "AppDatabase" data structure, an easy way is to use JSON.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)