简体   繁体   English

如何设置 IntelliJ IDEA 14 以尽可能添加“final”关键字?

[英]How to setup IntelliJ IDEA 14 to add “final” keyword where possible?

I would like to setup my IntelliJ IDEA 14 for automatic final keyword adding in all possible places (parameters, fields, etc.) to the existing Java class.我想设置我的 IntelliJ IDEA 14,以便在所有可能的位置(参数、字段等)自动将final关键字添加到现有的 Java 类中。 I found some solutions with macros and shortcuts but I would like setup IntelliJ to add the final keyword when it saves the file.我找到了一些带有宏和快捷方式的解决方案,但我想设置 IntelliJ 在保存文件时添加final关键字。 Does anyone know a good solution for this?有谁知道一个好的解决方案? Maybe can you recommend some plugin or combination of plugins?也许您可以推荐一些插件或插件组合?

Open the "Configure inspections" view (click on the person in the hat icon).打开“配置检查”视图(单击帽子图标中的人)。 Search on "final" and you'll find various options in the code style section that you can enable to enable highlighting of candidates for final .搜索“final”,您将在代码样式部分找到各种选项,您可以启用这些选项以突出显示final的候选。 You can then use alt-return to apply the suggestion within a given file.然后,您可以使用 alt-return 在给定文件中应用建议。

I wouldn't recommend you do it automatically on saving!我不建议您在保存时自动执行此操作!

Preferences - Code style issues首选项 - 代码风格问题

Since the 2018.3 version we can enable the checkbox:2018.3版本开始,我们可以启用复选框:

Preferences/Settings > Editor > Inspections > Java > Code style issues > Local variable or parameter can be final

Then perform the reformat code action.然后执行重新格式化代码操作。

If the Code cleanup checkbox is enabled then IDEA will automatically add final where possible.如果启用了代码清理复选框,则 IDEA 将在可能的情况下自动添加 final。

IntelliJ 2021.1 > Preferences/Settings > Editor > Inspections > Java > Code style issues > Local variable or parameter can be final 的截图

Use Save Actions plugin: https://plugins.jetbrains.com/plugin/7642-save-actions使用Save Actions插件: https : //plugins.jetbrains.com/plugin/7642-save-actions

You can enable adding of final keywords in:您可以在以下位置启用添加final关键字:

Settings -> Other Settings -> Save Actions -> Add final to field/local variable`.设置 -> 其他设置 -> 保存操作 -> 将最终添加到字段/局部变量`。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM