如何重载一些Groovy类型转换以避免NumberFormatException的try / catch? [英] How to overload some Groovy Type conversion for avoiding try/catch of NumberFormatException?

查看:521
本文介绍了如何重载一些Groovy类型转换以避免NumberFormatException的try / catch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我厌倦了用 try / catch 块封装每个 asType 的调用。

  def b =
def c
尝试{
c = b as整数
}
catch(NumberFormatException){
c = null
}
println c

而是我想在我的代码中写入以下内容:

  def b =
def c = b as Integer

如果 b 格式不正确,那么我希望将 null 分配给 c



那我该如何重载 asType 运算符的默认行为?



是吗?如果我为我的整个Grails应用程序执行操作会有风险吗?或者是简单地创建我自己的方法(如 asTypeSafe )并称之为最佳解决方案? Groovy / Grails是否对Groovy类型转换进行了一些配置调整?



编辑(对于实施答案感兴趣的人)
根据接受的答案,我将以下代码添加到我的bootstrap.groovy文件,它完美的工作。

  String.metaClass.asTypeSafe = {Class c  - > 
try {
delegate.asType(c)
}
catch(Exception){
return null
}
}

我把它称为如下:

  def myNum = myStr.asTypeSafe(Integer)


解决方案

您可以通过提供新的asType实现来覆盖默认行为。确保你保存旧的,并为其他你不想处理的课程打电话。例子:

  oldAsType = String.metaClass.getMetaMethod(asType,[Class] as Class [])
String.metaClass.asType = {Class c - >
if(c == Integer){
delegate.isInteger()? delegate.toInteger():null
} else {
oldAsType.invoke(delegate,c)
}
}

至于这是否是一个好主意,请记住很多对象将使用字符串,而且很有可能他们称这种转换为依赖于异常抛出。如果你从一个params对象中传入一个params对象,那么Grails域对象将完成大量的类型转换工作。控制器,但我认为它对这种事情没有任何形式的全局调整。


I am sick of encapsuling each call of asType with try/catch block like :

def b = ""
def c 
try {
    c = b as Integer
}
catch (NumberFormatException) {
    c = null
}
println c

instead I would like to write in my code the following:

def b = ""
def c = b as Integer

and if b is not well-formatted, then I want to have null be assigned to c

So how can I overload this default behavior for the asType operator ?

Is it risky if I do it for my entire Grails application? Or is the best solution to simply create a method of my own (like asTypeSafe) and call it ? Do Groovy/Grails have some configuration tweaks regarding Groovy Type conversion?

EDIT (for people interested in the implemented answer) Based on the accepted answer, I have added the following code to my bootstrap.groovy file and it works perfectly.

String.metaClass.asTypeSafe = {Class c ->
    try {
        delegate.asType(c)
    }
    catch (Exception) {
        return null
    }
}

and I call it as below:

def myNum = myStr.asTypeSafe(Integer)

解决方案

You can override the default behavior by providing a new asType implementation. Make sure you save the old one, and call it for other classes you don't want to handle yourself. Example:

oldAsType = String.metaClass.getMetaMethod("asType", [Class] as Class[])
String.metaClass.asType = { Class c ->
    if (c == Integer) { 
        delegate.isInteger() ? delegate.toInteger() : null
    } else {
        oldAsType.invoke(delegate, c)
    }
} 

As for whether this is a good idea, just remember that a lot of objects will be be using Strings and it's quite possible they call this conversion and rely on the exception being thrown. You're messing with things at quite a low level.

Grails domain objects will do a lot of the heavy lifting of type conversion if you pass in a params object from a controller, but I don't think it has any kind of global tweaks for this kind of thing.

这篇关于如何重载一些Groovy类型转换以避免NumberFormatException的try / catch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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