Kotlin reflection - getting all class field names

How can I get a list of the Kotlin it data class declaredFields? How javagetDeclaredFields()

And if possible, is it also possible to filter publicand fields private? (Like Java Modifier.isPrivate(field.getModifiers()))

+4
source share
2 answers

You probably want to get class properties, not fields. This can be done as follows:

MyClass::class.declaredMemberProperties

Getting fields is also possible through Java reflection:

MyClass::class.java.declaredFields

But fields are more likely an implementation in Kotlin, because some properties cannot have fields.


As for visibility, for properties you can check getter visibility modifiers:

val p = MyClass::class.declaredMemberProperties.first()
val modifiers = p.javaGetter?.modifiers

: null private val @JvmField. p.javaField.

, modifiers null, Modifier.isPrivate(...).

Kotlin getter setter, , , .

+8

, : API, KClass.members. , , .

+2

Source: https://habr.com/ru/post/1655169/


All Articles