3

I'm already know about the various annotation or comment I can add into my code, but it's not what I'm looking for.

I'm looking for a way that only have me modify the CyclomaticComplexity rule. I don't seen any option in the rule, although it seems like a rather common need. And there doesnt seems to be a xpath since it uses a java class.

If nothing better can you override the java class used for the rule?

TheBakker
  • 2,852
  • 2
  • 28
  • 49

2 Answers2

2

You are right, there is no specific option for this rule, to explicitly ignore hashCode and equals methods. However, it should be possible now via suppressions. Every rule has a "violationSuppressXPath" property - and it seems, that this is used for this rule in a way, that it should work with this use case: It's not only applied for the violations, but also while analyzing the source code.

Here's the source code for CyclomaticComplexityRule which inherits from StdCyclomaticComplexityRule. If you look at line 188, you'll see that the method node is checked for any suppressions. If the method is suppressed, then it is not analyzed and should not be counted towards the class complexity. [See update below].

A possible XPath expression for violationSuppressXPath might look like this:

./MethodDeclarator[@Image='hashCode' or @Image='equals']

You'll need to use a custom ruleset, in order to configure the property. You're ruleset might look like this:

<?xml version="1.0"?>
<ruleset name="Custom ruleset"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
  <description>CyclomaticComplexity ignoring hashCode and equals</description>

    <rule ref="rulesets/java/codesize.xml/CyclomaticComplexity">
        <properties>
            <property name="violationSuppressXPath" value="./MethodDeclarator[@Image='hashCode' or @Image='equals']"/>
        </properties>
    </rule>
</ruleset>

Please note, that my examples use PMD 5.4.x - which is much newer than PMD 4.3 you referred to. The rule organization has been changed in order to support multiple languages - this means, the rule reference for PMD 4.3 would be just rulesets/codesize.xml/CyclomaticComplexity.

Update (2016-05-20)

With the configured violationSuppressXPath property, the methods "equals" and "hashCode" are not highlighted anymore. Only the violation messages are suppressed. However the content of the method still does count to the total complexity of the class, so that any conditions/if statements/loops/etc. are checked (this is because super.visit() is called before suppression at line 186) and because the isSuppressed check only understands annotations.

This means, while the method itself is not highlighted anymore, the class is, because it contains a complex method.

The only way to completely ignore the equals/hashCode methods is, to annotate these with

@SuppressWarnings("PMD.CyclomaticComplexity")

Then, they are totally ignored and really do not count towards the class complexity.

adangel
  • 1,816
  • 14
  • 17
  • what would be excellent is the ability to put the method names 'equals' and 'hashCode' in the XML at the top level, similar to files that should be ignored. Probably a big ask, right? – Adam Jul 19 '16 at 10:45
  • 1
    I have annotated my `equals()` and `hashCode()` with `@SuppressWarnings("PMD.CyclomaticComplexity")` but I am still seeing the warnings on the class: `The class 'Order' has a Modified Cyclomatic Complexity of 3 (Highest = 11).` and similarly for `Standard Cyclomatic Comlexity`. – Adam Jul 25 '16 at 09:54
  • 1
    @Adam Old post, but I came across this as well. To address the `Standard Cyclomatic Complexity` and `Modified Cyclomatic Complexity`, you can also use `@SuppressWarnings` and include both `"PMD.ModifiedCyclomaticComplexity"` and `"PMD.StdCyclomaticComplexity"`. – avojak Aug 02 '17 at 16:18
0

It worked for me.

 // Ignore PMD warning that equals and hashCode should be defined, while we need only custom equals.
@SuppressWarnings("PMD.OverrideBothEqualsAndHashcode")
class DefineYourClass...
Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
  • Thanks but it's not what I'm looking for. I'm looking to ignore hashcode and equals for complexity PMD rule, by modifying the PMD rule itself. – TheBakker Feb 13 '19 at 07:39