1

How can I get the reference name of a Kotlin object? Say I have an object, how can I get, say, com.myapp.CustomClass@6406? My question is the reverse of How can I get a reference to a Kotlin object by name?

Jack Guo
  • 3,959
  • 8
  • 39
  • 60

1 Answers1

2

Looks like you need combination of full object name and object reference identity.

So, code can be like this:

val myTestObject = ...
val referenceValue = Integer.toHexString(System.identityHashCode(myTestObject))
val className = myTestObject.javaClass.canonicalName
val result = "$className@$referenceValue"
Manushin Igor
  • 3,398
  • 1
  • 26
  • 40
  • The hash code is different from the number printed out by debugger of that object – Jack Guo Jul 13 '18 at 17:19
  • 2
    You need `Integer.toHexString(System.identityHashCode(myTestObject))`, see http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/tip/src/share/classes/java/lang/Object.java. – Alexey Romanov Jul 13 '18 at 19:17