So I've got a code:
@Path("/foo") public class Hello { @GET @Produces("text/html") public String getHtml(@Context Request request, @Context HttpServletRequest requestss){ ... }
I am using AspectJ to catch all calls to getHtml
method. I would like to get parameters passed to @Produces
and to @Path
in my advice, i.e. "/foo"
and "text/html"
in this case. How can I do it using reflection ?
getParameterAnnotations() method of Method class returns a two-dimensional Annotation array, that represents the annotations on the parameters, of the Method object. If the Method contains no parameters, an empty array will be returned.
@interface is used to create your own (custom) Java annotations. Annotations are defined in their own file, just like a Java class or interface. Here is custom Java annotation example: @interface MyAnnotation { String value(); String name(); int age(); String[] newNames(); }
Annotations require constant values and a method parameter is dynamic. Show activity on this post. Annotation Usage: @CacheClear(pathToVersionId = "[0]") public int importByVersionId(Long versionTo){ ...... }
Retention Policy: A retention policy determines at what point an annotation is discarded. It is s specified using Java's built-in annotations: @Retention [About] 1. SOURCE: annotation retained only in the source file and is discarded during compilation.
To get value of the @Path
parameter:
String path = Hello.class.getAnnotation(Path.class).value();
Similarly, Once you have hold of Method
getHtml
Method m = Hello.class.getMethod("getHtml", ..); String mime = m.getAnnotation(Produces.class).value;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With