25

When I run a PMD analysis I receive violation:

Each class should declare at least one constructor

This violation is on a Spring controller. This controller is instantiated by Spring, so I shouldn't need to invoke this class.

What is recommended way of ignoring this violation?

According to this doc can use //NOPMD but I just want to ignore specific violation.

2240
  • 1,547
  • 2
  • 12
  • 30
blue-sky
  • 51,962
  • 152
  • 427
  • 752

3 Answers3

40

PMD also supports the @SuppressWarnings annotations:

// This will suppress all the PMD warnings in this class
@SuppressWarnings("PMD")
public class Bar {
 void bar() {
  int foo;
 }
}

Or just one type of warning:

// This will suppress UnusedLocalVariable warnings in this class
@SuppressWarnings("PMD.UnusedLocalVariable")
public class Bar {
 void bar() {
  int foo;
 }
}

And what you might also want to look into are creating rulesets and suppressing warnings. Maybe you want to disable a certain rule, or exclude certain files and folders from PMD checking.

HairyFotr
  • 1,453
  • 1
  • 15
  • 28
  • 2
    There's also the [official documentation](https://pmd.github.io/latest/pmd_userdocs_suppressing_warnings.html) from PMD about suppressions: If you want to ignore multiple warnings: `@SuppressWarnings({"PMD.UnusedLocalVariable", "PMD.UnusedPrivateMethod"})` – Pranav Nov 16 '19 at 00:50
1

In my organization I use the PMD ignore option in POM file to suppress all warnings that are generated for client stubs (and kept in separate module) that we auto-generate, as those are third party client-stubs we don't tend to touch them thought there are any warnings or violations and I am assuming the same thing for you as we well.

Below is a snippet from POM file, which has client stub module

<build>
    <plugins>
        <plugin>
            <!-- <groupId>org.apache.maven.plugins</groupId> -->
            <artifactId>maven-pmd-plugin</artifactId>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>

    <pluginManagement>
        <plugins>       
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.8</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

This way we can entirely skip the warnings that are raised by PMD plugin.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
akshayp
  • 109
  • 1
  • 8
0

If you want to ignore a rule for the entire project you can create a custom ruleset:

config/pmd/pmd-ruleset.xml

<?xml version="1.0" ?>
<ruleset name="custom rules">
    <rule ref="category/java/errorprone.xml">
        <exclude name="AvoidLiteralsInIfCondition" />
    </rule>
</ruleset>

Inside my build.gradle file (I use Gradle for dependency management) I then add:

pmd {
    incrementalAnalysis = true
    ruleSetFiles = files("${rootDir}/config/pmd/pmd-ruleset.xml")
    ruleSets = []
}

Links

Making rulesets | PMD

Suppressing warnings | PMD

Aron Hoogeveen
  • 437
  • 5
  • 16