Class ResourceAnalyzer

java.lang.Object
ru.vyarus.dropwizard.guice.url.resource.ResourceAnalyzer

public final class ResourceAnalyzer extends Object
Utility to search jersey annotations on resource class. Jersey assumes that all annotations are declared on the same method! This could be a superclass method or implemented interface.
Since:
25.09.2025
  • Method Details

    • validateResourceMethod

      public static void validateResourceMethod(Class<?> resource, Method method)
      Check that provided method belongs to resource class hierarchy (it might be superclass method or interface method).
      Parameters:
      resource - resource class
      method - target method to verify
    • getResourcePath

      public static String getResourcePath(Class<?> resource)
      Search for Path annotation on resource class or it's superclasses or interfaces.

      The result is normalized: will always start with slash and will never end with slash (simpler for concatenation).

      Parameters:
      resource - resource class to analyze
      Returns:
      path annotation value
      Throws:
      IllegalStateException - if path annotation is not found on a resource or any of it's super classes and interfaces
    • getMethodPath

      public static String getMethodPath(Method method)
      Search for the actual annotated method (could be superclass or interface) and read the configured path.

      The result is normalized: will always start with slash and will never end with slash (simpler for concatenation).

      Parameters:
      method - resource method
      Returns:
      value of Path annotation on metho
      Throws:
      IllegalStateException - if annotation not found
    • getMethodPath

      public static String getMethodPath(Class<?> resource, String method)
      Search for resource method @Path annotation. If multiple methods selected, will select method without arguments. If there is no matching no-args method found throws exception (about multiple methods found).
      Parameters:
      resource - resource class
      method - method name
      Returns:
      resource method @Path annotation value
      Throws:
      IllegalStateException - if method with annotations not found
    • findAnnotatedResource

      public static Class<?> findAnnotatedResource(Class<?> resource)
      Search for the actual resource annotations source (might be superclass of interface).
      Parameters:
      resource - resource class
      Returns:
      resource class or its superclass or interface where Path annotation declared
    • findMethod

      public static Method findMethod(Class<?> resource, String methodName, Class<?>... parameters)
      Shortcut for class.getMethod(Par1.class, Par2.class).
      Parameters:
      resource - resource class
      methodName - method name
      parameters - method parameter types
      Returns:
      method instance
      Throws:
      IllegalStateException - when method not found
    • findMethod

      public static Method findMethod(Class<?> resource, String method)
      Search for method with provided name in resource. If multiple methods selected, will select method without arguments. If there is no matching no-args method found throws exception (about multiple methods found). This no-args behavior is required to comply with arguments-based search (it would be otherwise impossible to search for no-args method).

      Returned method might be not the resource method, but a super method from a subclass or interface, where jersey annotations declared.

      Parameters:
      resource - resource clas
      method - method name
      Returns:
      resource method with jersey annotations
      Throws:
      IllegalStateException - if the method is not found or multiple methods found
    • findAnnotatedMethod

      public static Method findAnnotatedMethod(Method method)
      Search method, annotated with Path. Jersey supports declaring annotations in superclass or on interface, so the resource method may miss actual annotations.
      Parameters:
      method - method to search annotated for
      Returns:
      annotated method
      Throws:
      IllegalStateException - if method with annotations not found
    • isJerseyAnnotated

      public static boolean isJerseyAnnotated(Method method)
      Check if provided method is annotated with jersey annotations. Http methods must have http method annotation (like GET), but may lack Path annotation. Sub-resource lookup method must have annotation. So target method must be checked to contain one of possible annotations.

      Note that it is impossible to have Path and http method annotation on different methods (jersey requirement).

      Parameters:
      method - method to check
      Returns:
      true if method contains jersey annotations
    • findHttpMethod

      public static String findHttpMethod(Method method)
      Resolve http method by searching for method annotations like GET or POST.

      WARNING: does not search the correct annotated method (see findAnnotatedMethod(java.lang.reflect.Method)) - assumed correct method was already found.

      Parameters:
      method - method to read annotations on
      Returns:
      http method string (from method annotations)
      Throws:
      IllegalStateException - if annotation not found
    • getHttpMethod

      public static Optional<String> getHttpMethod(Method method)
      Search http annotations on method (like GET).
      Parameters:
      method - method to find annotation on
      Returns:
      http method or null
    • analyzeMethodCall

      public static <T> ResourceMethodInfo analyzeMethodCall(Class<T> resource, Caller<T> consumer)
      Use resource method call to collect data, required to call this rest method (like method path, query or path params). Method associate provided method arguments with data required for call (like pre-declared query or path params).

      Provided consumer must call one resource method with any arguments: analyzeMethod(Res.class, Res mock -> mock.someMethod(12, "foo", null)) where resource method is: Response someMethod(@QueryParam("p1") int p1, @PathParam("pp") String pp, @Context UriInfo info. From the call above we can:

      • find Path method annotation to get the path
      • find http method annotation
      • record required query param value: p1=12
      • record required path param value: pp="foo"

      Sub method calls assumed to be a sub-resource calls like resource.subResource(args).method(args). For sub-resource calls, returned info will contain root resource as base resource, but resource method path will include all paths from locator methods (class-level Path annotation is ignored for sub-resources!).

      Type Parameters:
      T - resource type
      Parameters:
      resource - resource class
      consumer - consumer calling exactly one resource method
      Returns:
      method analysis info
    • analyzeMethodCall

      public static ResourceMethodInfo analyzeMethodCall(MethodCall call)
      Analyze resource method, using method call arguments. Recognize query params, path params, headers, cookies, etc. from method parameter annotations for non-null arguments.

      Sub method calls assumed to be a sub-resource calls like resource.subResource(args).method(args). For sub-resource calls, returned info will contain root resource as base resource, but resource method path will include all paths from locator methods (class-level Path annotation is ignored for sub-resources!).

      Method call could be intercepted with ResourceMethodLookup.getMethodCalls(Class, ru.vyarus.dropwizard.guice.url.util.Caller).

      Parameters:
      call - intercepted method call
      Returns:
      analysis result
    • analyzeSingleCall

      public static ResourceMethodInfo analyzeSingleCall(MethodCall call)
      Analyze method call, ignoring sub calls MethodCall.getSubCall(). See analyzeMethodCall(MethodCall) for complete analysis.
      Parameters:
      call - method call
      Returns:
      single call analysis info