如何使用重构从类中删除泛型类型
我有这个类:
public class TimeIntCo<T> extends TimeValueCo<Integer>
{
}
我决定不再需要 T 类型,并且不在类中使用它(我知道它始终是 Integer)。
但是,我有很多类似于 TimeIntCo
的类的参考。
有没有办法使用 Eclipse 重构来删除 T
类型而不导致引用中出现任何错误?
我的意思是一步完成,而不是查找和替换。
编辑:更清楚 - 如果我只是从类中删除 T
我有大约 10,000 个错误,我不想手动执行此操作,而且我不想使用查找和替换,因为我认为重构更安全。
I have this class:
public class TimeIntCo<T> extends TimeValueCo<Integer>
{
}
I decided that the T type is not needed anymore, and it is not used in the class (I know it is always Integer).
However, I have a lot of references for the class that looks like TimeIntCo<Integer>
.
Is there a way using Eclipse refactoring to remove the T
type without causing any error in references?
I mean to do it in one step and not find&replace.
EDIT: to be more clear - if I just remove T
from class I have about 10,000 errors, I don't want to do it manually, and I prefer not to use find&replace because I consider refactor safer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
引入一个超类
并更改 TimeIntCo 来扩展它。
然后使用Eclipse的Refactor->尽可能使用超类型
Introduce a superclass
and change
TimeIntCo
to extend it.Then use Eclipse's Refactor -> Use Supertype Where Possible
只需进行文本查找/替换
TimeIntCo
->所有 java 类上的TimeIntCo
。Just do a textual find/replace
TimeIntCo<Integer>
->TimeIntCo
on all java classes.我怀疑是否有对此进行重构。只需删除
,保存文件,然后检查并修复可能导致的任何编译错误。 (如果不使用T
类型参数,则不应该有任何需要纠正的编译错误...)@ohadshai 评论:
我敢打赌,大多数错误都可以通过更改少量子类声明来修复。
不,我不。重构通常用于保留原始源代码含义的简单转换。这种转变不太可能做到这一点。
I doubt that there is a refactor for this. Just delete the
<T>
, save the file, and go through and fix any compilation errors that might result. (If theT
type parameter is not used, there shouldn't be any compilation errors to correct ...)@ohadshai comments:
I bet that most of them can be fixed by changing a small number of subclass declarations.
No I don't. Refactors are generally used for simple transformations that preserve the meaning of the original source code. This transformation is unlikely to do that.
由于内置IDE重构无法处理你想要的,那么
如果您厌倦了手动查找/替换,这将起作用
它会起作用,因为它会删除任何
TimeIntCo
或TimeIntCo
但它赢了不修复TimeIntCo
内的T someMethod(T t)
声明只是提出一个替代方案,而且您可能没有使用类似 unix 的环境或 Bash,所以忽略它。
As the buildin IDE refactoring can't handle what you want, then
if you're bored of the manual find/replace, this will work
It will work as in it will remove any
TimeIntCo<T>
orTimeIntCo<Integer>
but it won't fix your decrlarations ofT someMethod(T t)
insideTimeIntCo
Just proposing an alternative, also you may not be using a unix like environment or Bash, so just ignore this.
如果 T 没有给您带来任何好处,只需将其从类声明中删除即可:
它可以安全地保留在父类
TimeValueCo
中。if T isn't providing you any benefit, just drop it from the class declaration:
It can safely remain in the parent class
TimeValueCo
.在 Eclipse 中使用正则表达式搜索和替换(搜索 | 文件):
搜索:
SomeClass\<.*\>
并替换为:
SomeClass
。根据需要对子类重复此操作。
Use regular expression search and replace in Eclipse (Search | File):
search for:
SomeClass\<.*\>
and replace with:
SomeClass
.Repeat for subclassses as necessary.