如何在混合Java版本的世界中使用泛型? [英] How to use generics in a world of mixed Java versions?

查看:160
本文介绍了如何在混合Java版本的世界中使用泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢泛型,尽可能地使用它们。我偶尔需要在另一个项目中使用我的一个类,这个项目必须在旧的JVM上运行(5.0之前),需要在JavaME上运行(不能使用泛型)或者Microsoft J#(它非常差支持泛型)。



目前,我手动删除所有泛型,这意味着插入多个演员。



由于泛型被称为仅编译时,并且每个通用代码都可能自动转换为非泛型代码,所以我想知道是否有任何工具可以为我执行此操作。



如果没有这样的工具,我还能如何解决这个问题?我应该完全停止使用泛型吗?



已经有与 bytecode compability 有关的答案。如果我需要源代码兼容性

一个href =http://retroweaver.sourceforge.net/ =nofollow noreferrer> Retroweaver 为了实现这种事情。在这个问题上的其他答案有点误导。泛型是与以前版本兼容的字节码类型,但并不完全(如果您不相信我,请参阅 java.lang.reflect.Type )。此外,还有一个字节码版本属性的问题,它会阻止在1.5之前编译的类在以前的版本上运行。 Retroweaver可以解决这两个问题,同时还可以启用注释和枚举等其他Java 5功能。


I like generics a lot and use them whereever I can. Every now and then I need to use one of my classes in another project which has to run on an old JVM (before 5.0), needs to run on JavaME (where generics are not allowed neither) or in Microsoft J# (which has VERY poor Support for generics).

At the moment, I remove all generics manually, which means inserting many casts as well.

Since generics are said to be compile-time-only, and every piece of generic code could possibly converted to non-generic code automatically, I wonder if there is any tool which can do this for me.

If there is no such tool, how else could I solve the problem? Should I completely stop using generics?

There already are answers related to bytecode compability. What if I need source code compability for some reason?

解决方案

You need to use something like Retroweaver in order to achieve this sort of thing. The other answers on this question are slightly misleading. Generics are sort-of bytecode compatible with previous versions, but not entirely (see java.lang.reflect.Type if you don't believe me). Also, there is an issue of the bytecode version attribute, which will prevent a class compiled against 1.5 from running on a previous version. Retroweaver works around both problems while also enabling other Java 5 features like annotations and enums.

这篇关于如何在混合Java版本的世界中使用泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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