AvoidSynchronizedAtMethodLevel for static methods
A source code analyzer
Brought to you by:
adangel,
juansotuyo
AvoidSynchronizedAtMethodLevel should not be used in static methods - since inner blocks cannot be synchronized using" this".
If you disagree update the describe a suitable synchronization for a static methods
I do not believe this is an issue that PMD should be catching because that is actually a compilation error and not a best practices or pmd bug. Could you provide some example code where it's not a compilation error?
Is this an example of what you ment?
public class Foo{
public static void bar() throws Exception {
synchronized(this) // This causes compilation error
{Thread.sleep(10);}
}
}
Setting this bug to pending... so that it Oren does not reply (or agrees and don't want to lose time replying), the bug will be closed automatically after one month...
The rule triggers any method that is synchronized - whether static or not.
The goal of the rule is, to avoid synchronizing a complete method - block level synchronization is preferred.
I'll update the documentation to have an example for static method synchronization.
Bad Case:
Good Case: