How to replace all String.equals by StringUtils.equals

你说的曾经没有我的故事 提交于 2019-12-08 03:35:05

问题


I want to automatically replace all occurrences of

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

by

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

in a type-aware manner. That means, that variable and value should be sure to be String (not just lexicographical processing, eg. with awk/perl).

Also it should be flexible enough to apply for value as literal as well as a constant (final static) or another variable or parameter -- anything that is sure to be a String in this context. Also, there may be more boolean expressions following.

I suspect that Java Std API may help here, but the code-transformation itself is not handled by that API, just the access to the code.


回答1:


If you are using netbeans you can use a custom refactoring:

Menu Refactor/Inspect and Transform/Browse/New/Edit Script:

<!description="Convert to StringUtils.equals">

$var!=null && $var.equals($val) :: $var instanceof java.lang.String
=> StringUtils.equals($var, $val)
;;



回答2:


IntelliJ IDEA's Structural Search and Replace feature is able to make such transformations. See the Web help for more information.




回答3:


I have used JAPA known as Java parser. This is best code change tool i used. From reading a single line in if loop till saving it back to same source it has everything. Worth trying.



来源:https://stackoverflow.com/questions/28851648/how-to-replace-all-string-equals-by-stringutils-equals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!