[annotations][reflection] getting the annotation of the "super.method"

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top