如何用StringUtils.equals替换所有String.equals [英] How to replace all String.equals by StringUtils.equals

查看:1893
本文介绍了如何用StringUtils.equals替换所有String.equals的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动替换所有出现的

I want to automatically replace all occurrences of

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

by

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

以类型感知的方式。这意味着,变量确定字符串(不仅仅是字典处理,例如使用awk / perl)。

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).

此外,它应该足够灵活,可以申请 value 作为文字和常量(最终静态)或其他变量或参数 - 任何肯定是<在此上下文中code> String 。此外,可能还有更多的布尔表达式。

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.

我怀疑 Java Std API 可以在这里提供帮助,但代码转换本身不是由该API处理的,只是访问代码。

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.

推荐答案

如果您使用netbeans,您可以使用自定义重构:

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)
;;

这篇关于如何用StringUtils.equals替换所有String.equals的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆