Beispiel #1
0
  protected MethodInfo createMethodInfo(Class<?> clazz, Method method) {
    Class<?>[] parameterTypes = method.getParameterTypes();
    List<Annotation>[] parametersAnnotations = collectParameterAnnotations(clazz, method);

    List<ParameterInfo> parameters = new ArrayList<ParameterInfo>();
    List<ParameterInfo> bodyParameters = new ArrayList<ParameterInfo>();

    boolean hasCustomAnnotation = false;
    boolean hasHandlerAnnotation =
        ObjectHelper.hasAnnotation(method.getAnnotations(), Handler.class);

    int size = parameterTypes.length;
    if (LOG.isTraceEnabled()) {
      LOG.trace(
          "Creating MethodInfo for class: {} method: {} having {} parameters",
          new Object[] {clazz, method, size});
    }

    for (int i = 0; i < size; i++) {
      Class<?> parameterType = parameterTypes[i];
      Annotation[] parameterAnnotations =
          parametersAnnotations[i].toArray(new Annotation[parametersAnnotations[i].size()]);
      Expression expression =
          createParameterUnmarshalExpression(clazz, method, parameterType, parameterAnnotations);
      hasCustomAnnotation |= expression != null;

      ParameterInfo parameterInfo =
          new ParameterInfo(i, parameterType, parameterAnnotations, expression);
      LOG.trace("Parameter #{}: {}", i, parameterInfo);
      parameters.add(parameterInfo);
      if (expression == null) {
        boolean bodyAnnotation = ObjectHelper.hasAnnotation(parameterAnnotations, Body.class);
        LOG.trace("Parameter #{} has @Body annotation", i);
        hasCustomAnnotation |= bodyAnnotation;
        if (bodyParameters.isEmpty()) {
          // okay we have not yet set the body parameter and we have found
          // the candidate now to use as body parameter
          if (Exchange.class.isAssignableFrom(parameterType)) {
            // use exchange
            expression = ExpressionBuilder.exchangeExpression();
          } else {
            // assume it's the body and it must be mandatory convertible to the parameter type
            // but we allow null bodies in case the message really contains a null body
            expression = ExpressionBuilder.mandatoryBodyExpression(parameterType, true);
          }
          LOG.trace("Parameter #{} is the body parameter using expression {}", i, expression);
          parameterInfo.setExpression(expression);
          bodyParameters.add(parameterInfo);
        } else {
          // will ignore the expression for parameter evaluation
        }
      }
      LOG.trace("Parameter #{} has parameter info: ", i, parameterInfo);
    }

    // now let's add the method to the repository
    return new MethodInfo(
        camelContext,
        clazz,
        method,
        parameters,
        bodyParameters,
        hasCustomAnnotation,
        hasHandlerAnnotation);
  }