Skip to content

Commit 6398e0a

Browse files
committed
Add Checkstyle rule to enforce use of class literals for primitives & 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.
1 parent 459ff76 commit 6398e0a

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

gradle/config/checkstyle/checkstyleMain.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
<module name="IllegalImport">
2121
<property name="illegalPkgs" value="org.jetbrains.annotations" />
2222
</module>
23+
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
24+
<property name="id" value="primitiveClassLiterals"/>
25+
<property name="maximum" value="0"/>
26+
<property name="format" value="(Boolean|Character|Byte|Short|Integer|Long|Float|Double|Void)\.TYPE"/>
27+
<property name="message" value="Please use class literals for primitives and void -- for example, int.class instead of Integer.TYPE."/>
28+
<property name="ignoreComments" value="true"/>
29+
</module>
2330
</module>
2431

2532
<module name="JavadocPackage" />

gradle/config/checkstyle/checkstyleTest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
<module name="IllegalImport">
1313
<property name="illegalPkgs" value="org.jetbrains.annotations" />
1414
</module>
15+
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
16+
<property name="id" value="primitiveClassLiterals"/>
17+
<property name="maximum" value="0"/>
18+
<property name="format" value="(Boolean|Character|Byte|Short|Integer|Long|Float|Double|Void)\.TYPE"/>
19+
<property name="message" value="Please use class literals for primitives and void -- for example, int.class instead of Integer.TYPE."/>
20+
<property name="ignoreComments" value="true"/>
21+
</module>
1522
</module>
1623

1724
<module name="SuppressWarningsFilter" />

0 commit comments

Comments
 (0)