GoogleCloudPlatform / GoogleCloudPlatform/functions-framework-java

BackgroundFunctionExecutor.java failes to detect type when sub-classed

未關閉
#95 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
bug P3
主要語言
Java
星號
154
分支
63
PR 合併指標
30 天內沒有已合併 PR

描述

Error Condition

>Exception in thread "main" java.lang.RuntimeException: Could not determine the payload type for BackgroundFunction of type com.example.MyFunctionHandler; must implement BackgroundFunction for some T at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:140) at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:113) at com.google.cloud.functions.invoker.runner.Invoker.startServer(Invoker.java:256) at com.google.cloud.functions.invoker.runner.Invoker.main(Invoker.java:127)

Occurs when `BackgroundFunction` is subclassed:
> `public abstract class MyExtendedBackgroundFunction implements BackgroundFunction`

and
> `public class MyFunctionHandler extends MyExtendedBackgroundFunction`

In the class mentioned, the following function fails to detect sub-classed BackgroundFunction type:

```
static Optional backgroundFunctionTypeArgument(Class> functionClass) {
// If this is BackgroundFunction 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();
}
```

This fails for described `MyFunctionHandler`.

The intent is to add common boilerplate handling for a couple of functions which is common to all. A separate abstract function is employed, but the function fails to load because the type is not determined.

Temporary workaround is to re-implement `accept()` with the type in the sub-classed object with the type, but is inelegant.

貢獻指南

開啟貢獻指南

研究方向

從 BackgroundFunctionExecutor.java 第 140 行開始,檢查 backgroundFunctionTypeArgument。重現 issue 中描述的 MyExtendedBackgroundFunction 與 MyFunctionHandler 階層,然後驗證 executor 能偵測到 MyReturnType,同時保留對直接實作的 BackgroundFunction 型別的行為。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
java
領域
backend, cloud
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。