Why does the main Spring Boot application always trigger PMD's HideUtilityClassConstructorCheck?

The inspection speaks for itself.

By default any code inspector (IntelliJ IDEA, FindBugs, PMD, Sonar) assumes that if class has only static methods then it's utility class. Example of utility class is java.lang.Math, which looks like this:

public final class Math {

    /**
     * Don't let anyone instantiate this class.
     */
    private Math() {}

    public static double exp(double a) {
        ...
    }

    // More helper methods
}

Such classes are designed for using it as a bag of static functions: it's a good practice to declare private constructor for it, so no one will ever instantiate it by mistake and declare class final, because extending it makes no sense.

In your case (and in case almost every entry point of Spring Boot applications) SampleApplication class has one public static void main method, so PMD decides its utility class, checks private construction and final modifier and flags the error. It's not the problem, PMD just don't know about Spring Boot or any other frameworks and their entry points, so it makes perfect sense to suppress this warning and exclude your class from PMD: for me its more semantically correct than adding private constructor to application entry point.


The PMD UseUtilityClass rule can be suppressed just for classes with @SpringBootApplication annotation using the following snippet in the PMD ruleset XML file:

<rule ref="category/java/design.xml/UseUtilityClass">
    <properties>
        <property name="violationSuppressXPath" value="//ClassOrInterfaceDeclaration/preceding-sibling::Annotation/MarkerAnnotation/Name[@Image='SpringBootApplication']" />
    </properties>
</rule>