什么是invokedynamic,我该如何使用它?

2022-01-16 00:00:00 jvm reflection java invokedynamic

我不断听到有关添加到 JVM 的所有新酷特性,其中一个很酷的特性是 invokedynamic.我想知道它是什么以及它如何使 Java 中的反射式编程更容易或更好?

解决方案

这是一个新的 JVM 指令,它允许编译器生成调用具有比以前更宽松的规范的方法的代码——如果你知道鸭子打字" 是,invokedynamic 基本上允许鸭子打字.作为一个 Java 程序员,你可以做的事情并不多.但是,如果您是工具创建者,则可以使用它来构建更灵活、更高效的基于 JVM 的语言.这里是一篇非常精彩的博文,提供了很多细节.p>

I keep hearing about all the new cool features that are being added to the JVM and one of those cool features is invokedynamic. I would like to know what it is and how does it make reflective programming in Java easier or better?

解决方案

It is a new JVM instruction which allows a compiler to generate code which calls methods with a looser specification than was previously possible -- if you know what "duck typing" is, invokedynamic basically allows for duck typing. There's not too much you as a Java programmer can do with it; if you're a tool creator, though, you can use it to build more flexible, more efficient JVM-based languages. Here is a really sweet blog post that gives a lot of detail.

相关文章