Skip to content

Commit 770f253

Browse files
committed
docs: update kavaref-core
1 parent b1a2097 commit 770f253

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

docs-source/src/en/library/kavaref-core.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,25 @@ but we **do not recommend this**, which will mask the problem unless it is neces
489489
If you set `optional()`, please do not use `firstMethod`, `firstConstructor` and other methods to get a single result.
490490
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.
491491

492+
But one thing you need to pay attention to here is that if you do not set `optional()`,
493+
then methods such as `firstMethodOrNull` will still throw exceptions when there is no result**, which is the expected behavior because `method { ... }`
494+
This is the "build" operation of the filter. Exceptions are handled here.
495+
Methods such as `firstMethodOrNull` are just an encapsulation.
496+
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.
497+
498+
So you must do it like the following.
499+
500+
> The following example
501+
502+
```kotlin
503+
Test::class.resolve()
504+
// Set optional conditions.
505+
.optional()
506+
.firstMethodOrNull {
507+
name = "doNonExistentMethod"
508+
} // Return MethodResolver or null.
509+
```
510+
492511
:::
493512

494513
### Log Management

docs-source/src/zh-cn/library/kavaref-core.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,22 @@ No method found matching the condition for current class.
474474
如果你设置了 `optional()`,那么请不要使用 `firstMethod``firstConstructor` 等方法来获取单个结果,
475475
因为它们会在没有结果时抛出列表为空的异常,你可以使用后缀为 `OrNull` 的方法来获取单个结果。
476476

477+
但是这里需要注意一个事情,**如果你没有设置 `optional()`,那么 `firstMethodOrNull` 等方法依然会在没有结果时抛出异常**,这是预期行为,因为 `method { ... }`
478+
才是过滤器的 “构建” 操作,异常在此处理,`firstMethodOrNull` 等方法只是一个封装,是对结果 `List` 是否为空的 Kotlin 自身标准库异常处理,不参与 KavaRef 过滤器的异常处理。
479+
480+
所以你一定要像下面这样做。
481+
482+
> 示例如下
483+
484+
```kotlin
485+
Test::class.resolve()
486+
// 设置可选条件
487+
.optional()
488+
.firstMethodOrNull {
489+
name = "doNonExistentMethod"
490+
} // 返回 MethodResolver 或 null
491+
```
492+
477493
:::
478494

479495
### 日志管理

0 commit comments

Comments
 (0)