Add Checkstyle rule to enforce use of class literals for primitives & void

This commit introduces a globally applied Checkstyle rule which
enforces 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.
This commit is contained in:
Sam Brannen
2024-02-01 12:44:57 +01:00
parent 3d4d68c26f
commit c989cba963

View File

@@ -213,6 +213,13 @@
<property name="illegalPattern" value="true"/>
<property name="message" value="Trailing whitespace"/>
</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
name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="maximum" value="0"/>