private static BackgroundFunctionExecutor forClass()

in invoker/core/src/main/java/com/google/cloud/functions/invoker/BackgroundFunctionExecutor.java [120:157]


  private static BackgroundFunctionExecutor forClass(
      Class<?> functionClass, FunctionKind functionKind) {
    Object instance;
    try {
      instance = functionClass.getConstructor().newInstance();
    } catch (ReflectiveOperationException e) {
      throw new RuntimeException(
          "Could not construct an instance of " + functionClass.getName() + ": " + e, e);
    }
    FunctionExecutor<?> executor;
    switch (functionKind) {
      case RAW_BACKGROUND:
        executor = new RawFunctionExecutor((RawBackgroundFunction) instance);
        break;
      case BACKGROUND:
        BackgroundFunction<?> backgroundFunction = (BackgroundFunction<?>) instance;
        @SuppressWarnings("unchecked")
        Class<? extends BackgroundFunction<?>> c =
            (Class<? extends BackgroundFunction<?>>) backgroundFunction.getClass();
        Optional<Type> maybeTargetType = backgroundFunctionTypeArgument(c);
        if (!maybeTargetType.isPresent()) {
          // This is probably because the user implemented just BackgroundFunction rather than
          // BackgroundFunction<T>.
          throw new RuntimeException(
              "Could not determine the payload type for BackgroundFunction of type "
                  + instance.getClass().getName()
                  + "; must implement BackgroundFunction<T> for some T");
        }
        executor = new TypedFunctionExecutor<>(maybeTargetType.get(), backgroundFunction);
        break;
      case CLOUD_EVENTS:
        executor = new CloudEventFunctionExecutor((CloudEventsFunction) instance);
        break;
      default: // can't happen, we've listed all the FunctionKind values already.
        throw new AssertionError(functionKind);
    }
    return new BackgroundFunctionExecutor(executor);
  }