Groovy 如何将 GString 中的 char[] 转换为 String?
我试图弄清楚 Groovy 如何将 GString
中的 char[]
转换为 String
。
示例:
char[] pchar = ['p', 'a', 's', 's']
println "$pchar"
结果:
pass
起初我假设它会在 char[] 上使用 toString() 方法(http://groovy.codehaus.org/groovy-jdk/primitive-types/char[] .html#toString())。但运行以下代码的结果似乎表明不然:
char[] pchar = ['p', 'a', 's', 's']
println "$pchar"
pchar.class.metaClass.toString = {->
"****"
}
println pchar.toString()
println "$pchar"
结果:
pass
****
pass
我也尝试过重写 invokeMethod()
尝试找出结果无济于事:
char[] pchar = ['p', 'a', 's', 's']
println "$pchar"
pchar.class.metaClass.toString = {->
"****"
}
pchar.class.metaClass.invokeMethod = {String methodName, Object arguments ->
println("Method called on ${delegate.class}: $methodName, $arguments")
def metaMethod = delegate.metaClass.getMetaMethod(methodName)
return metaMethod.invoke(delegate, arguments)
}
println pchar.toString()
println "$pchar"
结果:
pass
在类 [C: toString, [] 上调用的方法
****
pass
有谁知道 Groovy 是如何进行这种转变的?
I'm trying to figure out how Groovy translates a char[]
to a String
within a GString
.
Example:
char[] pchar = ['p', 'a', 's', 's']
println "$pchar"
Result:
pass
At first I assumed it would use the toString() method on char[] (http://groovy.codehaus.org/groovy-jdk/primitive-types/char[].html#toString()). But the results of running the following code seems to suggest otherwise:
char[] pchar = ['p', 'a', 's', 's']
println "$pchar"
pchar.class.metaClass.toString = {->
"****"
}
println pchar.toString()
println "$pchar"
Result:
pass
****
pass
I've also tried overriding invokeMethod()
to try figuring it out to no avail:
char[] pchar = ['p', 'a', 's', 's']
println "$pchar"
pchar.class.metaClass.toString = {->
"****"
}
pchar.class.metaClass.invokeMethod = {String methodName, Object arguments ->
println("Method called on ${delegate.class}: $methodName, $arguments")
def metaMethod = delegate.metaClass.getMetaMethod(methodName)
return metaMethod.invoke(delegate, arguments)
}
println pchar.toString()
println "$pchar"
Result:
pass
Method called on class [C: toString, []
****
pass
Does anyone know how Groovy does this transformation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
浏览源代码,似乎相关的 该行位于源代码中。实例化 GString 时,它会创建一个值的 Object[],如果您获取 GString 上的 value 属性,您就可以看到该值:
在 GString 源中,它最终将对象值(char[])传递给 InvokerHelper write()方法,它将值传递给 toString(object)。在 toString() 方法中,您会发现它与条件不匹配,最后将其传递给 format() 方法,在源代码行中检查参数是否为 char 数组,然后创建一个新的来自 char 数组的字符串。
Browsing through the source, it would appear that the pertinent line is in the source. When instantiating the GString, it creates an Object[] of values that you can see if you get the values property on the GString:
In the GString source, it eventually passes the object value (the char[]) to the InvokerHelper write() method, which passes the value to toString(object). In the toString() method you'll find that it doesn't match the criteria and finally passes it to the format() method where on the line in the source it checks if the argument is an array of char and then creates a new String from the char array.