将Java 7编译为Java 6 [英] Compiling Java 7 to Java 6
问题描述
我知道Java 6不能使用Java 7的运行时功能,但是由于没有添加新的字节码,因此新的字节码invokedynamic
仅与非Java相关.语言,我想知道将Java 7源代码(新的switch
语句,diamond运算符)转换为纯Java 6有多困难(即能够在不失去Java 6兼容性的情况下开始将源转换为Java 7)
有指针吗?
将Java 7 javac输出的.class文件标记为1.6.0版(即0x32)
printf "\x00\x00\x00\x32" |dd of=Example.class seek=4 bs=1 count=4 conv=notrunc
(根据 http://en.wikipedia.org/wiki/Java_class_file#General_layout )>
如果将其(使用文件名使用$ 1)放入j6patch
中,则可以使用以下方法来处理所有类文件:
find . -name \*.class |xargs -I {} ./j6patch {}
我在大型(〜4.8 MB jar)代码库上使用了此代码,甚至在Java 6 jar上使用了RetroTranslator
,因此Java 7语言功能可以在以Java 5运行的应用程序上使用.Java 7编译器(javac
)进行了很多额外的优化(例如转义分析),从而显着提高了性能.
将RetroTranslator
与-verify -target 1.5
和JRE 1.6运行时jar一起使用,可以验证是否未使用Java 7运行时功能.
I'm aware that the runtime features of Java 7 are not available with Java 6 but since no new byte code has been added the new byte code invokedynamic
is only relevant for non-Java languages, I was wondering how hard it would be to convert Java 7 source code (new switch
statement, diamond operator) to pure Java 6 (i.e. to be able to start to convert the source to Java 7 without losing Java 6 compatibility).
Any pointers?
Mark a .class file output by Java 7 javac with version 1.6.0 (i.e. 0x32)
printf "\x00\x00\x00\x32" |dd of=Example.class seek=4 bs=1 count=4 conv=notrunc
(according to http://en.wikipedia.org/wiki/Java_class_file#General_layout)
If you put that (using $1 for the filename) into j6patch
you can do all class files with:
find . -name \*.class |xargs -I {} ./j6patch {}
I used this on a large (~4.8 MB jar) code base and even used RetroTranslator
on the java 6 jar so Java 7 language features can be used on an app that runs in Java 5. Also the Java 7 compiler (javac
) does lots of extra optimizations (e.g. escape analysis) that very noticeably improves performance.
Using RetroTranslator
with -verify -target 1.5
and JRE 1.6 runtime jars allows to verify that no Java 7 runtime features are used.
这篇关于将Java 7编译为Java 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!