批量重构使 Java 方法参数成为最终的
我正在寻找一种对完整的 Java 应用程序执行批量重构的方法。也就是说,如果情况还不是这样,则将方法参数设为最终的。
这里有人知道这样的工具吗?或者解析 Java 源代码并可以通过此类更改进行扩展的东西。
I'm looking around for a way to perform batch refactoring on a complete Java application. In this namely make method arguments final where it's not the case yet.
Does somebody in here knows about such a tool ? Or something that parses Java source and that can be extended with such changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 IntelliJ 中进行批量更改,以将最终的每个字段、局部变量或参数更改为最终的。
使用该选项进行代码分析,并全局“应用修复”,确保它仍然可以编译,因为在奇怪的情况下它并不总是 100% 正确。
You can do a bulk change in IntelliJ to change every field, local variable or parameter which can be final to be final.
Do a Code Analysis with the option, and "Apply Fix" globally, make sure it still compiles as it doesn't always get it 100% right in odd cases.
正如 Peter Lawrey 所建议的,IntelliJ 就是这样做的。
分析->检查代码->自定义配置文件
在“代码样式问题”部分中,您可以看到:
字段可能是最终的
局部变量或参数可以是最终的
这可能只会使最终的变量可以安全地如此,但您试图发现的那些将仍然是非最终的。尽管如此,这仍然是发现它们的一种方法。
As Peter Lawrey suggests, IntelliJ does that.
Analyze -> Inspect code -> custom profile
There, in the "Code style issues" section, you have:
Field may be final
Local variable or parameter can be final
That will propably only make final the variable that can safely be so but the ones that you're trying to spot will remain non-final. Still, it's a way to spot them.
我不知道 Eclipse 或 NetBeans 中有任何此类重构。但是一个像样的正则表达式替换就可以解决问题。为了确保您不会意外地在不应该发生的地方执行它,您可能需要手动确认每个替换。但是,如果您有数百个类,这可能不太可行。在这种情况下,在各处进行替换,然后检查与旧版本的差异可能会很有用。
如果任何参数因为被覆盖而未最终确定,那么它会在编译过程中变得清晰。
I don't know of any such refactoring in Eclipse or NetBeans. But a decent regular expression replace would do the trick. In order to make sure you don't accidentally perform it in places where it shouldn't happen, you might want to confirm each replace manually. This might not really be feasible if you have hundreds of classes, though. In that case, doing the replace everywhere and then checking a diff with the older version could be of use.
Should any argument not be made final because it is overwritten, it'll become clear during compilation.