diff --git a/mirai-core-utils/src/jvmBaseMain/kotlin/Services.kt b/mirai-core-utils/src/jvmBaseMain/kotlin/Services.kt index 979c380e850..719c30cf59e 100644 --- a/mirai-core-utils/src/jvmBaseMain/kotlin/Services.kt +++ b/mirai-core-utils/src/jvmBaseMain/kotlin/Services.kt @@ -16,6 +16,7 @@ import kotlin.reflect.full.createInstance public actual fun loadService(clazz: KClass, fallbackImplementation: String?): T { var suppressed: Throwable? = null return ServiceLoader.load(clazz.java).firstOrNull() + ?: ServiceLoader.load(clazz.java, clazz.java.classLoader).firstOrNull() ?: (if (fallbackImplementation == null) null else runCatching { findCreateInstance(fallbackImplementation) }.onFailure { suppressed = it }.getOrNull()) ?: throw NoSuchElementException("Could not find an implementation for service class ${clazz.qualifiedName}").apply { @@ -29,10 +30,18 @@ private fun findCreateInstance(fallbackImplementation: String): T { public actual fun loadServiceOrNull(clazz: KClass, fallbackImplementation: String?): T? { return ServiceLoader.load(clazz.java).firstOrNull() + ?: ServiceLoader.load(clazz.java, clazz.java.classLoader).firstOrNull() ?: if (fallbackImplementation == null) return null else runCatching { findCreateInstance(fallbackImplementation) }.getOrNull() } public actual fun loadServices(clazz: KClass): Sequence { - return ServiceLoader.load(clazz.java).asSequence() + return sequence { + val current = ServiceLoader.load(clazz.java).iterator() + if (current.hasNext()) { + yieldAll(current) + } else { + yieldAll(ServiceLoader.load(clazz.java, clazz.java.classLoader)) + } + } } \ No newline at end of file