Skip to content
Advertisement

Java seek a method with specific annotation and its annotation element

Suppose I have this annotation class

JavaScript

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

Advertisement

Answer

Here is a method, which returns methods with specific annotations:

JavaScript

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:

JavaScript

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement