Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fixing color coding

...

This noncompliant code example compares the name of the class (Auth) of object auth to the string DefaultAuthenticationHandler and proceeds depending on the result of the comparison.

Code Block
bgColorffcccc#FFCCCC
 // Determine whether object auth has required/expected class name
if (auth.getClass().getName().equals("com.application.auth.DefaultAuthenticationHandler")) {
  // ...
}

...

This compliant solution compares the class object of class Auth to the class object of the class that the current class loader loads, instead of comparing just the class names.

Code Block
bgColorccccff#ccccff
 // Determine whether object h has required/expected class name
if (auth.getClass() == this.getClassLoader().loadClass("com.application.auth.DefaultAuthenticationHandler")) {
  // ...
}

...

This noncompliant code example compares the names of the class objects of classes x and y using the equals() method.

Code Block
bgColorffcccc#FFCCCC
// Determine whether objects x and y have the same class name
if (x.getClass().getName().equals(y.getClass().getName())) {
  // Code assumes that the objects have the same class
}

...

This compliant solution correctly compares the two objects' classes.

Code Block
bgColorccccff#ccccff
// Determine whether objects x and y have the same class
if (x.getClass() == y.getClass()) {
  // Code determines whether the objects have the same class
}

...