英文:
Why are java interface constants treated as non-public by ConstantName check in Checkstyle
问题
在我们的项目中,我们希望确保私有常量始终以**_(下划线)开头,而其他常量则不以下划线开头。
Checkstyle检查ConstantName**未将接口常量视为公共常量,并应用了私有修饰符的规则。
我们在我们的Gradle项目中使用Checkstyle 8.35来分析Java代码(OpenJdk 11,Gradle 6.4)。
以下是具有常量的接口的代码片段。
public interface MyInterface() {
int MAX_SIZE = 1024;
//Some methods here
}
ConstantName检查的Checkstyle配置如下:
<module name="ConstantName">
<property name="format" value="^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
<property name="applyToPrivate" value="false"/>
</module>
<module name="ConstantName">
<property name="format" value="^_[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
<property name="applyToPublic" value="false"/>
<property name="applyToProtected" value="false"/>
<property name="applyToPackage" value="false"/>
</module>
运行Checkstyle分析后,将报告错误,错误消息为Name 'MAX_SIZE' must match pattern '^_[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'
,而我们的期望是没有错误。
英文:
In our project we want to ensure that the private constants always start with _ (underscore) and rest all do not start with an underscore.
The checkstyle check ConstantName fails to treat the interface constants as public and applies the rules of private modifier.<br>
We are using checkstyle 8.35 in our gradle project to analyse java code (OpenJdk 11, Gradle 6.4).
Below is the code snippet of Interface with constants.
public interface MyInterface() {
int MAX_SIZE = 1024;
//Some methods here
}
Checkstyle configuration for ConstantName check is as below
<module name="ConstantName">
<property name="format" value="^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
<property name="applyToPrivate" value="false"/>
</module>
<module name="ConstantName">
<property name="format" value="^_[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
<property name="applyToPublic" value="false"/>
<property name="applyToProtected" value="false"/>
<property name="applyToPackage" value="false"/>
</module>
Post running the checkstyle analysis the error is reported for MAX_SIZE
as Name 'MAX_SIZE' must match pattern '^_[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.
while our expectation is No Errors.
答案1
得分: 4
根据源代码的快速扫描,这似乎是一个错误。只有在关键字public
存在时(这在类中是这样,但在接口中不是这样)才认为常量是公共的。
我建议将此问题报告给https://github.com/checkstyle/checkstyle/issues。
英文:
Judging by a quick scan of the source code, this is a bug. They're only considering the constant to be public if the keyword public
is present (which is the case in classes, but not interfaces).
I would suggest reporting this as an issue at https://github.com/checkstyle/checkstyle/issues
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论