Skip to content

Commit

Permalink
Merge pull request #780 from didebughu/main
Browse files Browse the repository at this point in the history
🎨 catch information of Error-Prone
  • Loading branch information
cyw3 authored Mar 2, 2023
2 parents e4cf15f + 854fd1d commit e072003
Show file tree
Hide file tree
Showing 6 changed files with 7,203 additions and 1 deletion.
4 changes: 3 additions & 1 deletion client/tool/javawarning.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

"""
javawarning, 获取java编译警告信息
Error Prone,增强java的类型分析
"""

import os
Expand Down Expand Up @@ -65,7 +66,8 @@ def analyze(self, params):
issues = list()
fi = open(build_log_path)
for line in fi.readlines():
if line.find(": warning: [") != -1 or line.find(": 警告: [") != -1:
if (line.find(": warning: [") != -1 or line.find(": 警告: [") != -1 or
line.find(": error: [") != -1 or line.find(": 错误: [") != -1):
infos = line.split(":")
path = infos[0].strip()[pos:]
line_num = int(infos[1].strip())
Expand Down
1 change: 1 addition & 0 deletions doc/.vuepress/configs/sidebar/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const en: SidebarConfig = {
'/en/guide/代码检查/工具/TCA-Armory-C1.md',
'/en/guide/代码检查/工具/TCA-Armory-Q1.md',
'/en/guide/代码检查/工具/cppcheck.md',
'/en/guide/代码检查/工具/Error-Prone.md',
],
},
{
Expand Down
1 change: 1 addition & 0 deletions doc/.vuepress/configs/sidebar/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const zh: SidebarConfig = {
'/zh/guide/代码检查/工具/TCA-Armory-C1.md',
'/zh/guide/代码检查/工具/TCA-Armory-Q1.md',
'/zh/guide/代码检查/工具/cppcheck.md',
'/zh/guide/代码检查/工具/Error-Prone.md',
],
},
{
Expand Down
97 changes: 97 additions & 0 deletions doc/en/guide/代码检查/工具/Error-Prone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Error Prone 使用手册

## Error Prone 介绍

> Error Prone是google开源的Java编译时检测工具,将常见的Java错误捕获为编译时错误,增强对java代码的类型分析,从而让开发人员及时发现问题
## TCA使用指引

TCA原有编译时检测工具JavaWarning获取java代码编译时的告警信息,现集成Error Prone规则至JavaWarning工具以增加获取Error Prone的错误告警信息。
- 在规则包中添加JavaWarning工具的Error Prone规则(可通过规则解决方法进行区分);
- 采用[TCA Client](../../../guide/客户端/本地分析.md)模式,根据客户端环境配置工具和编译命令,详情参考下文;
- 客户端启动分析,在TCA Web页面上查看问题。


## Error Prone 配置

### 通过Bazel构建

- Bazel在构建java项目时,默认打开了Error Prone,所以在本地配置Bazel环境,编写Bazel构建文件,`bazel build :project`构建项目即可。
- 详情请参考[Bazel官方文档](https://bazel.build/?hl=zh-cn)

### Maven 配置 Error Prone

编辑pom.xml文件将设置添加到maven-compiler-plugin,例如:
```xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>8</source>
<target>8</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-XDcompilePolicy=simple</arg>
<arg>-Xplugin:ErrorProne</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>${error-prone.version}</version>
</path>
<!-- Other annotation processors go here.
If 'annotationProcessorPaths' is set, processors will no longer be
discovered on the regular -classpath; see also 'Using Error Prone
together with other annotation processors' below. -->
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
```
对于`JDK 16`或更高的版本,需要将以下内容`--add-exports``--add-opens`标志添加到`.mvn/jvm.config`文件中:
```html
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
--add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
--add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
```

###命令行
Error Prone 支持`com.sun.source.util.Plugin`API,并且可以通过将 Error Prone 添加到`-processorpath`并设置`-Xplugin`标志来与`JDK 9`及更高版本一起使用:
```shell
wget https://repo1.maven.org/maven2/com/google/errorprone/error_prone_core/${EP_VERSION?}/error_prone_core-${EP_VERSION?}-with-dependencies.jar
wget https://repo1.maven.org/maven2/org/checkerframework/dataflow-errorprone/3.15.0/dataflow-errorprone-3.15.0.jar
javac \
-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
-XDcompilePolicy=simple \
-processorpath error_prone_core-${EP_VERSION?}-with-dependencies.jar:dataflow-errorprone-3.15.0.jar \
'-Xplugin:ErrorProne -XepDisableAllChecks -Xep:CollectionIncompatibleType:ERROR' \
Example.java
```
- 对于`JDK 16`以及更高的版本`--add-exports``--add-opens`参数是必须的
- 对于`JDK 8`,请参考[旧版本安装说明](https://github.com/google/error-prone/blob/f8e33bc460be82ab22256a7ef8b979d7a2cacaba/docs/installation.md)

### 其他配置和注意事项
- Error Prone还支持通过Gradle和Ant配置,详情参考[Error Prone官方配置文档](https://errorprone.info/docs/installation)
- 不同JDK版本参数有所不同,详情参考[旧版本安装说明](https://github.com/google/error-prone/blob/f8e33bc460be82ab22256a7ef8b979d7a2cacaba/docs/installation.md)
97 changes: 97 additions & 0 deletions doc/zh/guide/代码检查/工具/Error-Prone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Error Prone 使用手册

## Error Prone 介绍

> Error Prone是google开源的Java编译时检测工具,将常见的Java错误捕获为编译时错误,增强对java代码的类型分析,从而让开发人员及时发现问题
## TCA使用指引

TCA原有编译时检测工具JavaWarning获取java代码编译时的告警信息,现集成Error Prone规则至JavaWarning工具以增加获取Error Prone的错误告警信息。
- 在规则包中添加JavaWarning工具的Error Prone规则(可通过规则解决方法进行区分);
- 采用[TCA Client](../../../guide/客户端/本地分析.md)模式,根据客户端环境配置工具和编译命令,详情参考下文;
- 客户端启动分析,在TCA Web页面上查看问题。


## Error Prone 配置

### 通过Bazel构建

- Bazel在构建java项目时,默认打开了Error Prone,所以在本地配置Bazel环境,编写Bazel构建文件,`bazel build :project`构建项目即可。
- 详情请参考[Bazel官方文档](https://bazel.build/?hl=zh-cn)

### Maven 配置 Error Prone

编辑pom.xml文件将设置添加到maven-compiler-plugin,例如:
```xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>8</source>
<target>8</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-XDcompilePolicy=simple</arg>
<arg>-Xplugin:ErrorProne</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>${error-prone.version}</version>
</path>
<!-- Other annotation processors go here.
If 'annotationProcessorPaths' is set, processors will no longer be
discovered on the regular -classpath; see also 'Using Error Prone
together with other annotation processors' below. -->
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
```
对于`JDK 16`或更高的版本,需要将以下内容`--add-exports``--add-opens`标志添加到`.mvn/jvm.config`文件中:
```html
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
--add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
--add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
```

###命令行
Error Prone 支持`com.sun.source.util.Plugin`API,并且可以通过将 Error Prone 添加到`-processorpath`并设置`-Xplugin`标志来与`JDK 9`及更高版本一起使用:
```shell
wget https://repo1.maven.org/maven2/com/google/errorprone/error_prone_core/${EP_VERSION?}/error_prone_core-${EP_VERSION?}-with-dependencies.jar
wget https://repo1.maven.org/maven2/org/checkerframework/dataflow-errorprone/3.15.0/dataflow-errorprone-3.15.0.jar
javac \
-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
-XDcompilePolicy=simple \
-processorpath error_prone_core-${EP_VERSION?}-with-dependencies.jar:dataflow-errorprone-3.15.0.jar \
'-Xplugin:ErrorProne -XepDisableAllChecks -Xep:CollectionIncompatibleType:ERROR' \
Example.java
```
- 对于`JDK 16`以及更高的版本`--add-exports``--add-opens`参数是必须的
- 对于`JDK 8`,请参考[旧版本安装说明](https://github.com/google/error-prone/blob/f8e33bc460be82ab22256a7ef8b979d7a2cacaba/docs/installation.md)

### 其他配置和注意事项
- Error Prone还支持通过Gradle和Ant配置,详情参考[Error Prone官方配置文档](https://errorprone.info/docs/installation)
- 不同JDK版本参数有所不同,详情参考[旧版本安装说明](https://github.com/google/error-prone/blob/f8e33bc460be82ab22256a7ef8b979d7a2cacaba/docs/installation.md)
Loading

0 comments on commit e072003

Please sign in to comment.