All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.java.S3067.html Maven / Gradle / Ivy

There is a newer version: 8.10.0.38194
Show newest version

Why is this an issue?

getClass should not be used for synchronization in non-final classes because child classes will synchronize on a different object than the parent or each other, allowing multiple threads into the code block at once, despite the synchronized keyword.

Instead, hard code the name of the class on which to synchronize or make the class final.

Noncompliant code example

public class MyClass {
  public void doSomethingSynchronized(){
    synchronized (this.getClass()) {  // Noncompliant
      // ...
    }
  }

Compliant solution

public class MyClass {
  public void doSomethingSynchronized(){
    synchronized (MyClass.class) {
      // ...
    }
  }

Resources

  • CERT, LCK02-J. - Do not synchronize on the class object returned by getClass()




© 2015 - 2025 Weber Informatics LLC | Privacy Policy