GroovyClassLoading机制

GroovyClassLoading机制

本文介绍了GroovyClassLoading机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是groovy脚本的新手,但是在完成一些教程之后,我尝试使用GroovyClassLoaderparseClass()方法在Java代码中动态加载一些groovy类.我写了一些代码片段,对我来说效果很好.问题是我无法清楚地理解groovy引擎在做什么,以及这些脚本是如何编译的?
是否会创建新类并将其加载到jvm中?还是我的应用程序使用了某些缓存的源?

I'm really newbie to groovy scripting but following some tutorial I tried to dynamically load some groovy class within my java code using parseClass() method of GroovyClassLoader.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?
Does a new class gets creted and loaded into jvm? Or does my application uses some cached sources?

这是我要解析的课程:

  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();
  }

我用build()加载它,如下所示:

The I load it with build() as below:

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<?> 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();

}


非常感谢任何澄清.


Any clarification is greatelly appreciated.

BR.

推荐答案

在加载类之后,它会出现在您的类加载器中,并且可以像其他任何类一样进行访问.

After loading class it appears in your class loader, and can be accessed like any other class.

有一个简单的教程[此处],说明如何从字符串加载类.

There is a simple tutorial [here], that show how to load class from string.

在最简单的情况下,您可以加载类并保留其Class对象,并使用它动态创建对象.对于字段访问或方法调用,您可以依靠Groovy动态性质.

In simplest case, you can load class, and hold it's Class object, using it to create objects dynamically. For field access or method invokation you can rely on Groovy dynamic nature.

在幕后没有像这样的缓存源"或其他东西,您可以忘记从何处加载了类.您还可以缓存已编译的类,并将其保存在某处,如[此处].如果您需要经常加载同一个类,它将大大提高性能.

There is no "cached source" or smth like that behind the scene and you can forget, from where your class is loaded. You can also cache classes, that are already compiled, and save them somewhere, as described [here]. It will drastically improve performance, if you need to load same class often.

但是,深入研究主题会更好,因为动态类加载是高级Java/Groovy技术,它是链式类加载器的整个基础结构,因此最好参考有关它们的文档.

But it will be better, to dig down in topic, because dynamic class loading is advanced Java/Groovy technique, it's whole infrastructure of chained classloaders, so it's better to refer documentation about them.

下面的链接可能会有所帮助.

Links below may be helpful.

http://javarevisited.blogspot.ru/2012/12/how-classloader-works-in-java.html

如何使用URLClassLoader加载* .class文件?

这篇关于GroovyClassLoading机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:46