haroldadmin
09/22/2019, 8:50 AMinterface MyType {
fun giveHello(): String = "Hello!"
}
class Polite: MyType {
// Does not override giveHello()
}
class Rude: MyType {
override fun giveHello(): String = "I don't like you"
}
I get access to the giveHello
method using reflection like this:
val methodOfPolite = Polite::class.java.getDeclaredMethod("giveHello")
val methodOfRude = Rude::class.java.getDeclaredMethod("giveHello")
There's one weird thing here. The polite class does not override the giveHello
method, but the declaringClass
property of this method object still points to the class Polite.
So is there a way I can check whether the class actually did override the default interface method or not?
My use case looks something like this (assuming we can get the behaviour I'm asking for in a property called isOverriden
):
if (methodOfPolite.isOverriden) {
// do something
} else {
// do something else
}
Here's a link to the question on StackOverflow: https://stackoverflow.com/questions/58047229/how-to-check-if-a-class-has-overriden-a-default-method-from-an-interface-using-r/58047393karelpeeters
09/22/2019, 8:58 AM::class.java
there is no way to recover that information.instance::class.memberFunctions.first { it.name == "giveHello" } in instance::class.declaredFunctions
haroldadmin
09/22/2019, 9:13 AMkarelpeeters
09/22/2019, 9:17 AMharoldadmin
09/22/2019, 9:34 AM