Repository navigation
[java] Fix #6780: NonThreadSafeSingleton checkNonStaticMethods contradicting impl - #6864
Conversation
… its description (pmd#6780) checkNonStaticMethods=true (the default) was silently skipping non-static methods instead of checking them - the exact opposite of the documented behavior. Inverted the condition so the flag now matches its Javadoc, and added regression tests covering both the default (checked) and checkNonStaticMethods=false (skipped) cases.
|
Compared to main: (comment created at 2026-09-06 18:24:45+00:00 for 8d69d13) |
|
The the bug was introduced 20 years ago by migration to Java in 6007383#diff-2a607102b1fe9a683c3b7e498cf69a1087e5289883022e799608a29e8e16a307. For any user who configured the rule since then to be more strict (setting |
|
Going even further back, both properties are part of the rule since it was added in 3cc106a. Unfortunately, neither commit has a link to a bug tracker. I would really like to see the rationale why In any case, I agree that inverting the meaning of a 20-year-old flag would be problematic. |
|
The For class Invoice {
static Databse db;
public short money;
public void save() {
if (db == null) {db = Database.connect("cloud");}
db.saveInvoice(money);
}
}and class Invoice {
static Databse db;
public short money;
private static void initDB() {
if (db == null) {db= Database.connect("cloud");}
}
public void save() {
initDB();
db.saveInvoice(money);
}
}Whether the "singleton" classification is applicable to these two samples is debatable, but it fits both equally well. |
|
Thanks for the explanation. Makes sense. But I think we should try to keep both flags. Enabling them makes the rule stricter, but also increases the risk of false positives. We have no way to know which initializations need to be thread-safe. Just my gut feeling. Alternative proposal: Rename the rule to something like In any case, we should do the following:
|
|
It seems, that these two properties of the rule are totally untested. No test case uses these properties. The only testcase, that exercises a non-static method is this: The bug id "1573591" refers to sourceforge's bug id, before they migrated to the new system. You can still search by this number and you'll find https://sourceforge.net/p/pmd/bugs/584/ In summary, I think, we can argue that we are actually fixing a false negative of this rule (that it ignores by default non-static methods). I like the idea of deprecating these properties, to make this clear. The rule checks for a lazy-intialization pattern (if x == null x = new X()) and IMHO the rule should simply report this whether the method is static or not.
Yes, I'd do it.
I'd argue, that whether it is a singleton or a lazy loading pattern can't be decided by just looking at the code. If there is only one instance of this class (e.g. a Spring singleton bean), then what looks like lazy loading is at the same time a singleton pattern (very unnecessary, but still). Even the rule description seems to make some assumptions already ("Static singletons are usually not needed as only a single instance exists anyway" ???). Maybe in a separate PR, we should also deprecate the "checkNonStaticFields" property and rename the rule to "NonThreadSafeInitialization"? So, in summary I'd suggest for this PR:
Then for the future, maybe create an issue to rename the rule to "NonThreadSafeInitialization" (or similar) and improve it further. |
|
If the two of you are fine with deprecating the property, feel free to ignore my gut feeling! |
Your gut feeling is correct, the proposed change may create false positives. Maybe the release notes should mention how to get the current behavior by setting the XPath suppression property for this rule. But since setting up XPath suppression is relatively easy, maybe there's no need to maintain the deprecated property (?). |
|
I didn't mean to derail the discussion. How should we proceed from here?
I'm fine either way, you guys decide. |
|
I think making the buggy property no-op and documenting how to restore old behavior with XPath suppression would improve things and would not require major version bump. If you see need for bigger changes (like renaming the rule or removing/deprecating the non-buggy property), it would make sense to wait for v8. |
Updated the NonThreadSafeSingletonRule to remove the check for non-static methods as it is now always enforced. Added deprecation notice for the ignored property.
- for suppressing via xpath - for checkNonStaticFields
This makes sure, we resolve the correct fields in case of shadowing.
Describe the PR
The
checkNonStaticMethodsproperty description says "Check for non-static methods," implying that when set totrue(the default), non-static methods are analyzed by the rule. The implementation did the opposite: non-static methods were skipped whencheckNonStaticMethods=true, and only checked when it was set tofalse.This meant the rule silently missed non-static singleton getter methods (e.g.
public Singleton getInstance()) under default settings, which is a common real-world form of this anti-pattern.Fix: inverted the condition in
NonThreadSafeSingletonRule#visit(ASTMethodDeclaration, Object)so the property now behaves as documented —truechecks non-static methods,falseskips them.Tests: added two regression cases to
NonThreadSafeSingleton.xml— one confirming a non-staticgetInstance()is flagged with default properties, and one confirming it's not flagged whencheckNonStaticMethods=false. All 11 pre-existing test cases still pass unchanged, since they all exercise static methods and never hit the changed branch.Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)