Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#54 Submission of StaticAssignmentSynchronized check

Unstable_(example)
wont-fix
nobody
None
5
2013-11-12
2004-04-03
Matt Inger
No

This check is intended to flag potential race conditions
caused by assigning a static class member in a block of
code which is not synchronized. In a majority of cases,
this will apply to the getInstance() method in a
singleton class, which typically looks like:

public static synchronized final MyClass getInstance() {
if (instance == null) {
instance = new MyClass();
}
return instance;
}

However, if the sychronization is left out, it is possible
that two threads could call the getInstance() method at
once, and both have the "if" clause evaluate to true,
and thus, you would end up constructing two instance
of MyClass, which would be a clear violation of the
Singleton pattern.

This check takes it a step further, to ensure that any
static variable assignment is performed inside of a
synchronized code block, which can be:
A synchronized block
A synchronized method
* A static intializer block

This check visits Assignment nodes, and then traverses
on it's own up and down the tree to find the appropriate
variable definition for what is being assigned. Once it
finds it, it will decide if it is static or not, and then
whether it's in a synchronized block.

Discussion

  • Matt Inger
    Matt Inger
    2004-04-03

    Source code, test class, and test input for the check.

     
    Attachments
  • Checkstyle is a static code analysis tool and so it hasn`t any ability to check if code is really called from multiple threads, or not. So, this check produces more false positives than right warnings. I am closing this patch.

     
    • status: open --> wont-fix
    • Group: --> Unstable_(example)