Groovy嵌入在Java中,回拨给Java [英] Groovy, embedded in Java, calling back to Java

查看:96
本文介绍了Groovy嵌入在Java中,回拨给Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我必须做的事情只是大脑死亡愚蠢的这不起作用,但我处于一种情况下,我想动态地加载到正在运行的服务器的行为。我选择groovy作为我的工具来做到这一点。行为将需要引用服务器类路径上的类,比如我的模型对象以及像Freemarker这样的第三方库。

I know I have to be doing something just brain-dead stupid for this not to work, but I'm in a situation where I want to dynamically load behavior into a running server. I chose groovy as my tool to do this. The behavior will need to reference classes on the server's classpath, such as my model objects as well as third party libraries like Freemarker.

我把这个愚蠢的POC扔在一起来展示可行性。尽管我将GroovyClassPath的父类路径设置为当前,但我无法获得groovy脚本来解决Java类ThingyDoodle和Fooable。

I threw together this silly POC to show feasibility. I can't get the groovy script to resolve the Java classes "ThingyDoodle" and "Fooable", in spite of the fact that I am setting the parent classpath of the GroovyClassPath to be my current.

public class GroovyTest
{
    public static void main ( String [ ] argv ) throws Throwable
    {
            // Setting parent classloader!
            // also tried plain old "GroovyTest.class.getClassLoader()" as well
        GroovyClassLoader gcl = new GroovyClassLoader ( Thread.currentThread().getContextClassLoader() ) ;
        String src =
              "class Arf implements Fooable {
                 public String foo ( ) {
                   return new ThingyDoodle().doStuff('Arf');}}" ;
        Class clazz = gcl.parseClass(src, "AppleSauce.groovy");
        Object aScript = clazz.newInstance();
        Fooable myObject = (Fooable) aScript;
        System.out.println ( myObject.foo() ) ;
    }

    public static interface Fooable { public String foo ( ) ; }

    public static class ThingyDoodle
    {
        public String doStuff ( String input )
        {
            return "Hi Worlds" ;
        }
    }
}

我在做什么

What the heck am I doing wrong here?

Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
AppleSauce.groovy: 1: unable to resolve class Fooable
 @ line 1, column 1.
   class Arf implements Fooable { public String foo ( ) { return new ThingyDoodle().doStuff('Arf');}}
   ^

AppleSauce.groovy: 1: unable to resolve class ThingyDoodle
 @ line 1, column 63.
   ublic String foo ( ) { return new Thingy
                                     ^


推荐答案

代码中的问题是 Fooable 接口和 ThingyDoodle 类无法找到,因为它们都是内部类,需要使用包含的类名进行限定,即 GroovyTest 。我在嵌入脚本中限定了两个名字(并且修复了脚本中的引号),并且按预期运行。

The problem in your code is that the Fooable interface and ThingyDoodle class cannot be found because they're both internal classes and need to be qualified with the containing class name, i.e. GroovyTest. I qualified both names in the embedded script (and fixed the quotes around the script) and it ran as expected.

import groovy.lang.GroovyClassLoader;

public class GroovyTest
{

    public static void main ( String [ ] argv ) throws Throwable
    {
            // Setting parent classloader!
            // also tried plain old "GroovyTest.class.getClassLoader()" as well
        GroovyClassLoader gcl = new GroovyClassLoader ( Thread.currentThread().getContextClassLoader() ) ;
        String src =
              "class Arf implements GroovyTest.Fooable { " + 
                 "public String foo ( ) { " + 
                   "return new GroovyTest.ThingyDoodle().doStuff('Arf');}}" ;
        Class clazz = gcl.parseClass(src, "AppleSauce.groovy");
        Object aScript = clazz.newInstance();
        Fooable myObject = (Fooable) aScript;
        System.out.println ( myObject.foo() ) ;
    }       
     public static interface Fooable { public String foo ( ) ; }

    public static class ThingyDoodle
    {
        public String doStuff ( String input )
        {
            return "Hi Worlds" ;
        }
    }


}

这篇关于Groovy嵌入在Java中,回拨给Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆