自动将大括号添加到 java 代码库中的所有 if/else/for/while 等

2022-01-17 00:00:00 formatting sonarqube java

我想减少大型遗留 Java 代码库中声纳违规的数量,似乎速赢"是将所有这些条件语句更新为带有大括号.这似乎是一件容易的事情,我不明白为什么它不容易自动化.

I want to reduce the number of sonar violations in a large legacy java code-base, and it seems a "quick win" would be to update all these conditional statements to have curly brackets. This seems like an easy thing to do and I can't see why it wouldn't be readily automatable.

有人知道可以执行这样的批量操作的工具吗?或者为什么在我花时间自己写东西之前做这样的事情可能是个坏主意?如果我自己写一个,最好的工具是什么?理想情况下是 Java 语言感知的东西,这样我就不必处理格式化极端情况等.

Does anybody know of a tool that could perform a bulk operation like this? Or why to do such a thing might be a bad idea before I go and spend the time writing something myself? If I were to write one myself what would be the best tools to use? Ideally something that is java language aware, so that I don't have to deal with formatting corner-cases and the like.

顺便说一句,这条规则是不可协商的,所以这确实是最好的方法.

The rule is non-negotiable by the way, so this really is the best approach.

推荐答案

最简单的方法是使用 Eclipse然后点击整个项目的Clean-up.在 Clean-up 配置文件中选择 Code style 选项卡.在那里你可以选择 Use blocks in if/while/for/do statements as Always.

The easiest thing would be to use Eclipse and click Clean-up on the whole project. In Clean-up profile configuration select Code style tab. There you can select Use blocks in if/while/for/do statements as Always.

相关文章