遗留代码中的泛型
我们有相当多的代码刚刚跳转到 Java 5。我们一直在那些打算在 Java 5 版本中发布的组件中使用泛型,但是剩下的代码当然充满了原始的代码。类型。 我已将编译器设置为生成原始类型错误并开始手动清除它们,但按照目前的速度,需要非常很长时间才能完成它(大约有 >2500 错误)。 这就是 Eclipse 有用的 Infer Generic Type 快速修复,它总是可以消除错误,但通常会生成需要进一步工作的代码。
有没有更好的方法来处理这个问题? 有没有比 Eclipse 更好的自动化工具? 有什么方法可以将重构应用于所有事件,而不是逐一进行? 或者您只是忽略这些警告?
We've got a fairly large amount of code that just made the jump to Java 5. We've been using generics in those components targeted at being released in the Java 5 version, but the remaining code is, of course, full of raw types. I've set the compiler to generate an error for raw types and started manually clearing them, but at the present rate it'll take a very long time to go through with it (there are about 2500 errors). And that's with Eclipse's helpful Infer Generic Type quick fix, which always gets rid of the errors, but often generates code that needs further work.
Is there any better way to dealing with this? Are there any automated tools better than Eclipse? Any way to apply the refactoring to all occurences instead of doing them one-by-one? Or do you just ignore the warnings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议忽略这些警告。 否则,您将花费大量时间来更新遗留代码,而不会对其功能进行任何改进。
更新:来自Luke的精彩评论,我认为应该获得更多关注:
“泛型是在编译时捕获运行时错误的一种方法。除非此遗留代码中存在您认为与转换相关的错误,否则我不会管它(如果它没有损坏,请不要修复它) )”
I would suggest ignoring the warnings. Otherwise, you'll be putting a lot of time into updating the legacy code without making any improvements to its functionality.
Update: Great comment from Luke that I thought should get more visibility:
"Generics are a way to catch run time bugs at compile time. Unless this legacy code has bugs in it that you think are related to casting I would leave it alone (if it ain't broke, don't fix it)"
据我所知,你正在尽可能高效地处理这件事。 它是
显然并不完美,但你最终会完成。
不过,我建议您分阶段进行; 有可能的部分
那些比其他代码更能从中受益的代码,请重点关注这些代码。 尝试去
一次性完成这一切可能会带来给代码引入新错误的风险。 我们
有这样一个地方,我们有一个包含上下文相关的集合
数据,而泛型实际上无法用于它。
基本上,做你正在做的事情,但作为其他工作的一部分分阶段进行,
而不是试图一次性解决所有问题。
As far as I know, you're going about it as efficiently as possible. It's
obviously not perfect, but you'll finish eventually.
I recommend that you do it in stages, however; there are likely parts of the
code that would benefit more from this than others, focus on those. Trying to
do it all in one sweep runs the risk of introducing new bugs to your code. We
have one such place where we have a collection that holds context-dependent
data, and generics actually can't work for it.
Basically, do what you're doing, but do it in stages as part of other work,
instead of trying to fix it all in one throw.
面对类似的挑战,我们选择仅在因其他原因编辑的代码中升级到 Java 5 风格的泛型。 因此,如果 DoItFast.java 中有需要修复的错误,请更新 DoItFast.java 以使用 Java 5 风格的泛型。 定期编辑和更改的代码区域将快速更新。 这种行为几周或几个月后,您可以重新评估代码库的状况。
如果这对你来说还不够快地完成工作,有时我会利用午餐后的缓慢时间来漫不经心地清理一些课程并加快进程。
Faced with a similar challenge, we opted to upgrade to Java 5 style generics only in the code that was edited for another reason. So if you have a bug to fix in DoItFast.java, update DoItFast.java to use Java 5 style generics. The areas of the code that are being edited and changed regularly will get updated quickly. After a few weeks or months of this behavior, you can reevaluate the condition of your code base.
If that doesn't get the job done fast enough for you, sometimes I'll use the slow time after lunch to just mindlessly cleanup a few classes and speed the process along.
我认为没有必要更新所有旧代码。 也许您可以以某种方式确定旧代码的哪些部分经常使用,并且只更新这些部分以使用泛型类型? 或者也许您只能担心原始类型何时从公共函数返回? 很多这样的情况可能只是私有/局部变量,它们已经在没有泛型类型的情况下编写,并且可能工作得很好,所以可能不值得重写它们。
I don't think it is necessary to update all the old code. Maybe if you could somehow identify which parts of the old code are used frequently, and only update those to use generic types? Or maybe you could only worry when the raw type is returned from a public function? A lot of these cases are probably just private/local variables, which are already written without generic types and presumably work just fine, so it's probably not worth the effort to rewrite them.
据我了解,使用 IDEA 您可以选择所有类,弹出上下文菜单并选择 Refactor | 泛化。 任务完成。 使用通用代码(IMO)无疑是一种更愉快的体验。
I understand with IDEA you can select all your classes, popup the context menu and select Refactor | Generify. Job done. It's certainly a more enjoyable experience to work with generified code (IMO).