Java recherche une méthode avec une annotation spécifique et son élément d’annotation

Supposons que je dispose de cette classe d’annotation

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodXY { public int x(); public int y(); } public class AnnotationTest { @MethodXY(x=5, y=5) public void myMethodA(){ ... } @MethodXY(x=3, y=2) public void myMethodB(){ ... } } 

Existe-t-il un moyen de rechercher un object, de “rechercher” la méthode avec l’annotation @MethodXY, où son élément x = 3, y = 2, et de l’appeler?

Merci

    Voici une méthode qui renvoie des méthodes avec des annotations spécifiques:

     public static List getMethodsAnnotatedWith(final Class type, final Class annotation) { final List methods = new ArrayList(); Class klass = type; while (klass != Object.class) { // need to iterated thought hierarchy in order to resortingeve methods from above the current instance // iterate though the list of methods declared in the class represented by klass variable, and add those annotated with the specified annotation final List allMethods = new ArrayList(Arrays.asList(klass.getDeclaredMethods())); for (final Method method : allMethods) { if (method.isAnnotationPresent(annotation)) { Annotation annotInstance = method.getAnnotation(annotation); // TODO process annotInstance methods.add(method); } } // move to the upper class in the hierarchy in search for more methods klass = klass.getSuperclass(); } return methods; } 

    Il peut être facilement modifié pour répondre à vos besoins spécifiques. Notez que la méthode fournie traverse la hiérarchie des classes afin de trouver des méthodes avec les annotations requirejses.

    Voici une méthode pour vos besoins spécifiques:

     public static List getMethodsAnnotatedWithMethodXY(final Class type) { final List methods = new ArrayList(); Class klass = type; while (klass != Object.class) { // need to iterated thought hierarchy in order to resortingeve methods from above the current instance // iterate though the list of methods declared in the class represented by klass variable, and add those annotated with the specified annotation final List allMethods = new ArrayList(Arrays.asList(klass.getDeclaredMethods())); for (final Method method : allMethods) { if (method.isAnnotationPresent(MethodXY.class)) { MethodXY annotInstance = method.getAnnotation(MethodXY.class); if (annotInstance.x() == 3 && annotInstance.y() == 2) { methods.add(method); } } } // move to the upper class in the hierarchy in search for more methods klass = klass.getSuperclass(); } return methods; } 

    Pour invoquer la ou les méthodes trouvées, reportez-vous à un tutoriel . L’une des difficultés potentielles est le nombre d’arguments de la méthode, qui peuvent varier entre les méthodes trouvées et nécessiter un traitement supplémentaire.

    essayez cet exemple de code:

     import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.annotation.ElementType; import java.lang.reflect.InvocationTargetException; class AnotTest { public static void main(Ssortingng... args) { AnnotationTest at = new AnnotationTest(); for (Method m : at.getClass().getMethods()) { MethodXY mXY = (MethodXY)m.getAnnotation(MethodXY.class); if (mXY != null) { if (mXY.x() == 3 && mXY.y() == 2){ try { m.invoke(at); } catch (IllegalAccessException e) { //do nothing; } catch (InvocationTargetException o) { //do nothing; } } } } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) static public @interface MethodXY { public int x(); public int y(); } static class AnnotationTest { @MethodXY(x=5, y=5) public void myMethodA() { System.out.println("boo"); } @MethodXY(x=3, y=2) public void myMethodB() { System.out.println("foo"); } } }