[console] feat: load SPI Service in jvm plugin (#2247)

* feat: load SPI Service in jvm plugin

* add: api dump

* fix: docs
This commit is contained in:
cssxsh 2022-10-28 21:06:41 +08:00 committed by GitHub
parent 1b5f45cd70
commit 5bf9fdf5b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View File

@ -2236,6 +2236,8 @@ public abstract class net/mamoe/mirai/console/plugin/jvm/AbstractJvmPlugin : net
public final fun reloadPluginData (Lnet/mamoe/mirai/console/data/PluginData;)V
public final fun savePluginConfig (Lnet/mamoe/mirai/console/data/PluginConfig;)V
public final fun savePluginData (Lnet/mamoe/mirai/console/data/PluginData;)V
protected final fun services (Ljava/lang/Class;)Lkotlin/Lazy;
protected final synthetic fun services (Lkotlin/reflect/KClass;)Lkotlin/Lazy;
}
public final class net/mamoe/mirai/console/plugin/jvm/AbstractJvmPluginKt {

View File

@ -16,6 +16,7 @@ import net.mamoe.mirai.console.data.PluginConfig
import net.mamoe.mirai.console.data.PluginData
import net.mamoe.mirai.console.internal.plugin.JvmPluginClassLoaderN
import net.mamoe.mirai.console.internal.plugin.JvmPluginInternal
import net.mamoe.mirai.console.internal.util.PluginServiceHelper
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.console.permission.PermissionService
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
@ -23,6 +24,7 @@ import net.mamoe.mirai.utils.minutesToMillis
import net.mamoe.mirai.utils.secondsToMillis
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.reflect.KClass
/**
* [JavaPlugin] [KotlinPlugin] 的父类. 所有 [JvmPlugin] 都应该拥有此类作为直接或间接父类.
@ -93,6 +95,48 @@ public abstract class AbstractJvmPlugin @JvmOverloads constructor(
}
error("jvmPluginClasspath not available for $classLoader")
}
/**
* 获取 指定类的 SPI Service
*
* 为了兼容 Kotlin object 单例类此方法没有直接使用 java 原生的 API,
* 而是 手动读取 `META-INF/services/` 的内容, 并尝试构造或获取实例
*
* : 仅包括当前插件 JAR Service
*/
@JvmSynthetic
protected fun <T: Any> services(kClass: KClass<out T>): Lazy<List<T>> = lazy {
val classLoader = try {
jvmPluginClasspath.pluginClassLoader
} catch (_: IllegalStateException) {
this::class.java.classLoader
}
with(PluginServiceHelper) {
classLoader
.findServices(kClass)
.loadAllServices()
}
}
/**
* 获取 指定类的 SPI Service
*
* 为了兼容 Kotlin object 单例类此方法没有直接使用 java 原生的 API,
* 而是 手动读取 `META-INF/services/` 的内容, 并尝试构造或获取实例
*
* : 仅包括当前插件 JAR Service
*/
protected fun <T: Any> services(clazz: Class<out T>): Lazy<List<T>> = services(kClass = clazz.kotlin)
/**
* 获取 指定类的 SPI Service
*
* 为了兼容 Kotlin object 单例类此方法没有直接使用 java 原生的 API,
* 而是 手动读取 `META-INF/services/` 的内容, 并尝试构造或获取实例
*
* : 仅包括当前插件 JAR Service
*/
protected inline fun <reified T : Any> services(): Lazy<List<T>> = services(kClass = T::class)
}
/**