[java] DoubleCheckedLocking: False positives
A source code analyzer
Brought to you by:
adangel,
juansotuyo
The rule triggers on the following implementation (in a Java7 codebase):
private static volatile Foo instance; public static Foo getInstance() { Foo result = instance; if (result == null) { synchronized (Foo.class) { result = instance; if (result == null) { result = instance = new Foo(); } } } return result; }
This implementation should be OK for JDK 5+ according to: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html (section "Under the new Java Memory Model")
See also: https://sourceforge.net/p/pmd/bugs/884/
As a workaround, you can get rid of the false positive by avoiding to use a local variable ("result"), until the next pmd release.
This bug will be fixed with PMD 5.3.7, 5.4.2 and 5.5.0.
Commit: https://github.com/pmd/pmd/commit/cb5ac3086e4d33c50c1bb91ca0a9971429327bd7
Last edit: Andreas Dangel 2016-04-30