I am writing some code to figure out metadata about classes implemented with JAX-RS
and I'm writing a method that takes a Method
and returns the HTTP Verb related to that method, basically figure out if it's annotated with @POST
, @GET
, @PUT
or @DELETE
.
What I currently have is this:
private static String extractHttpVerb(Method method) {
if(method.getAnnotation(GET.class) != null) {
return "GET";
} else if (method.getAnnotation(POST.class) != null) {
return "POST";
} else if (method.getAnnotation(PUT.class) != null) {
return "PUT";
} else if (method.getAnnotation(DELETE.class) != null){
return "DELETE";
} else {
return "UNKNOWN";
}
}
It works fine, but I figured out that all those annotations are annotated with @HttpMethod
and have a value
with it's name as a String. Example:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("POST")
@Documented
public @interface POST {
}
So I was wondering. Is there a way for me to figure out from my reference of Method
if it's annotated by an annotation which in turn is annotated with another specific annotation?
Something like:
boolean annotated = method.hasAnnotationsAnnotatedBy(HttpMethod.class);
PS: I know that method doesn't exist, it's just to illustrate what I'm looking for.