Java Static Analysis Tools in Eclipse and IntelliJ IDEA – Eclipse和IntelliJ IDEA中的Java静态分析工具

最后修改: 2017年 7月 24日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In our introduction to FindBugs, we looked at the functionality of FindBugs as a static analysis tool and how it can be directly integrated into IDEs like Eclipse and IntelliJ Idea.

在我们的FindBugs介绍中,我们探讨了FindBugs作为静态分析工具的功能,以及如何将其直接集成到Eclipse和IntelliJ Idea等IDE中。

In this article, we’re going look into few of the alternative static analysis tools for Java – and how these integrate with Eclipse and IntelliJ IDEA.

在这篇文章中,我们将研究一些替代性的Java静态分析工具–以及这些工具如何与Eclipse和IntelliJ IDEA集成。

2. PMD

2.PMD

Let’s start with PMD.

让我们从PMD开始。

This mature and quite well-established tool analyzes source code for possible bugs, suboptimal codes and other bad practices; it also looks at more advanced metrics such as cyclomatic complexity for the codebase it analyzes.

这个成熟和相当完善的工具分析了源代码中可能存在的错误、次优代码和其他不良做法;它还查看了更高级的指标,如它所分析的代码库的循环复杂性。

2.1. Integration With Eclipse

2.1.与Eclipse的集成

The PMD plugin can be directly installed from Eclipse Marketplace. The plugin can also be manually downloaded here as well. Once installed, we can run the PMD check directly from the IDE itself:

PMD插件可以直接从Eclipse Marketplace安装。该插件也可以手动下载这里。安装后,我们可以直接从IDE本身运行PMD检查。

pmd eclipse run

It’s worth noting we can run PMD at the project level or at individual class level.

值得注意的是,我们可以在项目层面或单个班级层面上运行PMD。

The results are shown below – with different colors for different levels of the finding which ranges from “warning” to “blocker” in the increasing order of severity:

结果显示如下–不同的颜色代表不同级别的发现,其范围从 “警告 “到 “阻止”,严重程度依次递增。

pmd eclipse result

We can dig into the details of each entry by right clicking it and selecting “show details” from the context menu. Eclipse will display a brief description of the issue and possible remediation how to solve it:

我们可以通过右键单击每个条目并从上下文菜单中选择 “显示细节 “来挖掘其细节。Eclipse将显示问题的简要描述和可能的补救措施,以解决这个问题。

pmd eclipse options
pmd eclipse details

It’s also possible to change the configuration of the PMD scan – we can do that in the menu, under Window -> Preferences -> PMD to launch the configuration page. Here, we can configure scan parameters, rule set, result display settings, etc.

也可以改变PMD扫描的配置 – 我们可以在菜单中,在Window -> Preferences -> PMD下启动配置页面。在这里,我们可以配置扫描参数、规则集、结果显示设置,等等。

If we need to deactivate some specific rules for the project – we can simply remove them from the scan:

如果我们需要停用项目的某些特定规则–我们可以简单地从扫描中删除它们。

pmd eclipse config

2.2. Integration With IntelliJ

2.2.与IntelliJ的集成

Of course, IntelliJ has a similar PMD plugin – which can be downloaded and installed from the JetBrains plugin store.

当然,IntelliJ有一个类似的PMD插件–可以从JetBrains插件商店下载和安装。

We can similarly run the plugin right in the IDE – by right-clicking the source we need to scan and selecting PMD scan from the context menu:

我们同样可以在IDE中运行该插件–右击我们需要扫描的源,从上下文菜单中选择PMD扫描。

pmd IntelliJ run

Results are displayed immediately but, unlike in Eclipse, if we try to open the description it will open up a browser with a public web page on finding information:

结果会立即显示出来,但与Eclipse不同的是,如果我们试图打开描述,就会打开一个浏览器,里面有一个关于查找信息的公共网页。

pmd Intellij result

We can set the behavior of the PMD plugin from the settings page, by going to File -> Settings -> other settings -> PMD to view configuration page. From the settings page, we can configure the rule set by loading a custom rule set with our own testing rules.

我们可以从设置页面设置PMD插件的行为,通过进入文件->设置->其他设置->PMD来查看配置页面。从设置页面,我们可以通过加载一个带有我们自己测试规则的自定义规则集来配置规则集。

3. JaCoCo

3.JaCoCo

Moving on – JaCoCo is a test coverage tool – used to keep track of unit test coverage in the codebase. Simply put, the tool calculates the coverage using a number of strategies e.g.: lines, class, methods, etc.

继续说–JaCoCo是一个测试覆盖率工具–用于跟踪代码库中的单元测试覆盖率。简单地说,该工具使用一些策略来计算覆盖率,例如:行、类、方法等等。

3.1. Integration With Eclipse

3.1.与Eclipse的集成

JaCoCo can be directly installed from the marketplace. An install link is also hosted on the official site available here.

JaCoCo可以直接安装从市场上。官方网站上也有一个安装链接,可从这里获得。

jacoco eclipse run

The tool can be executed from project level to individual method level. The Eclipse plugin uses different color schemes to pinpoint what portion of the code is covered by the test cases and what is not covered:

该工具可以从项目层面到单个方法层面执行。Eclipse插件使用不同的颜色方案来确定测试用例覆盖了代码的哪一部分,以及哪些没有覆盖。

jacoco eclise coverage

Our method is dividing two provided integer parameters and return the result. If the second parameter is zero it will return a max value for the integer data type.

我们的方法是将两个提供的整数参数相除并返回结果。如果第二个参数是零,它将返回一个整数数据类型的最大值。

In our test case, we’re only testing out the scenario where the second parameter is zero:

在我们的测试案例中,我们只测试出第二个参数为零的情况。

jacoco eclise coverage test

In this case, we can see that line 6 is colored in yellow. Only one branch of the ‘if’ condition is tested and runs in our simple test. Therefore it’s not completely tested and marked in yellow.

在这种情况下,我们可以看到第6行的颜色是黄色的。在我们的简单测试中,只有 “如果 “条件的一个分支被测试并运行。因此,它没有被完全测试,被标记为黄色。

Furthermore, line 7 has green color – it means that it is fully tested. Finally, line 9 is highlighted with a red color, which means that this line is not tested at all by our unit tests.

此外,第7行是绿色的–这意味着它被完全测试。最后,第9行用红色突出显示,这意味着这一行根本没有被我们的单元测试所测试。

We can see a summary of the test coverage where it displays how much code is covered under unit tests in class level and package levels:

我们可以看到测试覆盖率的摘要,它显示了在类级和包级的单元测试下有多少代码被覆盖。

jacoco eclise coverage summary

3.2. Integration With IntelliJ IDEA

3.2.与IntelliJ IDEA的集成

JaCoCo is bundled by default with the latest IntelliJ IDEA distribution, so there’s no requirement to install the plugin separately.

JaCoCo默认与最新的IntelliJ IDEA发行版捆绑在一起,所以不需要单独安装该插件。

When executing unit tests, we can select what coverage runner we need to use. We can run the test cases either at the project level or at the class level:

在执行单元测试时,我们可以选择我们需要使用的覆盖率运行器。我们可以在项目层面或在类层面运行测试用例。

jacoco intellij run

Similar to Eclipse, JaCoCo displays results using different color schemes for the coverage.

与Eclipse类似,JaCoCo使用不同的颜色方案来显示覆盖范围的结果。

jacoco intellij coverage-2

We can see the summary of the test coverage where it displays how much of the code is covered under unit tests in class level and package levels.

我们可以看到测试覆盖率的总结,它显示了在类级和包级的单元测试下有多少代码被覆盖。

jacoco intellij coverage summary

4. Cobertura

4.淘宝网

Finally, it’s worth mentioning Cobertura – this is similarly used to keep track of unit test coverage in the codebase.

最后,值得一提的是Cobertura–这同样是用来跟踪代码库中的单元测试覆盖率。

The latest version of Eclipse doesn’t support the Cobertura plugin at the time of writing; the plugin does work with earlier Eclipse versions.

在撰写本文时,最新版本的Eclipse不支持Cobertura插件;该插件可以在早期的Eclipse版本中工作。

Similarly, IntelliJ IDEA doesn’t have an official plugin which can execute the Cobertura coverage.

同样地,IntelliJ IDEA也没有一个官方插件可以执行Cobertura覆盖。

5. Conclusion

5.结论

We looked at integration with Eclipse and IntelliJ IDEA for three widely used static analysis tools. FindBug was covered in a previous introduction to FindBugs.

我们研究了三种广泛使用的静态分析工具与Eclipse和IntelliJ IDEA的集成情况。在之前的FindBugs介绍中已经介绍了FindBug。

The source code of this tutorial can be found in the GitHub project – this is a Maven-based project, so it should be easy to import and run as it is.

本教程的源代码可以在GitHub项目中找到–这是一个基于Maven的项目,所以应该很容易导入并按原样运行。