Ejemplo n.º 1
0
  private static Method findAnnotatedMethod(Class<?> root, Method implementation) {
    // check the method itself
    if (implementation.isAnnotationPresent(Path.class)
        || IsHttpMethod.getHttpMethods(implementation) != null) return implementation;

    if (implementation.isAnnotationPresent(Produces.class)
        || implementation.isAnnotationPresent(Consumes.class)) {
      // completely abort this method
      return null;
    }

    // Per
    // http://download.oracle.com/auth/otn-pub/jcp/jaxrs-1.0-fr-oth-JSpec/jaxrs-1.0-final-spec.pdf
    // Section 3.2 Annotation Inheritance

    // Check possible superclass declarations
    for (Class<?> clazz = implementation.getDeclaringClass().getSuperclass();
        clazz != null;
        clazz = clazz.getSuperclass()) {
      try {
        Method method =
            clazz.getDeclaredMethod(implementation.getName(), implementation.getParameterTypes());
        if (method.isAnnotationPresent(Path.class) || IsHttpMethod.getHttpMethods(method) != null)
          return method;
        if (method.isAnnotationPresent(Produces.class)
            || method.isAnnotationPresent(Consumes.class)) {
          // completely abort this method
          return null;
        }
      } catch (NoSuchMethodException e) {
        // ignore
      }
    }

    // Not found yet, so next check ALL interfaces from the root,
    // but ensure no redefinition by peer interfaces (ambiguous) to preserve logic found in
    // original implementation
    for (Class<?> clazz = root; clazz != null; clazz = clazz.getSuperclass()) {
      Method method = null;
      for (Class<?> iface : clazz.getInterfaces()) {
        Method m = findAnnotatedInterfaceMethod(root, iface, implementation);
        if (m != null) {
          if (method != null && !m.equals(method))
            throw new RuntimeException(
                "Ambiguous inherited JAX-RS annotations applied to method: " + implementation);
          method = m;
        }
      }
      if (method != null) return method;
    }
    return null;
  }
Ejemplo n.º 2
0
  protected static void processMethod(
      ResourceClassBuilder resourceClassBuilder, Class<?> root, Method implementation) {
    Method method = findAnnotatedMethod(root, implementation);
    if (method != null) {
      Path path = method.getAnnotation(Path.class);
      Set<String> httpMethods = IsHttpMethod.getHttpMethods(method);

      ResteasyUriBuilder builder = new ResteasyUriBuilder();
      if (root.isAnnotationPresent(Path.class)) {
        builder.path(root);
      }
      if (path != null) {
        builder.path(method);
      }
      String pathExpression = builder.getPath();
      if (pathExpression == null) pathExpression = "";

      ResourceLocatorBuilder resourceLocatorBuilder;

      if (httpMethods == null) {
        resourceLocatorBuilder = resourceClassBuilder.locator(implementation, method);
      } else {
        ResourceMethodBuilder resourceMethodBuilder =
            resourceClassBuilder.method(implementation, method);
        resourceLocatorBuilder = resourceMethodBuilder;

        for (String httpMethod : httpMethods) {
          if (httpMethod.equalsIgnoreCase(HttpMethod.GET)) resourceMethodBuilder.get();
          else if (httpMethod.equalsIgnoreCase(HttpMethod.PUT)) resourceMethodBuilder.put();
          else if (httpMethod.equalsIgnoreCase(HttpMethod.POST)) resourceMethodBuilder.post();
          else if (httpMethod.equalsIgnoreCase(HttpMethod.DELETE)) resourceMethodBuilder.delete();
          else if (httpMethod.equalsIgnoreCase(HttpMethod.OPTIONS)) resourceMethodBuilder.options();
          else if (httpMethod.equalsIgnoreCase(HttpMethod.HEAD)) resourceMethodBuilder.head();
          else resourceMethodBuilder.httpMethod(httpMethod);
        }
        Produces produces = method.getAnnotation(Produces.class);
        if (produces == null)
          produces = resourceClassBuilder.resourceClass.getClazz().getAnnotation(Produces.class);
        if (produces == null) produces = method.getDeclaringClass().getAnnotation(Produces.class);
        if (produces != null) resourceMethodBuilder.produces(produces.value());

        Consumes consumes = method.getAnnotation(Consumes.class);
        if (consumes == null)
          consumes = resourceClassBuilder.resourceClass.getClazz().getAnnotation(Consumes.class);
        if (consumes == null) consumes = method.getDeclaringClass().getAnnotation(Consumes.class);
        if (consumes != null) resourceMethodBuilder.consumes(consumes.value());
      }
      resourceLocatorBuilder.path(pathExpression);
      for (int i = 0; i < resourceLocatorBuilder.locator.params.length; i++) {
        resourceLocatorBuilder.param(i).fromAnnotations();
      }
      resourceLocatorBuilder.buildMethod();
    }
  }
Ejemplo n.º 3
0
  private static Method findAnnotatedInterfaceMethod(
      Class<?> root, Class<?> iface, Method implementation) {
    for (Method method : iface.getMethods()) {
      if (method.isSynthetic()) continue;

      if (!method.getName().equals(implementation.getName())) continue;
      if (method.getParameterTypes().length != implementation.getParameterTypes().length) continue;

      Method actual = Types.getImplementingMethod(root, method);
      if (!actual.equals(implementation)) continue;

      if (method.isAnnotationPresent(Path.class) || IsHttpMethod.getHttpMethods(method) != null)
        return method;
    }
    for (Class<?> extended : iface.getInterfaces()) {
      Method m = findAnnotatedInterfaceMethod(root, extended, implementation);
      if (m != null) return m;
    }
    return null;
  }