Maven Java 没有警告非详尽的开关
Maven Java not warning about nonexhaustive switch
如果您启用枚举,但既不涵盖所有情况也不提供默认值,那么获取编译器警告很有用。该站点上的其他答案建议 javac 应该提供这样的警告。
我正在使用 Maven,并已将以下内容添加到 pom.xml 以尝试启用所有警告:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgs>
<arg>-Xlint:all</arg>
</compilerArgs>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
但是编译器仍然对我的代码中非详尽的 switch 语句保持沉默。我可以设置其他标志来启用此类警告吗?
我知道此类警告是因为:
- 外部设置,like the IDE Eclipse
(Window->Preferences->Java->Compiler->Errors/Warnings/Enum type constant not covered on switch
)
- 外部 linter,如 custom rule in SonarQube/SonarLint
- 外部工具,例如 Findbugs, and the
SF_SWITCH_NO_DEFAULT
bug description
- 外部项目,例如
tmtron/enum-mapper
后者应该是感兴趣的:
An annotation processor will make sure that you get a compile-time error.
The project also includes a partial enum-mapper class which you may want to use instead of a switch statement.
该项目是 available in Maven Central,因此您可以将其添加到您的依赖项中。
重点仍然是:
像 -Xlint
这样的 javac
non-standard option 可能不足以单独捕捉部分开关枚举用例。
它只会检测失败案例:switch
块中的案例,而不是块中的最后一个案例,其代码不包含 break 语句,允许代码执行到 "fall through" 从那个案例到 下一个案例 .
如果您启用枚举,但既不涵盖所有情况也不提供默认值,那么获取编译器警告很有用。该站点上的其他答案建议 javac 应该提供这样的警告。
我正在使用 Maven,并已将以下内容添加到 pom.xml 以尝试启用所有警告:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgs>
<arg>-Xlint:all</arg>
</compilerArgs>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
但是编译器仍然对我的代码中非详尽的 switch 语句保持沉默。我可以设置其他标志来启用此类警告吗?
我知道此类警告是因为:
- 外部设置,like the IDE Eclipse
(Window->Preferences->Java->Compiler->Errors/Warnings/Enum type constant not covered on switch
) - 外部 linter,如 custom rule in SonarQube/SonarLint
- 外部工具,例如 Findbugs, and the
SF_SWITCH_NO_DEFAULT
bug description - 外部项目,例如
tmtron/enum-mapper
后者应该是感兴趣的:
An annotation processor will make sure that you get a compile-time error.
The project also includes a partial enum-mapper class which you may want to use instead of a switch statement.
该项目是 available in Maven Central,因此您可以将其添加到您的依赖项中。
重点仍然是:
像 -Xlint
这样的 javac
non-standard option 可能不足以单独捕捉部分开关枚举用例。
它只会检测失败案例:switch
块中的案例,而不是块中的最后一个案例,其代码不包含 break 语句,允许代码执行到 "fall through" 从那个案例到 下一个案例 .