Exemplo n.º 1
0
  private MethodInfo chooseMethodWithMatchingParameters(
      Exchange exchange, String parameters, Collection<MethodInfo> operationList)
      throws AmbiguousMethodCallException {
    // we have hardcoded parameters so need to match that with the given operations
    Iterator<?> it = ObjectHelper.createIterator(parameters);
    int count = 0;
    while (it.hasNext()) {
      it.next();
      count++;
    }

    List<MethodInfo> operations = new ArrayList<MethodInfo>();
    for (MethodInfo info : operationList) {
      if (info.getParameters().size() == count) {
        operations.add(info);
      }
    }

    if (operations.isEmpty()) {
      return null;
    } else if (operations.size() == 1) {
      return operations.get(0);
    }

    // okay we still got multiple operations, so need to match the best one
    List<MethodInfo> candidates = new ArrayList<MethodInfo>();
    for (MethodInfo info : operations) {
      it = ObjectHelper.createIterator(parameters);
      int index = 0;
      boolean matches = true;
      while (it.hasNext()) {
        String parameter = (String) it.next();
        Class<?> parameterType = BeanHelper.getValidParameterType(parameter);
        Class<?> expectedType = info.getParameters().get(index).getType();

        if (parameterType != null && expectedType != null) {
          if (!parameterType.isAssignableFrom(expectedType)) {
            matches = false;
            break;
          }
        }

        index++;
      }

      if (matches) {
        candidates.add(info);
      }
    }

    if (candidates.size() > 1) {
      MethodInfo answer = getSingleCovariantMethod(candidates);
      if (answer == null) {
        throw new AmbiguousMethodCallException(exchange, candidates);
      }
      return answer;
    }
    return candidates.size() == 1 ? candidates.get(0) : null;
  }
Exemplo n.º 2
0
  private MethodInfo chooseMethodWithMatchingParameters(
      Exchange exchange, String parameters, Collection<MethodInfo> operationList)
      throws AmbiguousMethodCallException {
    // we have hardcoded parameters so need to match that with the given operations
    Iterator<?> it = ObjectHelper.createIterator(parameters);
    int count = 0;
    while (it.hasNext()) {
      it.next();
      count++;
    }

    List<MethodInfo> operations = new ArrayList<MethodInfo>();
    for (MethodInfo info : operationList) {
      if (info.getParameters().size() == count) {
        operations.add(info);
      }
    }

    if (operations.isEmpty()) {
      return null;
    } else if (operations.size() == 1) {
      return operations.get(0);
    }

    // okay we still got multiple operations, so need to match the best one
    List<MethodInfo> candidates = new ArrayList<MethodInfo>();
    MethodInfo fallbackCandidate = null;
    for (MethodInfo info : operations) {
      it = ObjectHelper.createIterator(parameters, ",", false);
      int index = 0;
      boolean matches = true;
      while (it.hasNext()) {
        String parameter = (String) it.next();
        if (parameter != null) {
          // must trim
          parameter = parameter.trim();
        }

        Class<?> parameterType = BeanHelper.getValidParameterType(parameter);
        Class<?> expectedType = info.getParameters().get(index).getType();

        if (parameterType != null && expectedType != null) {

          // if its a simple language then we need to evaluate the expression
          // so we have the result and can find out what type the parameter actually is
          if (StringHelper.hasStartToken(parameter, "simple")) {
            LOG.trace(
                "Evaluating simple expression for parameter #{}: {} to determine the class type of the parameter",
                index,
                parameter);
            Object out =
                getCamelContext()
                    .resolveLanguage("simple")
                    .createExpression(parameter)
                    .evaluate(exchange, Object.class);
            if (out != null) {
              parameterType = out.getClass();
            }
          }

          // skip java.lang.Object type, when we have multiple possible methods we want to avoid it
          // if possible
          if (Object.class.equals(expectedType)) {
            fallbackCandidate = info;
            matches = false;
            break;
          }

          boolean matchingTypes = isParameterMatchingType(parameterType, expectedType);
          if (!matchingTypes) {
            matches = false;
            break;
          }
        }

        index++;
      }

      if (matches) {
        candidates.add(info);
      }
    }

    if (candidates.size() > 1) {
      MethodInfo answer = getSingleCovariantMethod(candidates);
      if (answer != null) {
        return answer;
      }
    }
    return candidates.size() == 1 ? candidates.get(0) : fallbackCandidate;
  }