Skip to content

Instantly share code, notes, and snippets.

@gim-
Last active May 26, 2024 14:05
Show Gist options
  • Save gim-/2e5d4ea07303fa4db437c0727ec39bae to your computer and use it in GitHub Desktop.
Save gim-/2e5d4ea07303fa4db437c0727ec39bae to your computer and use it in GitHub Desktop.
Collection of custom XPath CheckStyle rules
<!-- This is a collection of useful CheckStyle XPath rules -->
<!-- See https://checkstyle.sourceforge.io/checks/coding/matchxpath.html -->
<!-- Some useful rules can also be found in https://github.com/checkstyle/checkstyle/blob/master/config/checkstyle-checks.xml -->
<!-- Forbids usage of Collectors#toList in favour of Stream#toList -->
<module name="MatchXpath">
<property name="query" value="//METHOD_CALL[./DOT/IDENT[@text='collect'] and ..//METHOD_CALL//IDENT[@text='toList']]"/>
<message key="matchxpath.match" value="Use Stream#toList() method to collect items."/>
</module>
<!-- Forbids redundant Stream#collect and Stream#toList operations before Stream#forEach -->
<module name="MatchXpath">
<property name="query" value="//METHOD_CALL/DOT[./IDENT[@text='forEach'] and ./METHOD_CALL/DOT/IDENT[@text='toList']]"/>
<message key="matchxpath.match" value="Collect before forEach is redundant."/>
</module>
<module name="MatchXpath">
<property name="query" value="//METHOD_CALL/DOT[./IDENT[@text='forEach'] and ./METHOD_CALL/DOT/IDENT[@text='collect']]"/>
<message key="matchxpath.match" value="Collect before forEach is redundant."/>
</module>
<!-- Makes sure that utility classes (with only static methods) are final -->
<module name="MatchXpath">
<property name="query"
value="//CLASS_DEF[.//METHOD_DEF and count(.//METHOD_DEF[not(./MODIFIERS/LITERAL_STATIC)]) = 0 and not(./MODIFIERS/FINAL)]"/>
<message key="matchxpath.match"
value="Utility classes must be final."/>
</module>
<!-- Forbids empty methods with no explanation -->
<module name="MatchXpath">
<property name="query"
value="//METHOD_DEF[./SLIST and count(./SLIST/*) = 1]/SLIST"/>
<message key="matchxpath.match" value="Add a nested comment explaining why this method is empty."/>
</module>
<!-- Forbid 'test' in method, variable, class, record, and enum names -->
<module name="MatchXpath">
<property name="query" value="//METHOD_DEF/IDENT[starts-with(@text,'test') or contains(@text,'Test')]"/>
<message key="matchxpath.match" value="Method name must not contain &quot;test&quot; outside of test source sets."/>
</module>
<module name="MatchXpath">
<property name="query" value="//VARIABLE_DEF/IDENT[starts-with(@text,'test') or contains(@text,'Test')]"/>
<message key="matchxpath.match" value="Variable name must not contain &quot;test&quot;."/>
</module>
<module name="MatchXpath">
<property name="query" value="//CLASS_DEF/IDENT[contains(@text,'Test')]"/>
<message key="matchxpath.match" value="Class name must not contain &quot;test&quot;."/>
</module>
<module name="MatchXpath">
<property name="query" value="//RECORD_DEF/IDENT[contains(@text,'Test')]"/>
<message key="matchxpath.match" value="Record name must not contain &quot;test&quot;."/>
</module>
<module name="MatchXpath">
<property name="query" value="//ENUM_DEF/IDENT[contains(@text,'Test')]"/>
<message key="matchxpath.match" value="Enum name must not contain &quot;test&quot;."/>
</module>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment