InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.93k stars 711 forks source link

Odd Reflection Method Behavior `object is not an instance of declaring class` #1950

Closed chrisjenx closed 2 weeks ago

chrisjenx commented 2 weeks ago

Describe the bug

using Koin get(klass) will fail Method.invoke(instance, params) on instances created by Koin.

Using something like MyClass::class.constructors.first().call() will work tho.

java.lang.IllegalArgumentException: object is not an instance of declaring class
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.checkReceiver(DirectMethodHandleAccessor.java:202)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:100)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)

How does Koin instantiate classes? I thought it used the provided constructors in the module factories?

To Reproduce

Call a method on Class using Method reflection using a instance created by Koin.get(klass).

Expected behavior I expected it to work the same way as ::class.constructors.first().call()

Koin module and version: koin-core:3.4.+ using 4.0.0-RC1, version doesn't seem to matter Kotlin 2.0.20

chrisjenx commented 2 weeks ago

Related to overloading class loader

https://github.com/InsertKoinIO/koin/issues/1363#issuecomment-1179536048

Would createAtStart fix this?

chrisjenx commented 2 weeks ago

Nevermind, was trying to use JobRunr, gave up will use something else that doesn't require reflection.