I
Ingo R. Homann
Hi,
consider the following design:
@interface MyAnnot {}
class Sup {
@MyAnnot void foo() {...}
}
class Sub {
@Overrides void foo() {...}
}
Using reflection, i am accessing the method Sub#foo. Is there a *simple*
possibility to find out that the 'original' method (Sup#foo) was
annotated with @MyAnnot?
I thought, that getAnnotations() vs. getDeclaredAnnotations() would do
the job (like Class#getMethods() vs. getDeclaredMethods()), and that
java.lang.annotation Type Inherited could help. But I understand the
doku to this meta annotation, that this only works, if a class (rather
than a method) is annotated (my test indicate that it does not work the
way I want to).
Did I understand that correctly?
Why is this the case?
Is there a reasonable eh reason for this 'asymetric', limited design
that sun chose?
What is the alternative?
Is (*) the only solution?
Ciao,
Ingo
(*) to recurse over all supertypes of the class (which is not really
comfortable with reflection, because I would have to recursively check
all implementing interfaces, the super class and all its interfaces, and
all super interfaces of the interfaces), check if there is a method with
the same signature and then check if this method has the annotation I am
looking for.
consider the following design:
@interface MyAnnot {}
class Sup {
@MyAnnot void foo() {...}
}
class Sub {
@Overrides void foo() {...}
}
Using reflection, i am accessing the method Sub#foo. Is there a *simple*
possibility to find out that the 'original' method (Sup#foo) was
annotated with @MyAnnot?
I thought, that getAnnotations() vs. getDeclaredAnnotations() would do
the job (like Class#getMethods() vs. getDeclaredMethods()), and that
java.lang.annotation Type Inherited could help. But I understand the
doku to this meta annotation, that this only works, if a class (rather
than a method) is annotated (my test indicate that it does not work the
way I want to).
Did I understand that correctly?
Why is this the case?
Is there a reasonable eh reason for this 'asymetric', limited design
that sun chose?
What is the alternative?
Is (*) the only solution?
Ciao,
Ingo
(*) to recurse over all supertypes of the class (which is not really
comfortable with reflection, because I would have to recursively check
all implementing interfaces, the super class and all its interfaces, and
all super interfaces of the interfaces), check if there is a method with
the same signature and then check if this method has the annotation I am
looking for.