Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3551

Overrides should match their parent class methods in synchronization

    XMLWordPrintable

    Details

    • Message:
      * Make this method "synchronized" to match the parent class implementation.
    • Highlighting:
      • method name for missing "synchronized" keyword
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      20min
    • CERT:
      MET04-J.
    • SwiftLint:
      no_extension_access_modifier

      Description

      When @Overrides of synchronized methods are not themselves synchronized, the result can be improper synchronization as callers rely on the thread-safety promised by the parent class.

      Noncompliant Code Example

      public class Parent {
      
        synchronized void foo() {
          //...
        }
      }
      
      public class Child extends Parent {
      
       @Override
        public void foo () {  // Noncompliant
          // ...
          super.foo();
        }
      }
      

      Compliant Solution

      public class Parent {
      
        synchronized void foo() {
          //...
        }
      }
      
      public class Child extends Parent {
      
        @Override
        synchronized void foo () {
          // ...
          super.foo();
        }
      }
      

      See

      • CERT, TSM00-J - Do not override thread-safe methods with methods that are not thread-safe

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              alexandre.gigleux Alexandre Gigleux
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: