有没有一种方法/工具可以将 Java 源代码从使用原始类型自动转换为使用泛型类型?
我有一些遗留代码,其中包含 677 个对原始类型的引用:
ArrayList 47
Vector 420
Hashtable 61
Enumeration 64
Class 7
Iterator 78
TOTAL 677
现在我可以手动查看代码以推断泛型类型并进行替换,但这将花费很长时间。
最佳答案
Eclipse 确实有一个“推断泛型类型”重构工具(我不知道它是否会一次处理多个类,但很可能)。
Replaces raw type occurrences of generic types by parameterized types after identifying all places where this replacement is possible. Available: Projects, packages, and types Options: 'Assume clone() returns an instance of the receiver type'. Well-behaved classes generally respect this rule, but if you know that your code violates it, uncheck the box.
'Leave unconstrained type arguments raw (rather than inferring )'. If there are no constraints on the elements of e.g. ArrayList a, uncheck this box will cause Eclipse to still provide a wildcard parameter, replacing the reference with ArrayList.
关于java - 自动将 Java 源代码转换为使用泛型而不是原始类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2561072/