为什么 Java 接口常量在 Checkstyle 的 ConstantName 检查中被视为非公开?

huangapple go评论49阅读模式
英文:

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

&lt;module name=&quot;ConstantName&quot;&gt;
  &lt;property name=&quot;format&quot; value=&quot;^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$&quot;/&gt;
  &lt;property name=&quot;applyToPrivate&quot; value=&quot;false&quot;/&gt;
&lt;/module&gt;
&lt;module name=&quot;ConstantName&quot;&gt;
  &lt;property name=&quot;format&quot; value=&quot;^_[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$&quot;/&gt;
  &lt;property name=&quot;applyToPublic&quot; value=&quot;false&quot;/&gt;
  &lt;property name=&quot;applyToProtected&quot; value=&quot;false&quot;/&gt;
  &lt;property name=&quot;applyToPackage&quot; value=&quot;false&quot;/&gt;
&lt;/module&gt;

Post running the checkstyle analysis the error is reported for MAX_SIZE as Name &#39;MAX_SIZE&#39; must match pattern &#39;^_[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$&#39;. 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

huangapple
  • 本文由 发表于 2020年7月28日 14:49:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63128481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定