Eclipse 的 Checkstyle 插件

发布于 2024-09-13 10:56:46 字数 114 浏览 4 评论 0原文

Eclipse 有 checkstyle 插件,它可以指出不好的编码实践。是否有某种工具或某种方式可以自动修复其中一些问题?
例如,假设清理未使用的导入。该工具是否可以自动访问整个项目并删除未使用的导入?

Eclipse has checkstyle plugin which points out the coding practices which are not good. Is there some tool or someway in which some of those issues can be automatically fixed?
For example, let's say cleanup unused imports. If the tool can automatically visit the whole project and remove the unused imports?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

冷清清 2024-09-20 10:56:46

从 3.3 版开始,Eclipse 就有了一个清理向导,可以帮助完成部分工作。但在我看来,这个问题是以错误的方式提出的:

  • Checkstyle 确实检查了很多不同的东西,其中一些可以自动更正。因此,通过使用 Eclipse 的自动格式,并调整其符合 Checkstyle 的格式,这些警告将自动消失。
  • 一些警告表明缺少一些未知的东西。最突出的是这里到处都是评论,但也可能有其他警告。 Eclipse 在这里可能会提供部分帮助,但通过 Eclipse 使用其标准模板生成 JavaDoc 并不是一个好主意,因为这样,出现错误的指示就消失了,但原因还存在。
  • 有些警告表明可能存在多种解决方案。所以Eclipse不可能自动更改代码。有时存在可能合适的快速修复,但每个修复都必须由用户选择。

因此,您应该执行以下操作:

  1. 查看 Eclipse 中的清理向导 Window >首选项>爪哇>代码风格>清理,从现有的配置文件中创建一个新的配置文件,并将其调整为您想要在 Checkstyle 中使用的规则。
  2. 如果您选择了代码组织>;在您的配置文件中格式化源代码,同时调整 Java 的代码格式化程序。转到窗口>首选项>爪哇>代码风格> Formatter,在这里也创建一个新的配置文件,并根据您的检查样式规则对其进行调整。经验法则应该是,在编写任何代码并使用格式化程序对其进行格式化后,不应存在只能通过插入或删除空格、空格、换行符和括号来纠正的检查样式警告。
  3. 确保项目中的每个人都使用此配置,当然每个人都应该使用 Eclipse。

Eclipse has since the version 3.3 a cleanup wizard that could help doing part of the job. But in my opinion, the question is asked in the wrong way:

  • Checkstyle does check a lot of different things, and some of them could be automatically corrected. So by using the automatic format of Eclipse, and tweaking the format that it complies to Checkstyle, these warnings will go away automatically.
  • Some of the warnings indicate that something is missing which is not known. Most prominent are here comments everywhere, but there could be other warnings as well. Eclipse may here help partially, but it is not a good idea to generate JavaDoc by eclipse with its standard templates, because then the indication that something is wrong is gone, but the cause exists further.
  • Some of the warnings indicate something for which multiple solutions could exist. So it is not possible for Eclipse to automatically change the code. There sometimes a quick fix exists that may be appropriate, but each has to be selected by the user.

So you should do the following:

  1. Have a look at the Cleanup Wizard in Eclipse Window > Preferences > Java > Code Style > Clean Up, create there a new profile from an existing one, and tweak it to the rules you want to use in Checkstyle.
  2. If you have selected Code Organizing > Format Source Code in your profile, tweak the code formatter for Java as well. Go to Window > Preferences > Java > Code Style > Formatter, create here as well a new profile as well, and tweak it according to your checkstyle rules. The rule of thumb should be that after writing any code and formatting it with the formatter, no checkstyle warning should exist that could be corrected by only inserting or removing spaces, blanks, newlines, and parantheses.
  3. Ensure that this configuration is used by everyone in your project, and of course everyone should use Eclipse.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文