Friday, October 16, 2015

Custom PMD Rule creation

based on PMD 5.4, following XML should be just copied to pmd configuration file

This rule is extension to ShortVariable with update to skip validation of methods with Override annotation

Rule:
<rule name="CustomShortVariable"
message="Avoid variables with short names that shorter than 2 symbols: {0}"
language="java"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="">
<description>
Fields, local variables, or parameter names that are very short are not helpful to the reader.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//VariableDeclaratorId[string-length(@Image) < 2]
[not(ancestor::ForInit)]
[not(../../VariableDeclarator and ../../../LocalVariableDeclaration and ../../../../ForStatement)]
[not((ancestor::FormalParameter) and (ancestor::TryStatement))]
[not(ancestor::ClassOrInterfaceDeclaration[//MarkerAnnotation/Name[pmd-java:typeof(@Image, 'java.lang.Override', 'Override')]])]
]]>
</value>
</property>
</properties>
</rule>

No comments:

Post a Comment