Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 catch information of Error-Prone #780

Merged
merged 6 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
93 changes: 93 additions & 0 deletions doc/en/guide/代码检查/工具/Error-Prone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Error Prone 使用手册

## Error Prone 介绍

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

## TCA使用指引
由于Error Prone同为编译时检测工具,已添加规则至JavaWarning工具以增强类型检测。建议采用客户端本地分析模式以满足编译条件,参考[编译配置](../../../guide/分析方案/代码检查编译配置.md),Error Prone客户端配置和编译命令可参考文档下文。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要详细写下:

  • TCA集成方式。第一句有点表意不清,javawarning是什么?error prone跟javawarning的关系是?为什么这样集成?等问题需要解释下。
  • 配置步骤尽可能详细些,比如
      1. 在规则包中增加 JavaWarning 中对应 errorp prone的规则(如何在javawarning中区分出error prone规则?);
      1. TCA Client采用何种方式调用?需要怎么进行编译上的配置(可以跳转到下面)
      1. 启动分析,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)
93 changes: 93 additions & 0 deletions doc/zh/guide/代码检查/工具/Error-Prone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Error Prone 使用手册

## Error Prone 介绍

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

## TCA使用指引
由于Error Prone同为编译时检测工具,已添加规则至JavaWarning工具以增强类型检测。建议采用客户端本地分析模式以满足编译条件,参考[编译配置](../../../guide/分析方案/代码检查编译配置.md),Error Prone客户端配置和编译命令可参考文档下文。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

跟上面的一样。



## 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