Skip to content

Instantly share code, notes, and snippets.

@hjhjw1991
Forked from crowjdh/MethodProxy.kt
Created June 15, 2020 08:36
Show Gist options
  • Save hjhjw1991/bd13f3278b9f069ef492d71293e8744a to your computer and use it in GitHub Desktop.
Save hjhjw1991/bd13f3278b9f069ef492d71293e8744a to your computer and use it in GitHub Desktop.
Method proxy for Kotlin(verification necessary)
inline fun <reified T : Record> T.proxy(): T {
return Proxy.newProxyInstance(javaClass.classLoader,
arrayOf(Record::class.java),
RecordInvocationHandler(this)) as T
}
class RecordInvocationHandler<T: Record>(private val caller: T) : InvocationHandler {
override fun invoke(proxy: Any, method: Method, args: Array<Any>): Any? {
var result: Any? = null
try {
// TODO: Pre-invoke jobs
result = method.invoke(caller, *args)
// TODO: Post-invoke jobs
} catch (ignored: Exception) {
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment