Why are java interface constants treated as non-public by ConstantName check in Checkstyle

Viewed 97

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.
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 Answers

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

Related