Skip to content

Instantly share code, notes, and snippets.

@Jire
Created September 25, 2016 05:29
Show Gist options
  • Save Jire/df9c99fac5e4d3f35e1a6c517716f989 to your computer and use it in GitHub Desktop.
Save Jire/df9c99fac5e4d3f35e1a6c517716f989 to your computer and use it in GitHub Desktop.
inline fun <reified T : Any, DELEGATE : Any> findDelegate(
instance: T, delegatingTo: KClass<DELEGATE>): DELEGATE? {
for (prop in T::class.declaredMemberProperties) {
val javaField = prop.javaField ?: continue
javaField.isAccessible = true // is private, have to open that up
if (delegatingTo.java.isAssignableFrom(javaField.type)) {
@Suppress("UNCHECKED_CAST")
return javaField.get(instance) as DELEGATE
}
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment