groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Winnebeck, Jason" <Jason.Winneb...@windstream.com>
Subject RE: ShortTypeHandling castToString
Date Wed, 15 Jun 2016 12:36:55 GMT
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:paulk@asert.com.au] 
Sent: Wednesday, June 15, 2016 12:32 AM
To: users@groovy.apache.org
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 <Jason.Winnebeck@windstream.com> 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.
Mime
View raw message