t3wad
08/01/2023, 5:44 PMjava.lang.NoSuchMethodError: MyInterfaceWrapperOfJavaInterface.withJavaDefaultInterfaceMethod-impl(LMyJavaInterface;Ljava/lang/String;)Ljava/lang/String
If the interface is created as a kotlin interface, there isn't a problem. However, I ran into this working with an existing Java library (playwright), which has an existing interface I am trying to use.
Here's a small repo showing the issue (see IssueMain.kt#main()): https://github.com/twadzins/k2-issue-delegate-value-class
Looking at the decompiled code diff in the screenshot (left is non k2, right is k2), it seems that the non k2 is using box-impl(), but the k2 version does not. In both cases, the decompiled value class does indeed not contain the withJavaDefaultInterfaceMethod
method.
I see that I can workaround it if, in the value class body, I override the "missing" default method and have it just call super.withJavaDefaultInterfaceMethod()
IssueMain.kt:
/**
* From <https://kotlinlang.org/docs/inline-classes.html#inline-classes-and-delegation>
*/
@JvmInline
value class MyInterfaceWrapperOfJavaInterface(val m: MyJavaInterface) : MyJavaInterface by m
fun main() {
val myJava = MyInterfaceWrapperOfJavaInterface(MyJavaInterfaceImpl())
println(myJava.withJavaDefaultInterfaceMethod("Worked with one arg (with default interface)")) // works on non K2, fails with K2
}
Java interface: MyJavaInterface.java:
public interface MyJavaInterface {
String withJavaDefaultInterfaceMethod(String value, String otherString);
default String withJavaDefaultInterfaceMethod(String value) {
return withJavaDefaultInterfaceMethod(value, null);
}
...
Here's a log of it working without k2 and failing with k2:
% ./gradlew run -Pkotlin.experimental.tryK2=false
> Task :run
Worked with one arg (with default interface)
BUILD SUCCESSFUL in 1s
3 actionable tasks: 2 executed, 1 up-to-date
% ./gradlew run -Pkotlin.experimental.tryK2=true
> Configure project :
ATTENTION: 'kotlin.experimental.tryK2' is an experimental option enabled in the project for trying out the new Kotlin K2 compiler only.
Please refrain from using it in production code and provide feedback to the Kotlin team for any issues encountered via <https://kotl.in/issue>
> Task :compileKotlin
w: Language version 2.0 is experimental, there are no backwards compatibility guarantees for new language and library features
> Task :run FAILED
Exception in thread "main" java.lang.NoSuchMethodError: MyInterfaceWrapperOfJavaInterface.withJavaDefaultInterfaceMethod-impl(LMyJavaInterface;Ljava/lang/String;)Ljava/lang/String;
at IssueMainKt.main(IssueMain.kt:13)
at IssueMainKt.main(IssueMain.kt)
##### 'kotlin.experimental.tryK2' results (Kotlin/Native not checked) #####
:compileKotlin: 2.0 language version
##### 100% (1/1) tasks have been compiled with Kotlin 2.0 #####
FAILURE: Build failed with an exception.
dmitriy.novozhilov
08/01/2023, 9:09 PMt3wad
08/01/2023, 9:10 PMt3wad
08/01/2023, 9:23 PM