Do kotlin methods inherit annotation? From my test...
# announcements
n
Do kotlin methods inherit annotation? From my test it seems yes. How do I disable it?
n
I believe Kotlin annotations work the same as Java runtime-enabled annotations, so they shouldn't inherit.
c
I don’t think annotations inherit by default, it’s probably the test framework itself looking for annotations in the parent class
👆 1
n
I'm writing the reflection code myself. For some reason when I iterate through all functions of the object which has the annotation I want and execute them, I also execute the annotated functions from the parent class. I don't declare or annotate those functions in the child class. Weird :/
Code looks like this:
val testTasks = reflectionObject::class.memberFunctions.filter {
it.hasAnnotation<TestTask>()
}.sortedBy { it.findAnnotation<TestTask>()!!.order }
try {
for (task in testTasks) {
if (!(task.call(reflectionObject) as Boolean)) {
studentProc.failTest()
return
}
}
}
n
oh,
memberFunctions
maybe you want
declaredMemberFunctions
?
✔️ 1
n
@nanodeath Thanks a lot!! It solved the issue. But I don't quite understand why. declaredmemberfunctions exclude the methods from the parent class, but why do we need it when those are stripped of annotations and will be filtered out anyway?
n
🤷‍♂️
execute the annotated functions from the parent class
you said the parent class did have annotated methods
n
yes but as I understand it those methods are inherited without annotations. So the child object does have the methods but without annotations?
n
that's not really how inheritance works. when you call a method that's defined in a superclass, it runs the method defined in the superclass. inherited methods aren't actually copied into subclasses.
n
So what does "annotation inheritance" mean? I understand it that if A is subclass of B, then all annotations in B will be deleted from A in Kotlin, that is, A::class.memberfunctions contains both methods of B and A but without annotations.
n
annotation inheritance is like if you have an abstract class Foo with a method bar() in it, and you create a subclass that overrides bar(). if you ask for bar's annotations, do you get Foo's annotations? normally if you override a method and query the overridden method's annotations, it'll only return annotations declared on the override.