Exemplo n.º 1
0
  /**
   * Invokes the method, adjusting arguments as required.
   *
   * <p>That is:
   *
   * <ul>
   *   <li>if the method declares parameters but no arguments are provided, then will provide 'null'
   *       defaults for these.
   *   <li>if the method does not declare parameters but arguments were provided, then will ignore
   *       those argumens.
   * </ul>
   */
  @SuppressWarnings("unused")
  private static Object invokeWithDefaults(
      final Method method, final ObjectAdapter adapter, final ObjectAdapter[] argumentAdapters) {
    final int numParams = method.getParameterTypes().length;
    ObjectAdapter[] adapters;

    if (argumentAdapters == null || argumentAdapters.length == 0) {
      adapters = new ObjectAdapter[numParams];
    } else if (numParams == 0) {
      // ignore any arguments, even if they were supplied.
      // eg used by contributee actions, but
      // underlying service 'default' action declares no params
      adapters = new ObjectAdapter[0];
    } else if (argumentAdapters.length == numParams) {
      adapters = argumentAdapters;
    } else {
      throw new IllegalArgumentException(
          "Method has "
              + numParams
              + " params but "
              + argumentAdapters.length
              + " arguments provided");
    }

    return AdapterInvokeUtils.invoke(method, adapter, adapters);
  }
Exemplo n.º 2
0
  /**
   * Invokes the method, adjusting arguments as required to make them fit the method's parameters.
   *
   * <p>That is:
   *
   * <ul>
   *   <li>if the method declares parameters but arguments are missing, then will provide 'null'
   *       defaults for these.
   *   <li>if the method does not declare all parameters for arguments, then truncates arguments.
   * </ul>
   */
  public static Object invokeAutofit(
      final Method method,
      final ObjectAdapter target,
      List<ObjectAdapter> argumentsIfAvailable,
      final AdapterManager adapterManager) {
    final List<ObjectAdapter> args = Lists.newArrayList();
    if (argumentsIfAvailable != null) {
      args.addAll(argumentsIfAvailable);
    }

    adjust(method, args, adapterManager);

    final ObjectAdapter[] argArray = args.toArray(new ObjectAdapter[] {});
    return AdapterInvokeUtils.invoke(method, target, argArray);
  }