static Optional backgroundFunctionTypeArgument()

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


  static Optional<Type> backgroundFunctionTypeArgument(
      Class<? extends BackgroundFunction<?>> functionClass) {
    // If this is BackgroundFunction<Foo> then the user must have implemented a method
    // accept(Foo, Context), so we look for that method and return the type of its first argument.
    // We must be careful because the compiler will also have added a synthetic method
    // accept(Object, Context).
    return Arrays.stream(functionClass.getMethods())
        .filter(
            m ->
                m.getName().equals("accept")
                    && m.getParameterCount() == 2
                    && m.getParameterTypes()[1] == Context.class
                    && m.getParameterTypes()[0] != Object.class)
        .map(m -> m.getGenericParameterTypes()[0])
        .findFirst();
  }