java 嵌入groovy,Groovy嵌入在Java中,回拨给Java

博客讨论了在Java中使用GroovyClassLoader动态加载包含内部类的Groovy脚本时遇到的问题。作者指出,由于没有正确地限定内部类的名字,导致Groovy无法解析Fooable接口和ThingyDoodle类。解决方案是通过在脚本中添加包含类的名字前缀,如GroovyTest.Fooable和GroovyTest.ThingyDoodle,从而使脚本能够正确解析并运行。
摘要由CSDN通过智能技术生成

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.

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

^

解决方案

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" ;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值