[ 
https://issues.apache.org/jira/browse/GROOVY-4214?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16616624#comment-16616624
 ] 

Hari Krishna Dara commented on GROOVY-4214:
-------------------------------------------

I just bumped into this same issue. I was trying to load another script and 
have methods defined in the other script appear in the local script's 
namespace, similar to the one in the description. I can verify that it works if 
I copy methods individually, so this seems to be a bug in {{metaClass.mixin}}. 
In the sample from description, instead of {{this.metaClass.mixin script}}, if 
you copy individual methods, e.g., {{this.metaClass.say = script.&say}}, then 
it would work fine. I got the below generalization to work, in case anyone is 
looking for a workaround:

{noformat}
script.metaClass.methods.each {
    if (it.declaringClass.getTheClass() == script.class && ! 
it.name.contains('$') && it.name != 'main' && it.name != 'run') {
        this.metaClass."$it.name" = script.&"$it.name"
    }
}
{noformat}

> StackOverflowError tyring to mixin loaded script
> ------------------------------------------------
>
>                 Key: GROOVY-4214
>                 URL: https://issues.apache.org/jira/browse/GROOVY-4214
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>            Reporter: Ittay Dror
>            Priority: Major
>              Labels: mixin
>
> In groovyConsole I have this:
> {code}
> import groovy.util.*
> import org.codehaus.groovy.runtime.*
> def gse = new GroovyScriptEngine("c:\\temp")
> def script = gse.loadScriptByName("say.groovy")
> this.metaClass.mixin script
> say("bye")
> {code}
> say.groovy contains
> {code}
> def say(String msg) {
>   println(msg)
> }
> {code}
> I get:
> {noformat}
> java.lang.StackOverflowError
>       at sun.reflect.GeneratedMethodAccessor548.invoke(Unknown Source)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>       at java.lang.reflect.Method.invoke(Method.java:597)
>       at 
> org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
>       at 
> org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod.invoke(MixinInstanceMetaMethod.java:38)
>       at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:913)
>       at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
>       at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:706)
>       at 
> org.codehaus.groovy.runtime.metaclass.OwnedMetaClass.invokeMethod(OwnedMetaClass.java:97)
>       at 
> groovy.lang.GroovyObjectSupport.invokeMethod(GroovyObjectSupport.java:44)
>       at groovy.lang.Script.invokeMethod(Script.java:78)
>       at sun.reflect.GeneratedMethodAccessor548.invoke(Unknown Source)
>         ...
> {noformat}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to