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.

4

1 回答 1

0

不确定这是解决方案,但请尝试:

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

}

于 2012-04-11T15:41:22.390 回答