Kotlin类重新加载伴随对象/函数

Aur*_*ieh 1 classloader kotlin

我正在尝试Kotlin类重装,但最近我遇到了一些接近这个问题:

package com.aurieh.reloading
fun doSomething(): String { // a function that does not belong to the class,
    // so it gets compiled as FileName$doSomething$...
}

class FileName {
    // do things with doSomething
}
Run Code Online (Sandbox Code Playgroud)

如果我重新加载这个类(使用ImplClassLoader.defineClassByteArray),并尝试调用内部调用doSomething的方法,我得到类似于的错误:

java.lang.IllegalAccessError: tried to access method com.aurieh.reloading.FileName.doSomething$default()Ljava/lang/String; from class com.aurieh.ares.reloading.FileName`
Run Code Online (Sandbox Code Playgroud)

我会解释这个,好像重新加载的类没有附加doSomething ..所以我的问题是,我将如何解决这个错误?通过某种方式将doSomething附加到重装类加载器?

作为参考,我的类重装代码:

class Reloader : ClassLoader() {
    fun load(name: String, bytes: ByteArray, offset: Int, len: Int): Class<*> {
        return defineClass("com.aurieh.reloading.$name", bytes, offset, len)
    }
}
Run Code Online (Sandbox Code Playgroud)

并加载:

val bytes = File("../classes/path/to/class/FileName.class").readBytes()
Reloader().load("FileName", bytes, 0, bytes.size).newInstance()
Run Code Online (Sandbox Code Playgroud)

hot*_*key 5

基本上,顶级函数不会编译到文件中定义的任何类中.而是为顶级成员创建一个单独的类:( FileNameKt如果文件已命名FileName.kt).

因此,为了使您的类正确加载(即没有任何不满意的链接),您必须首先加载FileNameKt该类:

val bytes1 = File("../classes/path/to/class/FileNameKt.class").readBytes()
val bytes2 = File("../classes/path/to/class/FileName.class").readBytes()
val reloader = Reloader()
reloader.load("FileNameKt", bytes1, 0, bytes1.size)
reloader.load("FileName", bytes2, 0, bytes2.size).newInstance()
Run Code Online (Sandbox Code Playgroud)