0

I have simple Kotlin code in an existing Java project

class A(val p: Int)

fun main() {
    println("Hello World")
    println(A::javaClass)
    println(A::p)
}

However, this throws an exception

Exception in thread "main" java.lang.NoSuchMethodError: 'void kotlin.jvm.internal.PropertyReference1Impl.<init>(java.lang.Class, java.lang.String, java.lang.String, int)'
    at mloop.kt.graphql.TestKt$main$1.<init>(Test.kt)
    at mloop.kt.graphql.TestKt$main$1.<clinit>(Test.kt)
    at mloop.kt.graphql.TestKt.main(Test.kt:10)
    at mloop.kt.graphql.TestKt.main(Test.kt)

build.gradle.kts is also simple

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

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

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-reflect:1.7.20")
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "17"
}

Verified that kotlin-reflect is also listed in runtimeClassPath. However, the same code works in a Kotlin-only project.

compileClasspath - Compile classpath for compilation 'main' (target  (jvm)).
+--- org.slf4j:slf4j-api -> 2.0.3
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20
|    +--- org.jetbrains.kotlin:kotlin-stdlib:1.7.20
|    |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.7.20
+--- org.jetbrains.kotlin:kotlin-reflect:1.7.20
|    \--- org.jetbrains.kotlin:kotlin-stdlib:1.7.20 (*)
\--- org.projectlombok:lombok:1.18.24

runtimeClasspath - Runtime classpath of compilation 'main' (target  (jvm)).
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20
|    +--- org.jetbrains.kotlin:kotlin-stdlib:1.7.20
|    |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.7.20
+--- org.jetbrains.kotlin:kotlin-reflect:1.7.20
|    \--- org.jetbrains.kotlin:kotlin-stdlib:1.7.20 (*)
+--- org.jetbrains.kotlin:kotlin-reflect:{strictly 1.7.20} -> 1.7.20 (c)
+--- org.jetbrains.kotlin:kotlin-stdlib:{strictly 1.7.20} -> 1.7.20 (c)
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:{strictly 1.7.20} -> 1.7.20 (c)
+--- org.jetbrains.kotlin:kotlin-stdlib-common:{strictly 1.7.20} -> 1.7.20 (c)
Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
user1064504
  • 573
  • 6
  • 16

3 Answers3

1

Use ::class.java instead of ::javaClass.

The documentation isn't clear about this, but you don't need the kotlin-reflect library for basic property, function, and class references. You only need it for the deeper features like getting the class/property/function members or descriptors. Passing around KClasses, KProperties, and KFunction instances or invoking them doesn't require the library.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
0

This sounds more like a mismatch in the kotlin stdlib during runtime and a compiled class, not something related to reflection (but I'm not very familiar with reflection so I could be wrong).

It seems like the method with signature

PropertyReference1Impl(java.lang.Class, java.lang.String, java.lang.String, int)

Was added in kotlin 1.4, also see this for another example of the same error.

Not really sure where you kotlin < 1.4 stuff is coming from though, are you perhaps using an old gradle version (although I'm not even sure if that would matter)?

Please also add the full stacktrace to the question, instead of just 1 line, that should show what exactly is attempting to call the missing method.

somethingsomething
  • 1,746
  • 6
  • 8
0

After lot of dependancy scrutiny, I figured out com.newrelic.logging:logback:2.5.0 was breaking the java <-> kotlin reflection, upgrading to 2.6.0 fixed it.

user1064504
  • 573
  • 6
  • 16