I have 2 classes. I want to replace one, class A
so that ANY instance of it will actually behave like class B
.
class A {
fun test() = "a"
}
class B {
fun test() = "b"
}
I was trying to use the ClassLoader
to do this but I couldn't get the system to call MyClassLoader
.
class MyClassLoader() : ClassLoader() {
@Throws(ClassNotFoundException::class)
override fun loadClass(name: String): Class<*>? {
if (name == "com.myapp.A") {
return B::class.java
} else return super.loadClass(name)
}
}
This has to happen at compile time or runtime because I cannot manually replace every instance of A
for B
. (This is a minimalist example of the problem). A
cannot be removed, this is a requirement.
So how do I attach MyClassLoader
so that the system uses it?
I tried: Thread.currentThread().setContextClassLoader(MyClassLoader())
but the MyClassLoader
doesn't get called.