OK, I see the PR. I would be interested to hear Jochen's comments because the way the class is documented it looks like that class is meant to be called in situations with constrained conditions. I mean, doesn't the normal String cast in Groovy go through DefaultGroovyMethods.asType? I see in that method there is a specialization when casting to String that calls InvokerHelper.toString, which has a lot of logic in it. For your issue GROOVY-7853 around primitive arrays I see that asType goes to InvokerHelper.toString, which calls InvokerHelper.format, which sees its class "isArray()" is true, then it has a specialization for char[] but if it's not char[] it casts the array to a collection and invokes Invoker.format on that -- which seems to go through a lot of effort casting everything in there recursively to a String when everything is guaranteed to be a primitive at that point.
In that sense, even the fix provided in PR 345 doesn't match the standard Groovy cast to String operator, which recursively formats the elements in the array, instead you just use toString. But I also wonder if the real bug is that the compiler generates ShortTypeHandling calls when it should be reverting to the full asType instead. Jason -----Original Message----- From: Paul King [mailto:[email protected]] Sent: Wednesday, June 15, 2016 12:32 AM To: [email protected] Subject: Re: ShortTypeHandling castToString Yes, it's clearly a bug as can be seen by running an example such as below: String foo() { Integer } foo() // ClassCastException Whereas 'Integer as String' works fine. I have a fix for this in PR#345 as part of GROOVY-7853, see: https://github.com/apache/groovy/pull/345 Just waiting for to see if Cédric/Jochen have any comments on the other parts of that PR since that is an area of the codebase that I am a little less familiar with. Cheers, Paul. On Wed, Jun 15, 2016 at 1:29 AM, Winnebeck, Jason <[email protected]> wrote: > I came across this method in Groovy 2.4.6 in ShortTypeHandling class, > I’ve never seen it fail but it doesn’t see right to me: > > > > public static String castToString(Object object) { > if (object==null) return null; > if (object instanceof Class) return (String) object; > return object.toString(); > } > > > > How can cast to String work if the object is a Class? > > > > Jason > > > > ________________________________ > This email message and any attachments are for the sole use of the > intended recipient(s). Any unauthorized review, use, disclosure or > distribution is prohibited. If you are not the intended recipient, > please contact the sender by reply email and destroy all copies of the > original message and any attachments. ---------------------------------------------------------------------- This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.
