[groovy-user] Quadruple weird typecast exception...

7 views
Skip to first unread message

OC

unread,
Jan 16, 2014, 4:38:34 PM1/16/14
to user@groovy.codehaus.org User
Hello there,

I've got the following code:

===
println "WTF rtypes '${rtypes.getClass()}'$rtypes key '${key.getClass()}'$key' desc '${desc.getClass()}'$desc'"
rtypes[key]=desc // this is ClassTransformerAgent.groovy:117
println ' success'
===

Note it's part of a class instrumentation; nevertheless _no class is actually changed anyhow_; the ClassFileTransformer.transform method consistently returns null (for testing purposes). This code works many times all right and then crashes in a completely weird way:

===
... ... ... long list of 1500-odd printouts, similar to the few two ones below (sometimes nonempty rtypes, too) ...
WTF rtypes 'class java.util.LinkedHashMap'[:] key 'class java.lang.String'paramString' desc 'class java.lang.String'()Ljava/lang/String;'
success
WTF rtypes 'class java.util.LinkedHashMap'[:] key 'class java.lang.String'paramString' desc 'class java.lang.String'()Ljava/lang/String;'
success
WTF rtypes 'class java.util.LinkedHashMap'[:] key 'class java.lang.String'dictionary' desc 'class java.lang.String'()Lcom/webobjects/foundation/NSDictionary;'
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '()Lcom/webobjects/foundation/NSDictionary;' with class 'java.lang.String' to class 'java.util.Map'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:371)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2516)
at groovy.lang.ExpandoMetaClass.setProperty(ExpandoMetaClass.java:1170)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3462)
at org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:195)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.putAt(DefaultGroovyMethods.java:251)
at org.codehaus.groovy.runtime.dgm$429.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:271)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at cz.ocs.javaagent.Accessorizer.visitMethod(ClassTransformerAgent.groovy:117)
at groovyjarjarasm.asm.ClassReader.b(Unknown Source)
...
===

Does not make any sense at all to me (and can't repeat it in a sample code outside of my app, which -- given in there it works perfectly 1500-odd times before it crashes does surprise). Why in the name of all saints should it cast the value being stored into a dictionary to a Map?

Might perhaps someone have any idea what might be the culprit, or at least, how to pursue the problem? Aside of logging out rtypes/key/desc (which I did as seen above, without any clue) I have no idea :( Out of desperation I've tried 'rtypes.putAt(key,desc)', which, as expected, did not change anything at all.

Thanks,
OC


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


OC

unread,
Jan 16, 2014, 5:32:05 PM1/16/14
to us...@groovy.codehaus.org
As a followup...
... I still have abso-bloomin'-lootely no idea what the H might be the culprit, but I've found a work-around -- the following code seems to work right:

===
try { //TODO: find why this terrible contraption is ever needed!
rtypes[key]=desc
} catch (org.codehaus.groovy.runtime.typehandling.GroovyCastException exc) {
def nrt=[(key):desc]
if (rtypes) nrt.putAll(rtypes)
rtypes=nrt
}
===

Jochen Theodorou

unread,
Jan 17, 2014, 6:42:10 AM1/17/14
to us...@groovy.codehaus.org
Am 16.01.2014 22:38, schrieb OC:
> Hello there,
>
> I've got the following code:
>
> ===
> println "WTF rtypes '${rtypes.getClass()}'$rtypes key '${key.getClass()}'$key' desc '${desc.getClass()}'$desc'"
> rtypes[key]=desc // this is ClassTransformerAgent.groovy:117
> println ' success'
> ===

I strongly hope that is not the agent from the other thread, because
then it would be an agent depending on Groovy ;)

> Note it's part of a class instrumentation; nevertheless _no class is actually changed anyhow_; the ClassFileTransformer.transform method consistently returns null (for testing purposes). This code works many times all right and then crashes in a completely weird way:
>
> ===
> ... ... ... long list of 1500-odd printouts, similar to the few two ones below (sometimes nonempty rtypes, too) ...
> WTF rtypes 'class java.util.LinkedHashMap'[:] key 'class java.lang.String'paramString' desc 'class java.lang.String'()Ljava/lang/String;'
> success
> WTF rtypes 'class java.util.LinkedHashMap'[:] key 'class java.lang.String'paramString' desc 'class java.lang.String'()Ljava/lang/String;'
> success
> WTF rtypes 'class java.util.LinkedHashMap'[:] key 'class java.lang.String'dictionary' desc 'class java.lang.String'()Lcom/webobjects/foundation/NSDictionary;'
> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '()Lcom/webobjects/foundation/NSDictionary;' with class 'java.lang.String' to class 'java.util.Map'
> at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:371)
> at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2516)
> at groovy.lang.ExpandoMetaClass.setProperty(ExpandoMetaClass.java:1170)
> at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3462)
> at org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:195)
> at org.codehaus.groovy.runtime.DefaultGroovyMethods.putAt(DefaultGroovyMethods.java:251)
> at org.codehaus.groovy.runtime.dgm$429.invoke(Unknown Source)
> at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:271)
> at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
> at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
> at cz.ocs.javaagent.Accessorizer.visitMethod(ClassTransformerAgent.groovy:117)
> at groovyjarjarasm.asm.ClassReader.b(Unknown Source)

hmmm... odd... let me check...

If you look at
https://github.com/groovy/groovy-core/blob/GROOVY_2_2_1/src/main/groovy/lang/MetaClassImpl.java?source=cc#L2514
you see the map handling code after the block in line 2514. But this
exception tells me, it entered the block at 2514, which means method is
not null.

The most likely case is that LinkedHashMap has a field name dictionary,
and that the code is trying to set that field, and that fiel is of type Map.

I noticed we go the InvokerHelper path mainly because
DefaultGroovyMethods has no Map#putAt(String,Object) method. There is a
Map#putAt(Object,Object) version, but Object#putAt(String,Object) will
be more specific and thus get selected instead.

so a fix you could try is easy, add an extension method for
Map#putAt(String,Object) which does the same as
https://github.com/groovy/groovy-core/blob/GROOVY_2_2_1/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java#L5846

If that fixes your problem, then please let us know.

bye blackdrag

--
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org

OC

unread,
Jan 17, 2014, 9:09:04 AM1/17/14
to us...@groovy.codehaus.org
Jochen,

> If that fixes your problem, then please let us know.

Indeed -- looks like that was the culprit; I've added the extension, and fix the problem it did.

Thanks again!
OC
Reply all
Reply to author
Forward
0 new messages