This rule is Obsolete | |
Synopsis: | Invoke equals() on the object you've already ensured is not null |
Language: | Java |
Severity Level: | 2 |
Category: | Basic |
Description: |
After checking an object reference for null, you should invoke equals() on that object rather than passing it to another object's equals() method.
public class Test { public void method(String a, String b) { if (a != null && method1().equals(a)) { // will trigger the rule, because "a" is certainly not null //whatever } if (method1().equals(a)) { // won't trigger the rule, nothing is known about "a" or "method1" //whatever } if ("LITERAL".equals(a)) { // won't trigger the rule, because "LITERAL" is non-null by definition //whatever } if (a.equals("LITERAL")) { // will trigger the rule, because nothing is known about "a" //whatever } } } |