kosi-libs / Kodein

Painless Kotlin Dependency Injection
https://kosi-libs.org/kodein
MIT License
3.21k stars 174 forks source link

IllegalAccessError with kotlin 1.5.0 #375

Closed ghost closed 3 years ago

ghost commented 3 years ago

Faced a problem when updating to kotlin 1.5.0, example below

Steps:

  1. Download example project from https://github.com/zglrx99/test-illegal-access
  2. Run main function in module second

Result:

Exception in thread "main" java.lang.IllegalAccessError: class com.test.second.Dummy tried to access field com.test.first.CustomFactoryKt.$$delegatedProperties (com.test.second.Dummy and com.test.first.CustomFactoryKt are in unnamed module of loader 'app') at com.test.second.Dummy.(Dummy.kt:9) at com.test.second.MainKt.main(Main.kt:4) at com.test.second.MainKt.main(Main.kt)

Expect:

No errors

That code will be work properly, if you downgrade plugin version of kotlin - https://github.com/zglrx99/test-illegal-access/blob/master/build.gradle.kts plugins { kotlin("jvm") version "1.4.32" }

berezovik-rs commented 3 years ago

Hi, have same issue.

Working solution for me:

inline fun <reified T> createCustom(di: DI = customDi): T {
  return doCreateCustom(di, T::class)
}

@Suppress("UNCHECKED_CAST")
fun <T: Any> doCreateCustom(di: DI, clazz: KClass<*>): T {
  val something by di.Instance(erased(clazz))

  return something as T
}
romainbsl commented 3 years ago

Hi.

This seems to be a Kotlin issue with the new JVM IR backend. In fact, you said it yourself, if you use Kodein-DI 7.6.0 built with kotlin 1.5, on a kotlin 1.4.32 project this is working fine. However, if you enable the JVM IR backend on your project with kotlin 1.4.32 you'll get the same error.

Using your repro project @zglrx99 here is my gradle config to get the error on Kotlin 1.4.32

plugins {
    kotlin("jvm") version "1.4.32"
}

allprojects {

    apply(plugin = "org.jetbrains.kotlin.jvm")

    group = "org.example"

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation(kotlin("stdlib"))
        api("org.kodein.di:kodein-di-jvm:7.6.0")
    }

    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
        kotlinOptions.jvmTarget = "11"
        kotlinOptions.useIR = true
    }
}
version = "1.0-SNAPSHOT"

Also, you can drop an issue on Kotlin's issue tracker https://youtrack.jetbrains.com/issues.

ghost commented 3 years ago

Thank you for help, if you are interested, here new issue on youtrack - https://youtrack.jetbrains.com/issue/IDEA-272984

romainbsl commented 3 years ago

thanks. you should change the target of your issue, you've set it to IntelliJ IDEA m it should be Kotlin.

ghost commented 3 years ago

yea, i tried, but after creation it has restriction of target modification

ghost commented 3 years ago

moderator changed target, here new link - https://youtrack.jetbrains.com/issue/KTIJ-19026