diff --git a/docs-source/src/en/api/features.md b/docs-source/src/en/api/features.md index 418e9e2..75236e0 100644 --- a/docs-source/src/en/api/features.md +++ b/docs-source/src/en/api/features.md @@ -70,6 +70,52 @@ For more functions, please refer to [classOf](../api/public/com/highcapable/yuki ::: +### Lazy Loading + +Suppose we want to get a `Class` that cannot be called directly, but we do not need this `Class` immediately. + +At this time, you can use `lazyClass` to complete this function. + +> The following example + +```kotlin +// Lazy loading of this Class +val instance by lazyClass("com.demo.Test") +// Customize the ClassLoader where the Class is located +val customClassLoader: ClassLoader? = ... // Assume this is your ClassLoader +val instance by lazyClass("com.demo.Test") { customClassLoader } +// Call this Class at the appropriate time +instance.method { + // ... +} +``` + +If the current `Class` does not exist, using the above method will throw an exception. + +If you are not sure whether `Class` exists, you can refer to the following solution. + +> The following example + +```kotlin +// Lazy loading of this Class +// If not available, the result will be null but no exception will be thrown +val instance by lazyClassOrNull("com.demo.Test") +// Customize the ClassLoader where the Class is located +val customClassLoader: ClassLoader? = ... // Assume this is your ClassLoader +// If not available, the result will be null but no exception will be thrown +val instance by lazyClassOrNull("com.demo.Test") { customClassLoader } +// Call this Class at the appropriate time +instance?.method { + // ... +} +``` + +::: tip + +For more functions, please refer to [lazyClass](../public/com/highcapable/yukireflection/factory/ReflectionFactory#lazyclass-method), [lazyClassOrNull](../public/com/highcapable/yukireflection/factory/ReflectionFactory#lazyclassornull-method) methods. + +::: + ### Existential Judgment Suppose we want to determine whether a `Class` exists. diff --git a/docs-source/src/zh-cn/api/features.md b/docs-source/src/zh-cn/api/features.md index 73cda74..f0423ad 100644 --- a/docs-source/src/zh-cn/api/features.md +++ b/docs-source/src/zh-cn/api/features.md @@ -66,6 +66,50 @@ var instance = classOf(customClassLoader) ::: +### 延迟装载 + +假设我们要得到一个不能直接调用的 `Class`,但是我们也不是立刻就需要这个 `Class`。 + +这个时候,你可以使用 `lazyClass` 来完成这个功能。 + +> 示例如下 + +```kotlin +// 延迟装载这个 Class +val instance by lazyClass("com.demo.Test") +// 自定义 Class 所在的 ClassLoader +val customClassLoader: ClassLoader? = ... // 假设这个就是你的 ClassLoader +val instance by lazyClass("com.demo.Test") { customClassLoader } +// 在适当的时候调用这个 Class +instance.method { + // ... +} +``` + +如果当前 `Class` 并不存在,使用上述方法会抛出异常,如果你不确定 `Class` 是否存在,可以参考下面的解决方案。 + +> 示例如下 + +```kotlin +// 延迟装载这个 Class +// 得不到时结果会为 null 但不会抛出异常 +val instance by lazyClassOrNull("com.demo.Test") +// 自定义 Class 所在的 ClassLoader +val customClassLoader: ClassLoader? = ... // 假设这个就是你的 ClassLoader +// 得不到时结果会为 null 但不会抛出异常 +val instance by lazyClassOrNull("com.demo.Test") { customClassLoader } +// 在适当的时候调用这个 Class +instance?.method { + // ... +} +``` + +::: tip + +更多功能请参考 [lazyClass](../public/com/highcapable/yukireflection/factory/ReflectionFactory#lazyclass-method)、[lazyClassOrNull](../public/com/highcapable/yukireflection/factory/ReflectionFactory#lazyclassornull-method) 方法。 + +::: + ### 存在判断 假设我们要判断一个 `Class` 是否存在,通常情况下,我们可以使用标准的反射 API 去查找这个 `Class` 通过异常来判断是否存在。