mercredi 20 janvier 2016

Java reflection: Find base class/interface a method was declared in

I have a public API like:

interface Alien {
    @Help( "Make him say something" )
    String speak();
}

and an implementation like:

class Dalek implements Alien {
    @Override
    String speak(){ return "Exterminate!"; }
}

Now I need to access the Annotation @Help on the base interface but I only have the implementation / derived class which may implements other interfaces or extend other classes, too.

So is there a sane way in Java to find the "base" method which is overridden or another way to get to that annotation?

Pseudocode for what I'm looking for would be like:

Method m;

m.getDeclaringMethod().getAnnotation( Help.class );

// or:

m.getAnnotationRecusively( Help.class )





Aucun commentaire:

Enregistrer un commentaire