0

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

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.

Karthikeyan
  • 385
  • 3
  • 7
  • 18
  • Possible duplicate of [Is there a way to instantiate a class by name in Java?](https://stackoverflow.com/questions/9886266/is-there-a-way-to-instantiate-a-class-by-name-in-java). – Tim Biegeleisen Sep 17 '18 at 06: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 06: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 06:39
  • 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 06:45
  • what are you trying to accomplish exactly? – Roland Sep 17 '18 at 10:49
  • @Roland 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. – Karthikeyan Sep 17 '18 at 11:47
  • Any one guide me plz – Karthikeyan Sep 17 '18 at 12:35
  • I don't understand what you're requirements are. It would be helpful if you could share information about what you're trying to achieve here. – Birju Vachhani Feb 19 '19 at 21:30

2 Answers2

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

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

  • There is an interface or abstract class com.crypto.wallet.data.AppDatabase,
  • There is a non-static non-abstract class com.crypto.wallet.data.AppDatabase_Impl which implements com.crypto.wallet.data.AppDatabase,
  • There is an instance of com.crypto.wallet.data.AppDatabase_Impl that called something, with a "hashcode" 14731a42 (this hashcode is meaningless for most time).
  • Neither this interface nor this implementation has @Override the toString() call.
  • When called toString() on this "something" object, it happens to be "com.crypto.wallet.data.AppDatabase_Impl@14731a42"

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

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

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

Geno Chen
  • 4,916
  • 6
  • 21
  • 39
-1

You can use this:

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

in your case

val classType = Class.forName("com.crypto.wallet.data.AppDatabase_Impl")
Ibrahim
  • 152
  • 2
  • 9
  • 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 06:42