mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-11 02:50:15 +08:00
Support static field INSTANCE for JavaPlugin
This commit is contained in:
parent
fdc6ef8a35
commit
c29b58d728
@ -12,7 +12,9 @@ package net.mamoe.mirai.console.internal.util
|
|||||||
import net.mamoe.mirai.console.internal.data.cast
|
import net.mamoe.mirai.console.internal.data.cast
|
||||||
import net.mamoe.mirai.console.internal.data.createInstanceOrNull
|
import net.mamoe.mirai.console.internal.data.createInstanceOrNull
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
import java.lang.reflect.Modifier
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
import java.lang.reflect.Member as JReflectionMember
|
||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
internal class ServiceList<T>(
|
internal class ServiceList<T>(
|
||||||
@ -37,13 +39,16 @@ internal object ServiceHelper {
|
|||||||
fun <T : Any> ClassLoader.loadService(
|
fun <T : Any> ClassLoader.loadService(
|
||||||
classname: String
|
classname: String
|
||||||
): T? {
|
): T? {
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
return kotlin.runCatching {
|
return kotlin.runCatching {
|
||||||
val clazz =
|
val clazz =
|
||||||
Class.forName(classname, true, this).cast<Class<out T>>()
|
Class.forName(classname, true, this).cast<Class<out T>>()
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
clazz.kotlin.objectInstance
|
clazz.kotlin.objectInstance
|
||||||
|
?: kotlin.runCatching {
|
||||||
|
clazz.declaredFields.firstOrNull { it.isStatic() && it.name == "INSTANCE" }?.get(null)
|
||||||
|
}.getOrNull()
|
||||||
?: clazz.kotlin.createInstanceOrNull()
|
?: clazz.kotlin.createInstanceOrNull()
|
||||||
?: clazz.constructors.firstOrNull { it.parameterCount == 0 }?.newInstance() as T?
|
?: clazz.constructors.firstOrNull { it.parameterCount == 0 }?.newInstance()
|
||||||
?: error("Cannot find a no-arg constructor")
|
?: error("Cannot find a no-arg constructor")
|
||||||
}.getOrElse {
|
}.getOrElse {
|
||||||
throw ServiceLoadException("Could not load service ${classname}.", it)
|
throw ServiceLoadException("Could not load service ${classname}.", it)
|
||||||
@ -53,10 +58,12 @@ internal object ServiceHelper {
|
|||||||
{ "Could not load PluginLoader ${pluginQualifiedName}." },
|
{ "Could not load PluginLoader ${pluginQualifiedName}." },
|
||||||
PluginLoadException("Could not load PluginLoader ${pluginQualifiedName}.", it)
|
PluginLoadException("Could not load PluginLoader ${pluginQualifiedName}.", it)
|
||||||
)*/
|
)*/
|
||||||
}
|
} as T?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal fun JReflectionMember.isStatic(): Boolean = this.modifiers.and(Modifier.STATIC) != 0
|
||||||
|
|
||||||
@Suppress("unused", "RedundantVisibilityModifier")
|
@Suppress("unused", "RedundantVisibilityModifier")
|
||||||
internal open class ServiceLoadException : RuntimeException {
|
internal open class ServiceLoadException : RuntimeException {
|
||||||
public constructor() : super()
|
public constructor() : super()
|
||||||
|
Loading…
Reference in New Issue
Block a user