You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] NonThreadSafeSingleton: False negative with ternary conditional operator #6747
PMD's NonThreadSafeSingleton rule flags the unsafe lazy-initialization pattern if (field == null) field = new T(); in a non-synchronized method (a classic check-then-act race).
When the same logic is rewritten as a ternary assignment — field = field == null ? new T() : field; — the rule no longer fires (false negative), even though the rewrite is semantically identical and exposes the very same data race under concurrent access.
Reproducer
before — if-form (2 findings)
publicclassLazyInitBefore {
privatestaticObjectfoo;
privatestaticObjectbar;
publicstaticObjectgetFoo() {
if (foo == null)
foo = newObject(); // flaggedreturnfoo;
}
publicstaticObjectgetBar() {
if (bar == null)
bar = newObject(); // flaggedreturnbar;
}
}
NonThreadSafeSingleton should report the same number of findings on BEFORE and AFTER. The ternary form field = field == null ? new T() : field; is semantically identical to if (field == null) field = new T(); and has the same data-race window when invoked concurrently.
Actual behavior
Two if-form lazy inits → 2 findings (correct).
Two ternary-form lazy inits → 0 findings.
The matcher only recognizes the if-statement shape of the lazy-init pattern and does not look through the equivalent conditional-assignment ternary.
changed the title [-][java] FN: `NonThreadSafeSingleton` misses ternary-form lazy initialization[/-][+][java] NonThreadSafeSingleton: False negative with ternary conditional operator[/+]on Jun 2, 2026
Activity
[-][java] FN: `NonThreadSafeSingleton` misses ternary-form lazy initialization[/-][+][java] NonThreadSafeSingleton: False negative with ternary conditional operator[/+][java] NonThreadSafeSingleton: extract shared field-write helper (pmd…
[java] NonThreadSafeSingleton: also extract shared null-check helper (p…
[java] Fix pmd#6747: NonThreadSafeSingleton flags ternary lazy init (p…
[doc] Fix Kotlin XPath docs review comments