CallerFinder

This commit is contained in:
Karlatemp 2020-11-13 21:06:49 +08:00
parent 9a25ac78fa
commit c17762eaf5
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
5 changed files with 15 additions and 17 deletions

View File

@ -32,6 +32,7 @@ dependencies {
smartImplementation(yamlkt)
smartImplementation(`jetbrains-annotations`)
smartImplementation(`caller-finder`)
smartApi(`kotlinx-coroutines-jdk8`)
testApi(`mirai-core-qqandroid`)

View File

@ -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()
}
}

View File

@ -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'")

View File

@ -26,6 +26,7 @@ allprojects {
maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
jcenter()
mavenCentral()
maven(url = "https://dl.bintray.com/karlatemp/misc")
}
}

View File

@ -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"