How to modify existing PMD rules

There were numerous questions about creating new rules and creating rule sets. These are also topics that have been touched upon (or, so to speak) by official documentation.

But what if I want to adapt an existing rule? For instance. I want to change the rule "static end members must be all caps" to ignore the registrar. I know that this decision is contradictory , but we have reached a system-wide consensus on this issue.

Do I need to download and configure the source distribution? Hope there is a better way.

A better way might also include using a different code review structure with a simple setup, but I would rather not change horses in the middle of the stream.

+4
source share
3 answers

You have undoubtedly moved from this to the present, but provide an answer to those who see this question in the future.

You can extend the Java rule by creating your own Java rule that subclasses the built-in PMD. Then you create your own XML rule set that points to your Java rule.

Unfortunately, the VariableNamingConventions rule has all the logic in a private method without extension points. You should probably create your own rule that wraps them. This way you can check your special case and then delegate an existing rule to the rest.

+2
source

You can use XPath to create your copy of the rule in the rule set file. The declarative nature of XPath is also a little getting used to, but it can follow short complex rules.

http://techtraits.com/programming/2011/11/01/writting-pretty-code-with-pmd.html

http://blog.code-cop.org/search/label/PMD

+1
source

In Eclipse, go to Windows -> Prefernces -> PMD -> Rule Configration

Check out "Use Global..."

Scroll and find VariableNamingConvention, w/ ruleset Naming

Click "Rule Designer"

Click the Exceptions tab

Add VariableDeclaratorId[@Image='logger'] to the XPath exception expression

Click OK, and then repeat the PMD verification code.


Change the term logger above to any of your variable names.

0
source

Source: https://habr.com/ru/post/1335416/


All Articles