...
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 | ||
|---|---|---|
| ||
// 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 | ||
|---|---|---|
| ||
// 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 | ||
|---|---|---|
| ||
// 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 | ||
|---|---|---|
| ||
// Determine whether objects x and y have the same class
if (x.getClass() == y.getClass()) {
// Code determines whether the objects have the same class
}
|
...