Skip to content

Commit

Permalink
Add Checkstyle rule to enforce use of class literals for primitives &…
Browse files Browse the repository at this point in the history
… void

This commit introduces Checkstyle rules for `main` and `test` which
enforce the use of class literals for primitive types and void by
forbidding the use of the TYPE constants in Boolean, Character, Byte,
Short, Integer, Long, Float, Double, and Void.

For example, if MyClass uses one of the TYPE constants, the build will
now fail with a message similar to the following.

[ERROR] <...>/MyClass.java:39: Please use class literals for primitives and void -- for example, int.class instead of Integer.TYPE.
  • Loading branch information
sbrannen committed Feb 6, 2024
1 parent 459ff76 commit 6398e0a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions gradle/config/checkstyle/checkstyleMain.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
<module name="IllegalImport">
<property name="illegalPkgs" value="org.jetbrains.annotations" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="id" value="primitiveClassLiterals"/>
<property name="maximum" value="0"/>
<property name="format" value="(Boolean|Character|Byte|Short|Integer|Long|Float|Double|Void)\.TYPE"/>
<property name="message" value="Please use class literals for primitives and void -- for example, int.class instead of Integer.TYPE."/>
<property name="ignoreComments" value="true"/>
</module>
</module>

<module name="JavadocPackage" />
Expand Down
7 changes: 7 additions & 0 deletions gradle/config/checkstyle/checkstyleTest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
<module name="IllegalImport">
<property name="illegalPkgs" value="org.jetbrains.annotations" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="id" value="primitiveClassLiterals"/>
<property name="maximum" value="0"/>
<property name="format" value="(Boolean|Character|Byte|Short|Integer|Long|Float|Double|Void)\.TYPE"/>
<property name="message" value="Please use class literals for primitives and void -- for example, int.class instead of Integer.TYPE."/>
<property name="ignoreComments" value="true"/>
</module>
</module>

<module name="SuppressWarningsFilter" />
Expand Down

0 comments on commit 6398e0a

Please sign in to comment.