简体   繁体   English

如何使用正则表达式替换从checkstyle纠正“等于避免为空”

[英]How to use a regex replace to correct “Equals avoid Null” from checkstyle

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 ). 我的问题是我需要从checkstyle(com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck)中纠正大约500个“等于避免Null”冲突。 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 编辑:我用这种方式,即使我需要检查它并且不在我的整个源代码树上启动sed,它也能很好地工作

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. 我花了一天的时间来纠正500个违规情况。 Still a lot of work, but it would have been more painful if I had to do it by hand. 仍然有很多工作要做,但是如果我必须手动完成,那会更加痛苦。

Not certain it is the solution but try : 不确定这是解决方案,但尝试:

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM