예제 #1
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();
    }
  }
예제 #2
0
 /**
  * Build metadata from annotations on classes and methods
  *
  * @return
  */
 public static ResourceClass fromAnnotations(Class<?> clazz) {
   ResourceClassBuilder builder = resourceClass(clazz);
   for (Method method : clazz.getMethods()) {
     if (!method.isSynthetic() && !method.getDeclaringClass().equals(Object.class))
       processMethod(builder, clazz, method);
   }
   if (!clazz.isInterface()) {
     processFields(builder, clazz);
   }
   processSetters(builder, clazz);
   return builder.buildClass();
 }
예제 #3
0
 protected static void processDeclaredFields(
     ResourceClassBuilder resourceClassBuilder, Class<?> root) {
   for (Field field : root.getDeclaredFields()) {
     FieldParameterBuilder builder = resourceClassBuilder.field(field).fromAnnotations();
     if (builder.field.paramType == Parameter.ParamType.MESSAGE_BODY
         && !field.isAnnotationPresent(Body.class)) continue;
     if (builder.field.paramType == Parameter.ParamType.UNKNOWN) continue;
     builder.buildField();
   }
 }
예제 #4
0
 protected static void processDeclaredSetters(
     ResourceClassBuilder resourceClassBuilder, Class<?> root, Set<Long> visitedHashes) {
   for (Method method : root.getDeclaredMethods()) {
     if (!method.getName().startsWith("set")) continue;
     if (method.getParameterTypes().length != 1) continue;
     long hash = 0;
     try {
       hash = MethodHashing.methodHash(method);
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
     if (!Modifier.isPrivate(method.getModifiers()) && visitedHashes.contains(hash)) continue;
     visitedHashes.add(hash);
     SetterParameterBuilder builder = resourceClassBuilder.setter(method).fromAnnotations();
     if (builder.setter.paramType == Parameter.ParamType.MESSAGE_BODY
         && !method.isAnnotationPresent(Body.class)) continue;
     if (builder.setter.paramType == Parameter.ParamType.UNKNOWN) continue;
     builder.buildSetter();
   }
 }