Newbie: why are there no Java class property references?


#1

Hi all,
In Kotlin it is easy to make type safe frameworks that no longer need strings to refer to properties because we can use classname::propertyname to get a KProperty - which is a typeful reference to the actual thing. This solves one of my biggest complaints on Java :wink:

But this only works for kotlin classes, sadly enough. While “properties” on a Java class can be accessed with Kotlin property syntax the same does not go for property reference: it seems to be impossible to get a KProperty using the same syntax. This is bad news because it means lot of legacy code cannot be used with newer framework code that would try to use Kotlin property references.

Is there a reason for this “omission”, and can anyone think of a workaround?


#2

Here’s the relevant issue: https://youtrack.jetbrains.com/issue/KT-8575


#3

Thanks, I’ll vote for it and keep my fingers crossed :wink:


#4

This is what you are looking for: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-field/