Reputation: 2970
I know the way to make a Field
non-final via Reflection. But is there a way to make a method non-final? Somehow the same approach does not work.
// make all methods non-final
Clazz.javaClass.declaredMethods.forEach { method ->
method.isAccessible = true
val modifiersField = method.javaClass.getDeclaredField("modifiers")
modifiersField.isAccessible = true
modifiersField.setInt(method, modifiersField.modifiers and Modifier.FINAL.inv())
}
Upvotes: 2
Views: 741
Reputation: 3134
It's impossible because reflection just read the compiled program, which can't be modified at runtime, because Java is a compiled language, not a scripting language.
You can, however, dynamically generate and load classes, like this.
Upvotes: 1