介绍
官方原文介绍如图,下方是个人的白话简述
简单的来说,java 编译器在编译期间能够进行一些 'static type checking' ,也就是编译器类型检查,仅此而已。而ErrorProne能够在编译期间进行更深一步的检查,下文会给出直观的对比说明。
安装
maven 在pom 文件内添加如下片段,注意不要出现多个 maven-compiler-plugin。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<compilerId>javac-with-errorprone</compilerId>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<!-- maven-compiler-plugin defaults to targeting Java 5, but our javac
only supports >=6 -->
<source>7</source>
<target>7</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-javac-errorprone</artifactId>
<version>2.8</version>
</dependency>
<!-- override plexus-compiler-javac-errorprone's dependency on
Error Prone with the latest version -->
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.0.15</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
工具配置 IntelliJ IDEA
安装 Intellij ErrorProne插件 。(不会安装的自行百度如何安装插件吧)
启动 Error Prone, 设置 Settings | Compiler | Java Compiler | Use compiler: Javac with error-prone 。如果 Settings | Compiler | Use external build 有这个选项,则确保此选项没有选中。
以上就完成了 maven 配置及插件安装(注意,gradle 和 ant ,eclipse 等也是支持的,只是这里没有讲解)。
效果
如图
上图中的两段代码,在正常的javac 编译时候是不会出现错误的,这里就不贴图证明了。
配置了ErrorProne 之后,编译效果如下:
一个说array 进行直接toString 的问题,一个是 Set 使用Short泛型后,加入Integer类型的问题。自行感受吧。
后记将补充更多的关于ErrorProne的使用说明,请持续关注。