Is there any way to get only the declared members of a class (not inherited) with Kotlin Reflection?
Something equivalent to getDeclaredMethods(), or ...Fields(), in Java, but for members and JVM free, which:
Returns an array containing Method objects reflecting all the declared methods of the class ... but excluding inherited methods.
Or like a binding flag, such as BindingFlags.DeclaredOnly of dotnet.
Because the reflection is based on the class, So the following is only for the kotlin/JVM, not suitable for the Kotlin/JS or Kotlin/Native.
For the Kotlin/JS it supports limit, for detail, you can see this document
Firstly, you can use the
SomeClass::class.java.declaredMethodsto get the getDeclaredMethods. That is the java method. Because the Kotlin file after compiled it is still a class. so you can directly use it.You can also add the kotlin reflect to get the KClass, then use the
declaredFunctionsto get. Here is the DocumentFor how to get the KClass, you can use the following code
Besides the method, the other property you can also get. such as
you can read the document using it.