且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何用Java 11生成代码,但要针对Java 8及更高版本?

更新时间:2023-08-22 20:22:58

尽管从理论上讲,使用复杂的工具可以将为JDK 11编译的类转换为JDK 8,但这并非微不足道。二进制级别有重大变化。

While conversion of classes compiled for JDK 11 to JDK 8 would be theoretically possible with a sophisticated tool, it’s not trivial. There are significant changes on the binary level.

首先,JDK 11引入了嵌套类型,这样就无需在访问 private 时生成综合访问器方法内部/外部类的成员。当然,这种访问在旧版本中将失败。

First, JDK 11 introduced nest types, which eliminates the need to generate synthetic accessor methods when accessing private members of inner/outer classes. Of course, such access would fail in older versions.

它还引入了动态常量,尽管我不知道Java语言是否在任何地方都利用了该功能。

It also introduced dynamic constants, though I don’t know whether the Java language exploits that feature anywhere. This is mainly intended for future versions.

然后,由于JDK 9,使用 invokedynamic 引用 java Java 8中不存在的.lang.invoke.StringConcatFactory

Then, since JDK 9, string concatenation gets compiled using invokedynamic referring to java.lang.invoke.StringConcatFactory which is not present in Java 8.

一个可行的功能是 private 接口中的方法,作为语言功能在Java 9中引入,但已经在Java 8中以二进制级别进行了处理。

A feature that could work, is private methods in interfaces, introduced in Java 9 as a language feature, but already handled on the binary level in Java 8.

Java 8也将无法处理模块定义,但我想,它们将被忽略。

Java 8 would also be unable to process module definitions, but I suppose, they would be ignored.