mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-25 15:40:28 +08:00
CallerFinder
This commit is contained in:
parent
9a25ac78fa
commit
c17762eaf5
@ -32,6 +32,7 @@ dependencies {
|
||||
|
||||
smartImplementation(yamlkt)
|
||||
smartImplementation(`jetbrains-annotations`)
|
||||
smartImplementation(`caller-finder`)
|
||||
smartApi(`kotlinx-coroutines-jdk8`)
|
||||
|
||||
testApi(`mirai-core-qqandroid`)
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
package net.mamoe.mirai.console.internal.util
|
||||
|
||||
import io.github.karlatemp.caller.StackFrame
|
||||
import net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl
|
||||
|
||||
internal inline fun <reified E : Throwable, R> runIgnoreException(block: () -> R): R? {
|
||||
@ -31,19 +32,9 @@ internal inline fun <reified E : Throwable> runIgnoreException(block: () -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
internal fun getCallerClassloader(): ClassLoader? {
|
||||
internal fun StackFrame.findLoader(): ClassLoader? {
|
||||
classInstance?.let { return it.classLoader }
|
||||
return runCatching {
|
||||
/*
|
||||
java.base/java.lang.Thread.getStackTrace(Thread.java:1598)
|
||||
net.mamoe.mirai.console.internal.util.CommonUtils.getCallerClassloader(CommonUtils.kt:37)
|
||||
net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription$Companion.loadFromResource$default(JvmPluginDescription.kt:67)
|
||||
net.mamoe.mirai.console.KotlinP.<init>(TestMiraiConosle.kt:34)
|
||||
net.mamoe.mirai.console.KotlinP.<clinit>(TestMiraiConosle.kt:34)
|
||||
net.mamoe.mirai.console.TestMiraiConosleKt.main(TestMiraiConosle.kt:37)
|
||||
net.mamoe.mirai.console.TestMiraiConosleKt.main(TestMiraiConosle.kt)
|
||||
*/
|
||||
val traces = Thread.currentThread().stackTrace
|
||||
val classname = traces[3].className
|
||||
BuiltInJvmPluginLoaderImpl.classLoaders.firstOrNull { it.findClass(classname, true) != null }
|
||||
BuiltInJvmPluginLoaderImpl.classLoaders.firstOrNull { it.findClass(className, true) != null }
|
||||
}.getOrNull()
|
||||
}
|
||||
}
|
||||
|
@ -11,10 +11,12 @@
|
||||
|
||||
package net.mamoe.mirai.console.plugin.jvm
|
||||
|
||||
import io.github.karlatemp.caller.CallerFinder
|
||||
import io.github.karlatemp.caller.StackFrame
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.mamoe.mirai.console.compiler.common.ResolveContext
|
||||
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.*
|
||||
import net.mamoe.mirai.console.internal.util.getCallerClassloader
|
||||
import net.mamoe.mirai.console.internal.util.findLoader
|
||||
import net.mamoe.mirai.console.plugin.description.PluginDependency
|
||||
import net.mamoe.mirai.console.plugin.description.PluginDescription
|
||||
import net.mamoe.mirai.console.util.SemVersion
|
||||
@ -65,13 +67,13 @@ public interface JvmPluginDescription : PluginDescription {
|
||||
* 从 [pluginClassloader] 读取资源文件 [filename] 并以 YAML 格式解析为 [SimpleJvmPluginDescription]
|
||||
*
|
||||
* @param filename [ClassLoader.getResourceAsStream] 的参数 `name`
|
||||
* @param pluginClassloader 默认通过 [Thread.getStackTrace] 获取调用方 [Class] 然后获取其 [Class.getClassLoader].
|
||||
* @param pluginClassloader 默认通过 [CallerFinder.getCaller] 获取调用方 [StackFrame] 然后获取其 [Class.getClassLoader].
|
||||
*/
|
||||
// @JvmOverloads // compiler error
|
||||
@JvmStatic
|
||||
public fun loadFromResource(
|
||||
filename: String = "plugin.yml",
|
||||
pluginClassloader: ClassLoader = getCallerClassloader() ?: error("Cannot find caller classloader, please specify manually."),
|
||||
pluginClassloader: ClassLoader = CallerFinder.getCaller()?.findLoader() ?: error("Cannot find caller classloader, please specify manually."),
|
||||
): JvmPluginDescription {
|
||||
val stream = pluginClassloader.getResourceAsStream(filename) ?: error("Cannot find plugin description resource '$filename'")
|
||||
|
||||
|
@ -26,6 +26,7 @@ allprojects {
|
||||
maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
maven(url = "https://dl.bintray.com/karlatemp/misc")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,3 +63,6 @@ const val `mirai-core-api` = "net.mamoe:mirai-core-api:${Versions.core}"
|
||||
const val yamlkt = "net.mamoe.yamlkt:yamlkt:${Versions.yamlkt}"
|
||||
|
||||
const val `jetbrains-annotations` = "org.jetbrains:annotations:19.0.0"
|
||||
|
||||
|
||||
const val `caller-finder` = "io.github.karlatemp:caller:1.0.1"
|
||||
|
Loading…
Reference in New Issue
Block a user