diff --git a/docs-source/src/en/library/kavaref-core.md b/docs-source/src/en/library/kavaref-core.md index af7d2a1..08d2710 100644 --- a/docs-source/src/en/library/kavaref-core.md +++ b/docs-source/src/en/library/kavaref-core.md @@ -489,6 +489,25 @@ but we **do not recommend this**, which will mask the problem unless it is neces If you set `optional()`, please do not use `firstMethod`, `firstConstructor` and other methods to get a single result. Because they throw an exception with empty list when there is no result, you can use the method with the suffix `OrNull` to get a single result. +But one thing you need to pay attention to here is that if you do not set `optional()`, +then methods such as `firstMethodOrNull` will still throw exceptions when there is no result**, which is the expected behavior because `method { ... }` +This is the "build" operation of the filter. Exceptions are handled here. +Methods such as `firstMethodOrNull` are just an encapsulation. +It is an exception handling of Kotlin's own standard library whether the result `List` is empty. It does not participate in exception handling of KavaRef filters. + +So you must do it like the following. + +> The following example + +```kotlin +Test::class.resolve() + // Set optional conditions. + .optional() + .firstMethodOrNull { + name = "doNonExistentMethod" + } // Return MethodResolver or null. +``` + ::: ### Log Management diff --git a/docs-source/src/zh-cn/library/kavaref-core.md b/docs-source/src/zh-cn/library/kavaref-core.md index 125e637..90381f2 100644 --- a/docs-source/src/zh-cn/library/kavaref-core.md +++ b/docs-source/src/zh-cn/library/kavaref-core.md @@ -474,6 +474,22 @@ No method found matching the condition for current class. 如果你设置了 `optional()`,那么请不要使用 `firstMethod`、`firstConstructor` 等方法来获取单个结果, 因为它们会在没有结果时抛出列表为空的异常,你可以使用后缀为 `OrNull` 的方法来获取单个结果。 +但是这里需要注意一个事情,**如果你没有设置 `optional()`,那么 `firstMethodOrNull` 等方法依然会在没有结果时抛出异常**,这是预期行为,因为 `method { ... }` +才是过滤器的 “构建” 操作,异常在此处理,`firstMethodOrNull` 等方法只是一个封装,是对结果 `List` 是否为空的 Kotlin 自身标准库异常处理,不参与 KavaRef 过滤器的异常处理。 + +所以你一定要像下面这样做。 + +> 示例如下 + +```kotlin +Test::class.resolve() + // 设置可选条件 + .optional() + .firstMethodOrNull { + name = "doNonExistentMethod" + } // 返回 MethodResolver 或 null +``` + ::: ### 日志管理