Groovy 如何将 GString 中的 char[] 转换为 String?

发布于 2025-01-03 22:42:53 字数 1259 浏览 1 评论 0原文

我试图弄清楚 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜柠檬 2025-01-10 22:42:53

浏览源代码,似乎相关的 该行位于源代码中。实例化 GString 时,它会创建一个值的 Object[],如果您获取 GString 上的 value 属性,您就可以看到该值:

char[] pchar = ['p', 'a', 's', 's']
pchar.values // [pass]

在 GString 源中,它最终将对象值(char[])传递给 InvokerHelper write()方法,它将值传递给 toString(object)。在 toString() 方法中,您会发现它与条件不匹配,最后将其传递给 format() 方法,在源代码行中检查参数是否为 char 数组,然后创建一个新的来自 char 数组的字符串。

    if (arguments.getClass().isArray()) {
        if (arguments instanceof char[]) {
            return new String((char[]) arguments);
        }
        return format(DefaultTypeTransformation.asCollection(arguments), verbose, maxSize);
    }

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:

char[] pchar = ['p', 'a', 's', 's']
pchar.values // [pass]

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.

    if (arguments.getClass().isArray()) {
        if (arguments instanceof char[]) {
            return new String((char[]) arguments);
        }
        return format(DefaultTypeTransformation.asCollection(arguments), verbose, maxSize);
    }
心凉怎暖 2025-01-10 22:42:53
char[] pchar = ['p', 'a', 's', 's']
assert pchar.join() == 'pass'
println "${pchar.join()}"
char[] pchar = ['p', 'a', 's', 's']
assert pchar.join() == 'pass'
println "${pchar.join()}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文