批量重构使 Java 方法参数成为最终的

发布于 2024-12-12 07:41:07 字数 117 浏览 0 评论 0原文

我正在寻找一种对完整的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆消瘦 2024-12-19 07:41:07

您可以在 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.

[旋木] 2024-12-19 07:41:07

正如 Peter Lawrey 所建议的,IntelliJ 就是这样做的。

分析->检查代码->自定义配置文件

在“代码样式问题”部分中,您可以看到:

字段可能是最终的

This inspection reports any fields which may safely be made final. A static field may 
be final if it is initialized in its declaration or in one static class initializer, but 
not both. A non-static field may be final if it is initialized in its declaration or in 
one non-static class initializer or in all constructors.
Powered by InspectionGadgets

局部变量或参数可以是最终的

This inspection reports parameters or local variables, found in the specified inspection
scope, that may have a final modifier added.

Use check boxes in the inspection options below, to define whether parameters or local
variables (or both) are to be reported.

这可能只会使最终的变量可以安全地如此,但您试图发现的那些将仍然是非最终的。尽管如此,这仍然是发现它们的一种方法。

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

This inspection reports any fields which may safely be made final. A static field may 
be final if it is initialized in its declaration or in one static class initializer, but 
not both. A non-static field may be final if it is initialized in its declaration or in 
one non-static class initializer or in all constructors.
Powered by InspectionGadgets

Local variable or parameter can be final

This inspection reports parameters or local variables, found in the specified inspection
scope, that may have a final modifier added.

Use check boxes in the inspection options below, to define whether parameters or local
variables (or both) are to be reported.

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.

原谅过去的我 2024-12-19 07:41:07

我不知道 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文