0

EDIT : I edited my post at the end to give the regex I used

Having sonar to monitor quality of a code is great, but on the downside, with crappy project, you have to correct a lot. My problem is that I need to correct about 500 "Equals avoid Null" violations from checkstyle ( com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck ). With one regex to correct it semi automatically would be great.

The goal of this violation is to change

myObject.getMyMember().toString().equals("one string")

to

"one string".equals(myObject.getMyMember().toString())

EDIT : I used this way and it worked well even if I need to check it and not launch a sed on my entire source tree

The regex to match the total line

([\(|& \t!])([^\(|& \t!])([a-zA-Z0-9_\[\]\(\)\.]*)\.(equals|equalsIgnoreCase)\(("[^"]*")\)

The regex to replace the line for

\1\5.\4(\2\3)

It took me a day to correct the 500 violations. Still a lot of work, but it would have been more painful if I had to do it by hand.

Dolanor
  • 822
  • 9
  • 19
  • 2
    It doesn't avoid the null checks. A NullPointerException would still be thrown. Think about it again, you will see why. – Martijn Courteaux Apr 11 '12 at 15:12
  • 1
    don't take short cuts - treat the exercise in tedium as penance for your sins. In any event, you still need null checks on `myObject` and `myObject.getMyMember()` ... – Alnitak Apr 11 '12 at 15:12
  • Right Matijn and Alnitak, I don't know why I said that ... Anyway, I don't like penitence which should be taken by others :p – Dolanor Apr 11 '12 at 15:14
  • @beny23 yes, you would. Just because `.equals()` can take an object doesn't mean that object will have its `.toString()` method invoked automatically. – Alnitak Apr 11 '12 at 15:20

1 Answers1

0

Not certain it is the solution but try :

final String sTmp = myObject.getMyMember();
    if(null != sTmp) {<br>
    // Test .equals() here <br>
    ..<br>

}

cl-r
  • 1,264
  • 1
  • 12
  • 26