Skip to content

[java] UnusedPrivateMethod: could not handle javax.annotation #6627

Closed

Description

@Emilyaxe

Affects PMD Version: 7.23.0

Rule: UnusedPrivateMethod — https://docs.pmd-code.org/latest/pmd_rules_java_bestpractices.html#unusedprivatemethod

Description:

UnusedPrivateMethod incorrectly flags private methods annotated with @javax.annotation.PostConstruct and @javax.annotation.PreDestroy as unused. These methods are invoked reflectively by the container (e.g., CDI / Spring) during the application lifecycle and should therefore not be considered unused.

Code Sample demonstrating the issue:

// (false-positive)
package demo.before;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class LifecycleBean {
    private String message = "initialized";

    @PostConstruct
    private void setup() {              // UnusedPrivateMethod reported here
        this.message = "post-construct done";
    }

    @PreDestroy
    private void cleanup() {            // UnusedPrivateMethod reported here
        this.message = "pre-destroy done";
    }

    public String getMessage() { return message; }
}

Expected outcome: Private methods annotated with @javax.annotation.PostConstruct or @javax.annotation.PreDestroy should be treated as used (invoked reflectively by the container), and UnusedPrivateMethod should not report a violation.

Actual outcome: PMD reports UnusedPrivateMethod on methods annotated with @javax.annotation.PostConstruct and @javax.annotation.PreDestroy, resulting in a false positive.

Running PMD through: CLI

Activity

  1. added
    a:false-positive
    on Apr 27, 2026
  2. UncleOwen commented on Apr 27, 2026

    @UncleOwen
    Member

    Have you tried setting the ignoredAnnotations property?

  3. Emilyaxe commented on Apr 28, 2026

    @Emilyaxe
    Author

    Have you tried setting the ignoredAnnotations property?

    Thank you, I will carefully study the rule configuration.

  4. added this to the 7.26.0 milestone on Jun 29, 2026
  5. added a commit that references this issue on Jun 29, 2026
    07bab85
  6. added a commit that references this issue on Jun 29, 2026
    50157a5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions