A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/5117 below:

[java] UnusedPrivateMethod for methods annotated with jakarta.annotation.PostConstruct or PreDestroy · Issue #5117 · pmd/pmd · GitHub

Affects PMD Version:
7.3.0

Rule:

UnusedPrivateMethod
Description:

UnusedPrivateMethod should not be reported for methods annotated with jakarta.annotation.PostConstruct or jakarta.annotation.PreDestroy since they are usually called by a container (for example Spring).

As a workaround, the annotations can be added to configuration:

    <rule ref="category/java/bestpractices.xml/UnusedPrivateMethod">
        <properties>
            <property name="ignoredAnnotations" value="java.lang.Deprecated,jakarta.annotation.PostConstruct,jakarta.annotation.PreDestroy" />
        </properties>
    </rule>

However, I think such methods should be excluded by default.

Code Sample demonstrating the issue:

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.stereotype.Component;

@Component
public class JakartaTest {

    @PostConstruct
    private void setup() {
    }

    @PreDestroy
    private void cleanup() {
    }
}

Expected outcome:

PMD reports a violation at lines 0 and 13, but that's wrong. That's a false positive.

Running PMD through: Gradle


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4