Suppose I have this annotation class
@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(){ ... }
}
So is there a way to look into an object, “seek” out the method with the @MethodXY annotation, where its element x = 3, y = 2, and invoke it?
Thanks
Here is a method, which returns methods with specific annotations:
It can be easily modified to your specific needs. Pls note that the provided method traverses class hierarchy in order to find methods with required annotations.
Here is a method for your specific needs:
For invocation of the found method(s) pls refer a tutorial. One of the potential difficulties here is the number of method arguments, which could vary between found methods and thus requiring some additional processing.