title: Cannot dynamically change or add methods in a class using GroovyClassLoader
body: <p>I try to dynamically change and add methods of a class defined in a groovy script from another groovy script but cannot figure out why it works if I use the classname directly in .metaClass. but not if I load the class using the GroovyClassLoader(which I need to do!).</p>
  
  <p>In one file 'MyTest.groovy' I have</p>
  
  <pre><code>class MyTest {
  
     public void setUp() {
        println "SET UP"
     }
     public void tearDown() {
        println "TEAR DOWN"
     }
     public void testA() {
        println "testA"
     }
  }
  </code></pre>
  
  <p>and another file 'suite.groovy' contains</p>
  
  <pre><code>#!/usr/bin/env groovy
  
  public class MyTestSuite {
     // For debug
     public static printClassLoader(Class cls) {
        ClassLoader loader = cls.classLoader
        while (loader) {
           println loader.class
           println loader.URLs.join("\n")
           println "\n\n"
           loader = loader.parent
        }
     }
  
     public static void suite()  {
        // First method to define my class (change/addition of methods works)
        //Class testClass = MyTest
  
        // Second way to define my class (change/addition of methods doesn't work)
        ClassLoader parent = MyTestSuite.class.getClassLoader();
        GroovyClassLoader gcl = new GroovyClassLoader(parent);
        Class testClass = gcl.parseClass(new File("MyTest.groovy"));
  
        printClassLoader(testClass)
  
        testClass.metaClass.setUp = {-&gt; println "I'm your new setUp()" ;}
        testClass.metaClass.newTest = {-&gt; println "I'm your new newTest()" ; }
     }
  }
  
  MyTestSuite.suite()
  MyTest aTest = new MyTest()
  aTest.setUp()
  aTest.newTest()
  </code></pre>
  
  <p>With the first method I get the expected result:</p>
  
  <pre><code>I'm your new setUp()
  I'm your new newTest()
  </code></pre>
  
  <p>But with the second one I get</p>
  
  <pre><code>SET UP
  Caught: groovy.lang.MissingMethodException: No signature of method: MyTest.newTest() is applicable for argument types: () values: []
  Possible solutions: testA(), getAt(java.lang.String), wait(), inspect(), every()
      at suite.run(suite.groovy:34)
  </code></pre>
  
  <p>Interestingly modifying an existing method doesn't work but there is no exception but trying to add a new one throws one.
  I reckon it has to do something with the class loaders used but couldn't figure what exactly and what to change!
  For the first version here are the class loaders called:</p>
  
  <pre><code>class groovy.lang.GroovyClassLoader$InnerLoader
  class groovy.lang.GroovyClassLoader
  class org.codehaus.groovy.tools.RootLoader
  class sun.misc.Launcher$AppClassLoader
  class sun.misc.Launcher$ExtClassLoader
  </code></pre>
  
  <p>For the second version:</p>
  
  <pre><code>class groovy.lang.GroovyClassLoader$InnerLoader
  class groovy.lang.GroovyClassLoader
  class groovy.lang.GroovyClassLoader$InnerLoader
  class groovy.lang.GroovyClassLoader
  class org.codehaus.groovy.tools.RootLoader
  class sun.misc.Launcher$AppClassLoader
  class sun.misc.Launcher$ExtClassLoader
  </code></pre>
  
  <p>Any idea welcome!<br>
  Franck</p>