Skip to content

[java] NonThreadSafeSingleton: False negative with property checkNonStaticMethods #6780

Closed

Description

@JavaGuangGitHub

Body:

Affects PMD Version: 7.25.0 (also reproducible in 6.44.0)

Rule: NonThreadSafeSingleton

Description:

The property checkNonStaticMethods has the description:

Check for non-static methods. Do not set this to false and checkNonStaticFields to true.

The natural reading is that when checkNonStaticMethods=true, the rule checks non-static methods. However, the implementation does the opposite.

In NonThreadSafeSingletonRule.java:

if (checkNonStaticMethods && !node.isStatic()
        || node.isSynchronized()) {
    return data; // skip the method
}

When checkNonStaticMethods=true (the default), non-static methods are skipped, not checked. Conversely, setting it to false causes non-static methods to be analyzed.

This is inconsistent with the property name and description.

Minimal reproduction:

public class Singleton {
    private static Singleton instance;

    // Non-static getInstance - should be flagged by default
    public Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

With default properties (checkNonStaticMethods=true), PMD reports 0 violations.
Setting checkNonStaticMethods=false reports 1 violation.

Expected behavior:

The behavior should match the description, i.e.:

checkNonStaticMethods=true (default): non-static methods are checked.
checkNonStaticMethods=false: non-static methods are skipped.
Alternatively, if the current behavior is intentional, the property name/description should be changed to something like skipNonStaticMethods to avoid confusion.

Running PMD via: any (verified with unit tests against pmd-java 6.44.0 and 7.25.0)

Activity

  1. added
    a:bug
    on Jun 15, 2026
  2. added
    a:false-negative
    on Jul 17, 2026
  3. changed the title [-][java] NonThreadSafeSingleton: checkNonStaticMethods property description contradicts implementation[/-] [+][java] NonThreadSafeSingleton: False negative with property checkNonStaticMethods[/+] on Aug 31, 2026
  4. added a commit that references this issue on Aug 31, 2026
  5. added this to the 7.28.0 milestone on Aug 31, 2026
  6. added a commit that references this issue on Sep 6, 2026
    e80f725
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:buga:false-negative

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions