Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] NonThreadSafeSingleton false positive with double-checked locking #4483

Closed
adangel opened this issue Apr 18, 2023 · 0 comments
Closed
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@adangel
Copy link
Member

adangel commented Apr 18, 2023

Affects PMD Version: 6.x

Rule: NonThreadSafeSingleton

Description:

The rule reports a violation even for double checked locking, that is implemented correctly.

Code Sample demonstrating the issue:

public class Foo {
    private static volatile Object o;
    public static Object getO() {
        if (o == null) {   // violation
            synchronized(Foo.class) {
                if (o == null) {
                    o = new Object();
                }
            }
        }
        return o;
    }
}

Expected outcome: No violation

Note: This has already been fixed for PMD 7.0.0-rc1 via #3474. This issue is just there for documentation.

@adangel adangel added the a:false-positive PMD flags a piece of code that is not problematic label Apr 18, 2023
@adangel adangel added this to the 7.0.0 milestone Apr 18, 2023
@adangel adangel changed the title [java] NonThreadSafeSingleton false positive with double checked locking [java] NonThreadSafeSingleton false positive with double-checked locking Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

No branches or pull requests

1 participant