Menu

#991 AvoidSynchronizedAtMethodLevel for static methods

PMD-5.0.5
closed
PMD
4-Minor
Bug
2013-08-03
2012-01-22
No

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

Discussion

  • Steven Christou

    Steven Christou - 2012-01-31

    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);}
    }
    }

     
  • Romain PELISSE

    Romain PELISSE - 2012-01-31

    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...

     
  • Andreas Dangel

    Andreas Dangel - 2013-03-16
    • labels: pmd --> pmd, documentation
    • summary: AvoidSynchronizedAtMethodLevel --> AvoidSynchronizedAtMethodLevel for static methods
    • status: pending --> in-progress
    • assigned_to: Steven Christou --> Andreas Dangel
    • module: --> PMD
    • milestone: --> PMD-5.1.0
    • priority: 5 --> 4-Minor
    • type: --> Bug
    • affects_version: -->
     
  • Andreas Dangel

    Andreas Dangel - 2013-03-16

    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:

    public class Test {
        public static synchronized void foo() {
            // complete method is synchronized on Test.class
        }
    }
    

    Good Case:

    public class Test {
        public static void foo() {
            synchronized(Test.class) {
                // only a block is synchronized on Test.class
            }
        }
    }
    
     
  • Andreas Dangel

    Andreas Dangel - 2013-03-30
    • status: in-progress --> closed
     
  • Andreas Dangel

    Andreas Dangel - 2013-08-03
    • Milestone: PMD-5.1.0 --> PMD-5.0.5
     

Log in to post a comment.