Do kotlin methods inherit annotation? From my test...
# announcements
Do kotlin methods inherit annotation? From my test it seems yes. How do I disable it?
I believe Kotlin annotations work the same as Java runtime-enabled annotations, so they shouldn't inherit.
I don’t think annotations inherit by default, it’s probably the test framework itself looking for annotations in the parent class
👆 1
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 {
}.sortedBy { it.findAnnotation<TestTask>()!!.order }
try {
for (task in testTasks) {
if (!( as Boolean)) {
maybe you want
✔️ 1
@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?
execute the annotated functions from the parent class
you said the parent class did have annotated methods
yes but as I understand it those methods are inherited without annotations. So the child object does have the methods but without annotations?
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.
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.
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.