Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress violations in PMD?

Tags:

java

spring

pmd

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.

like image 909
blue-sky Avatar asked Aug 28 '15 13:08

blue-sky


People also ask

How do you suppress PMD warnings?

NOPMD comment ✏️️ Note that PMD expects the //NOPMD marker to be on the same line as the violation. So, for example, if you want to suppress an “empty if statement” warning, you'll need to place it on the line containing the if keyword, e.g.: $ cat ~/tmp/Foo.

What is PMD violations in Java?

PMD violations are assigned a priority from 1 (most severe) to 5 (least severe) according the the rule's priority. Violations at or less than this priority level are considered failures and will fail the build if failOnViolation=true and the count exceeds maxAllowedViolations .

What are PMD rules?

Conceptually, PMD rules work by matching a “pattern” against the AST of a file. Rules explore the AST and find nodes that satisfy some conditions that are characteristic of the specific thing the rule is trying to flag. Rules then report a violation on these nodes.


2 Answers

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 a ruleset and exclusions. Maybe you want to disable a certain rule, or exclude certain files from PMD checking.

like image 160
HairyFotr Avatar answered Sep 25 '22 22:09

HairyFotr


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.

like image 31
akshayp Avatar answered Sep 22 '22 22:09

akshayp