public Object invoke()

in ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/OperationInvocationHandler.java [135:221]


  public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    if (isSelfMethod(method)) {
      return invokeSelfMethod(method, args);
    } else {
      final Operation operation = method.getAnnotation(Operation.class);
      if (operation != null) {
        final Annotation[][] annotations = method.getParameterAnnotations();
        final List<String> parameterNames;
        final LinkedHashMap<Parameter, Object> parameters = new LinkedHashMap<Parameter, Object>();
        if (annotations == null || annotations.length == 0) {
          parameterNames = null;
        } else {
          parameterNames = new ArrayList<String>();
          for (int i = 0; i < args.length; i++) {
            for (Annotation paramAnnotation : annotations[i]) {
              if (paramAnnotation instanceof Parameter) {
                parameterNames.add(((Parameter) paramAnnotation).name());
                parameters.put((Parameter) paramAnnotation, args[i]);
              }
            }

            if (parameters.size() <= i) {
              throw new IllegalArgumentException(
                      "Paramter " + i + " is not annotated as @" + Parameter.class.getSimpleName());
            }
          }
        }

        final Map.Entry<URI, EdmOperation> edmOperation;
        if (target instanceof EntityContainerInvocationHandler) {
          edmOperation = getUnboundOperation(operation, parameterNames);
        } else if (target instanceof EntityInvocationHandler) {
          edmOperation = getBoundOperation(operation, parameterNames);
        } else if (target instanceof EntityCollectionInvocationHandler) {
          edmOperation = getCollectionBoundOperation(operation, parameterNames);
        } else {
          throw new IllegalStateException("Invalid target invocation");
        }

        final Map<String, ClientValue> parameterValues = new LinkedHashMap<String, ClientValue>();
        for (Map.Entry<Parameter, Object> parameter : parameters.entrySet()) {
          if (!parameter.getKey().nullable() && parameter.getValue() == null) {
            throw new IllegalArgumentException(
                    "Parameter " + parameter.getKey().name() + " is not nullable but a null value was provided");
          }

          final EdmTypeInfo parameterType = new EdmTypeInfo.Builder().
                  setEdm(service.getClient().getCachedEdm()).setTypeExpression(parameter.getKey().type()).build();

          final ClientValue paramValue = parameter.getValue() == null
                  ? null
                  : CoreUtils.getODataValue(service.getClient(), parameterType, parameter.getValue());

          parameterValues.put(parameter.getKey().name(), paramValue);
        }

        final EdmTypeInfo returnType = edmOperation.getValue().getReturnType() == null
                ? null
                : new EdmTypeInfo.Builder().setEdm(service.getClient().getCachedEdm()).setTypeExpression(
                        edmOperation.getValue().getReturnType().getType().getFullQualifiedName().toString()).build();

        final InvokerInvocationHandler handler = returnType != null
                && (returnType.isEntityType() || returnType.isComplexType()) && operation.isComposable()
                ? new StructuredComposableInvokerInvocationHandler(
                        edmOperation.getKey(),
                        parameterValues,
                        operation,
                        edmOperation.getValue(),
                        ClassUtils.getTypeArguments(method.getReturnType().getGenericInterfaces()[0]),
                        returnType,
                        service)
                : new InvokerInvocationHandler(
                        edmOperation.getKey(),
                        parameterValues,
                        operation,
                        edmOperation.getValue(),
                        ClassUtils.getTypeArguments(method.getGenericReturnType()),
                        service);
        return Proxy.newProxyInstance(
                Thread.currentThread().getContextClassLoader(),
                new Class<?>[] {ClassUtils.getTypeClass(method.getGenericReturnType())},
                handler);
      } else {
        throw new NoSuchMethodException(method.getName());
      }
    }
  }