is there a way to get the class name without appen...
# getting-started
a
is there a way to get the class name without appending
(Kotlin reflection is not available)
?
Copy code
fun main() {
    class Foo
    
    blah(Foo())
}

fun blah(value: Any) {
    val cls = value::class
    println("cls: $cls")
    // cls: class FileKt$main$Foo (Kotlin reflection is not available)
}
Playground demo
e
value::class.qualifiedName
k
Also, to make Kotlin reflection available, add the dependency
implementation("org.jetbrains.kotlin:kotlin-reflect:1.8.21")
a
no joy with
qualifiedName
I wanted to avoid adding Kotlin reflect because the full name is only needed for an error message
ah okay, got it - use
cls.java.name
and add
class
to the string
Copy code
println("cls: class ${cls.java.name}")
r
Also, instead of
value::class.java
, you can do
value.javaClass
e
oh right.
simpleName
might work there but that is a local class so most names don't work
plus1 1
javaClass
was supposed to be deprecated, not sure what happened with that… https://blog.jetbrains.com/kotlin/2017/02/kotlin-1-1-release-candidate-is-here/
y
Looks like this issue was erroneously renamed for a period of time to "Deprecate and delete javaClass property", but at the same exact time it was linked as "Relates to" to this issue "Deprecate and delete javaClass() function" which specifically is deleting the now-deleted function, not property. Seems like perhaps whoever was compiling the blog-post took the issue name at face value (which is absolutely fair enough). This PR confirms that ultimately the javaClass() function got deprecated
1
e
that was all pre-1.0 though, what happened to the statement from the 1.1 release
y
You are correct! Further digging reveals that on Feb 10th, 2017, this PR deprecated the
.javaClass
extension property, and the 1.1 RC article came out on Feb 17th, merely a week after. Then, on Feb 22nd, this PR undid the deprecation, citing:
its replacement is often inconvenient.
1.1.0 was then released on March 1st, 2017. I can't seem to find an issue related to these deprecations though, or a concrete discussion that caused the deprecation to not occur.
🕵️ 1
This Slack message confirms that the deprecation was taken back, although the linked issue is partially related: https://kotlinlang.slack.com/archives/C0AVAB92L/p1487777508000300
e
I see, I never saw the undeprecation, but I have noticed for a long time that the property wasn't marked deprecated. now I know why