5

I'm working on a library that uses reflection, and I would like to manipulate all the properties / function of a given KClass.

Using the KClass::members property, I can manipulate all the accessible members (as per the documentation), ie: the public, internal and protected fields, properties and functions. But I can't see the private ones.

Using Java reflection, I can see all fields (inluding private ones) using the Class.getDeclaredFields(), and likewise I can see private methods using Class.getDeclaredMethods().

Is there a way to do this using the kotlin reflection API ?

XGouchet
  • 10,002
  • 10
  • 48
  • 83

1 Answers1

3

Yes, you can use the declaredMembers, declaredFunctions, and declaredMemberProperties, which will include the private members, but won't include the members declared in the supertypes, just like with the Java reflection API.

hotkey
  • 140,743
  • 39
  • 371
  • 326
  • Thanks, I feel stupid. I only looked at the KClass documentation, but didn't think there would be an extension function to do that. – XGouchet Sep 25 '19 at 13:44