java - 自动将 Java 源代码转换为使用泛型而不是原始类型

标签 java generics

有没有一种方法/工具可以将 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/

相关文章:

java - 在 OnCreate 中定义按钮 - 无法解析方法中的符号

java - 在 Java 中使用泛型编程,就像这样 : class A<B>, 为什么我不能 new B 的对象

java - 具有组合多个角色的 Spring security 拦截 url

java - MessageSource bean 在自定义异常映射器类中注入(inject) null

java - 执行依赖于java代码的脚本os

java - NumberFormatException 在 jdk1.7 中将字符串转换为具有下划线、连字符的 int

java - 类型参数扩展和方法参数不匹配错误

java - 有界类型参数不能在需要父类的接口(interface)中使用

java - 如何调用不带参数的泛型方法?

c# - 通用扩展方法的编译器错误