为什么Kotlin字节码引用java.util.function.BiConsumer? [英] Why does Kotlin byte code reference java.util.function.BiConsumer?

查看:260
本文介绍了为什么Kotlin字节码引用java.util.function.BiConsumer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,Kotlin应该可以使用JRE 6运行.但是,由于引用Java 8类(java/util/function/BiConsumer),此代码在地图上带有foreach的操作失败了

From what I understand Kotlin should be able to run using JRE 6. But this code with a foreach on a map fails because of a reference to a Java 8 class (java/util/function/BiConsumer)

CompilerTest.kt:

CompilerTest.kt:

fun main(args: Array<String>) {
  val aMap = mapOf("bar" to "bat")
  aMap.forEach { k, v -> println("$k -> $v")}
}

编译Kotlin代码:

Compile the Kotlin code:

» kotlinc CompilerTest.kt -jvm-target 1.6 -include-runtime -d compilerTest.jar

在JRE 6上测试编译后的代码:

Testing the compiled code on JRE 6:

» docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp openjdk:6-jdk-slim java -jar compilerTest.jar
Exception in thread "main" java.lang.NoClassDefFoundError: java/util/function/BiConsumer
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    at CompilerTestKt.main(CompilerTest.kt:5)
Caused by: java.lang.ClassNotFoundException: java.util.function.BiConsumer
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    ... 12 more

版本信息:

» kotlinc -version         
info: kotlinc-jvm 1.1.4-3 (JRE 1.8.0_131-b11)

推荐答案

结果证明kotlin Map继承自java.util.Map.调用aMap.forEach { k, v -> println("$k -> $v")}时,就是在调用

Turns out that kotlin Map inherits from java.util.Map. When you call aMap.forEach { k, v -> println("$k -> $v")}, you are calling the Java version of the method.

但是,如果您更改代码以输入:aMap.forEach { entry -> println("$entry.key -> $entry.value")},则您正在调用

However, if you change the code to take an entry: aMap.forEach { entry -> println("$entry.key -> $entry.value")}, you are calling the Kotlin version and the code will run on JRE6.

您可以通过使用-no-jdk标志编译到编译器来标记对JRE的依赖关系.

You can flag the dependency on the JRE by compiling with the -no-jdk flag to the compiler.

» kotlinc CompilerTest.kt -no-jdk -jvm-target 1.6 -include-runtime -d compilerTest.jar
CompilerTest.kt:5:10: error: type inference failed: inline fun <K, V> Map<out K, V>.forEach(action: (Map.Entry<K, V>) -> Unit): Unit
cannot be applied to
receiver: Map<String, String>  arguments: ((Map.Entry<String, String>, ???) -> Unit)

    aMap.forEach { k, v -> println("$k -> $v")}
         ^
...

这篇关于为什么Kotlin字节码引用java.util.function.BiConsumer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆