title: GroovyClassLoading Mechanism
body: <p>I'm really newbie to groovy scripting but following some tutorial I tried to dynamically load some groovy class within my java code using <code>parseClass()</code> method of <code>GroovyClassLoader</code>.
  I wrote some snippet and it worked fine for me. The problem is that I don't clearly understand what groovy engine is doing beyond my view and how those scripts are compiled?<br>
  Does a new class gets creted and loaded into jvm? Or does my application uses some cached sources?</p>
  
  <p>Here is the class I'm trying to parse:</p>
  
  <pre><code>  private static class MyScript {
  
    @Override
    public String toString()
    {
       StringBuilder builder = new StringBuilder();
       builder.append("public class SomeClass\n");
       builder.append("{\n");
       builder.append("Some code...").append("\n");
       builder.append("}\n");
       return builder.toString();
    }
  </code></pre>
  
  <p>The I load it with <code>build()</code> as below:</p>
  
  <pre><code>private Class MyGroovyBuilder {
    private Script script = new Script();
    public String build() throws TemplateCompilationException
    //
    String groovyText = script.toString();
    //
    CompilerConfiguration config = new CompilerConfiguration();
    //
    byte[] bytes;
    try
    {
       bytes = groovyText.getBytes(config.getSourceEncoding());
    }
    catch (UnsupportedEncodingException e)
    {
       throw new TemplateCompilationException(e, groovyText);
    }
    //
    InputStream in = new ByteArrayInputStream(bytes);
    GroovyCodeSource gcs = new GroovyCodeSource(in, "SomeName", "/groovy/shell");
    GroovyClassLoader loader = new
    GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config);
    Class&lt;?&gt; scriptClass;
    try
    {
       scriptClass = loader.parseClass(gcs, false);
    }
    catch (CompilationFailedException e)
    {
       throw new GroovyCompilationException(e, "SomeName", groovyText);
    }
    catch (ClassFormatError e)
    {
       throw new GroovyCompilationException(e, "SomeName", groovyText);
    }
  return scriptClass.getName();
  </code></pre>
  
  <p>}</p>
  
  <p><br>Any clarification is greatelly appreciated.</p>
  
  <p>BR.</p>