How to replace all String.equals with StringUtils.equals

I want to automatically replace all occurrences

if(variable!=null && variable.equals(value)) ... 

by

 if(StringUtils.equals(variable, value)) ... 

subject to type. This means that variable and value must be String (and not just lexicographic processing, for example, with awk / perl).

It should also be flexible enough to apply either a literal or a constant ( final static ) or another variable or parameter to value — something that will necessarily be String in this context. In addition, there may be more logical expressions.

I suspect the Java Std API may help here, but the conversion code itself is not handled by this API, but simply access to the code.

+1
source share
3 answers

If you use netbeans, you can use custom refactoring:

Menu Refactoring / Verification and Conversion / Overview / New / Editing Script:

 <!description="Convert to StringUtils.equals"> $var!=null && $var.equals($val) :: $var instanceof java.lang.String => StringUtils.equals($var, $val) ;; 
+1
source

IntelliJ IDEA Structural Search and Replace is capable of performing such transformations. See the online help for more information .

+3
source

I used JAPA , known as the Java parser. This is the best code change tool I've used. From reading one line in an if loop to saving it back to the same source, it has it all. Worth a try.

+2
source

All Articles