From bf1c906a88c2c6c473bdc60eda8d48a270ecf7df Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 25 Mar 2023 13:04:53 +0900 Subject: [PATCH] Reflection util --- src/net/torvald/reflection/Reflection.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/net/torvald/reflection/Reflection.kt diff --git a/src/net/torvald/reflection/Reflection.kt b/src/net/torvald/reflection/Reflection.kt new file mode 100644 index 000000000..919c8119b --- /dev/null +++ b/src/net/torvald/reflection/Reflection.kt @@ -0,0 +1,17 @@ +package net.torvald.reflection + +/** + * Created by minjaesong on 2023-03-25. + */ +fun Any.extortField(name: String): Any? { // yes I'm deliberately using negative words for the function name + return this.javaClass.getDeclaredField(name).let { + it.isAccessible = true + it.get(this) + } +} +fun Any.forceInvoke(name: String, params: Array): Any? { // yes I'm deliberately using negative words for the function name + return this.javaClass.getDeclaredMethod(name, *(params.map { it.javaClass }.toTypedArray())).let { + it.isAccessible = true + it.invoke(this, *params) + } +} \ No newline at end of file