-->

反向移植的Java 5/6功能到Java 1.4吗?(Backport Java 5/6 featu

2019-06-23 13:43发布

我们坚持与Java2SE V1.4,直到2010年底。这真是讨厌,但我们一点办法也没有。 做什么选择,我们必须使用一些新功能已经现在呢? 我能想到的几种方法一样

  • 改变的字节码,例如使用Retrotranslator或的Retroweaver 。

  • 图书馆的反向移植,如并发反向移植 ,但这并不能帮助仿制药。

  • 的Java 5个的特性仿真,例如检查集合,可变参数与辅助方法,等等。

  • 通过预编译改变源代码,用剥离所有1.5的东西最后汇编,如前Declawer可以做到这一点。

我使用WebLogic和“真实”的东西,我最感兴趣的是在生产环境中使用它非常积极的经验。

Answer 1:

Thanks for your answers. Here is the summary of all relevant answers and my own research.

Changing the bytecode: The Retros
This is done by the "retro"-tools: Retrotranslator, Retroweaver and JBossRetro. Retrotranslator seems to be the most mature and active of them tool. These tools scan all classes and change the bytecode to remove Java 5 and 6 features. Many Java5 features are supported, some by using 3rd party backport libraries. This option is most popular and there is some positive feedback from users. Experiments showed that it's working as expected. See a short overview on developerworks.

Pro: You can develop entirely in Java 5, build modules and all kind of JARs. In the end you just transform all classes to Java 1.4 and package your EAR. This is easily done with Retrotranslator's Maven integration (org.codehaus.mojo:retrotranslator-maven-plugin).

Con: Conservative environments do not allow changed bytecode to be deployed. The result of the retro-step is not visible to any coder and can't be approved. The second problem is fear: There might be some cryptic production problem and the retro-code is another step that might be blamed for that. App-server vendors might refuse help due to changed bytecode. So nobody wants to take responsibility to use it in production. As this is rather a policital than a technical problem, so I see no solution. It has happened to us, so I was looking for further options :-(

Compiling Java5 to Java 1.4: jsr14
There is an unsupported option, javac -source 1.5 and -target jsr14 which compiles the Java5 source to valid Java 1.4 bytecode. Most features like varargs or extended for loop are translated by the compiler anyway. Generics and annotations are stripped. Enums are not supported and I don't know about autoboxing, as the valueOf methods were mostly introduced in Java5.

Con: Only byte code is translated, library usage is not changed. So you have to be careful not to use Java5 specific APIs (but could use Backports). Further you have to build all modules at the same time, because for development time you propably want Java5 code with generic and annotation information. So you have to build the entire project from scratch for Java 1.4 production.

Changing Source back to Java 1.4: Declawer
As answered in a related question, there is Declawer, a compiler extension, that works for generics and varargs, but not for enhanced for loop or autoboxing. The generated source "is a little funky, but not too bad".

Pro: The generated source is available and can be reviewed. In worst case fixes can be made in this source. There is no "magic", because the source is valid Java. Some people even use JAD (Java decompiler) to get the Java 1.4 source again. The output of Jad readable is readable if you compile with debug information and don't use inner classes.

Con: Similar to -target jsr14, you need an extra step in the deployment. Same problems with libraries, too.

Changing Source back to Java 1.4: by hand
Several answers suggested doing it by hand. For an automatic, repeating build process this is of course not useful, but for one-time changes it's reasonable. Just automate what is possible. Maybe look at Antlr for creating a home-grown conversion tool.

Backported Libraries:
The problem is, that Java5 also ships new libraries, that are not available in older JREs, see related question. Fortunately there are several backported libraries that give you some functionality of Java5, but can't simulate language features, like generics.

  • 注释 ,在所讨论的TSS
  • 同时
  • com.sun.net.httpserver (Java 6中至5)
  • GIF写作 (Java 6中5)
  • 开始你自己的反向移植项目;-)
  • 你可能会复制你从JDK或其他库需要的类,但最有可能它们与其他类。

仿效的Java 1.4代码Java5的特点:
我在想一些事情你可以做,使您的生活更轻松,还与Java 1.4停留。 最重要的特点是类型安全的集合,这里有一些想法:

  • 而不是使用泛型的,你可以用一些模板创建自己的类型安全的容器。
  • 添加一个类型安全的迭代器(这是没有任何的Iterator更多)。
  • 添加asList方法,其允许1,2,...,n的参数和它们的阵列(模拟可变参数)。
  • 对于可变参数(转换方法1,...,n参数数组)和valueOf可以把一些辅助类。


Answer 2:

源代码预编译,最后编译和部署之前剥离所有1.5的东西。 有没有能做到这一点的任何工具?

是。 他们被称为Retrotranslator或的Retroweaver。 除了泛型(只对编译器的缘故存在这样),你不能简单地“剥夺1.5的东西”。 枚举(也许还有其他一些功能)必须与功能相当的代码来代替。 这正是这些工具做。



Answer 3:

您可以使用JDK 1.5特性的代码在编译时目标JDK 1.4。 查看可用的javac的选项。 然而,大多数图书馆现在正在使用JDK 1.5的代码,所以你会与旧库被卡住。



Answer 4:

值得指出的是,虽然Java 1.4中已经停产一段时间。 Java 5.0中会EOL 10月8日,2009年如果有人在2010年看好你的Java 5.0,我会问为什么?



Answer 5:

为了模拟在Java 1.4中的注释,你可以使用http://xdoclet.sourceforge.net/xdoclet/index.html



文章来源: Backport Java 5/6 features to Java 1.4?