Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] UnusedPrivateMethod for methods annotated with jakarta.annotation.PostConstruct or PreDestroy #5117

Closed
kdebski85 opened this issue Jul 15, 2024 · 2 comments · Fixed by #5118
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@kdebski85
Copy link
Contributor

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

@kdebski85 kdebski85 added the a:false-positive PMD flags a piece of code that is not problematic label Jul 15, 2024
@jsotuyod
Copy link
Member

@kdebski85 thanks for the report! this looks like a great candidate for a contribution if you are up to it, I'd expect that same approach + the corresponding tests.

@kdebski85
Copy link
Contributor Author

@jsotuyod I prepared #5118
Could you please review?

@adangel adangel added this to the 7.4.0 milestone Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants