4

我知道我必须做一些愚蠢的事情才能让它不起作用,但我处于一种我想将行为动态加载到正在运行的服务器中的情况。我选择 groovy 作为我的工具来做到这一点。该行为将需要引用服务器类路径上的类,例如我的模型对象以及 Freemarker 等第三方库。

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

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

我到底做错了什么?

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
                                     ^
4

2 回答 2

3

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

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


}
于 2012-04-26T10:38:49.127 回答
2

不知道你是如何编译它的......我试过这个(使用Gradle作为构建工具)并且它工作......

./build.gradle

apply plugin:'application'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.codehaus.groovy', name: 'groovy', version: '1.8.6'
}

mainClassName = 'org.timyates.GroovyTest'

./src/main/java/org/timyates/Fooable.java

package org.timyates ;

public interface Fooable {
  public String foo() ;
}

./src/main/java/org/timyates/ThingyDoodle.java

package org.timyates ;

public class ThingyDoodle {
  public String doStuff( String input ) {
    return String.format( "Hi %s", input ) ;
  }
}

./src/main/java/org/timyates/GroovyTest.java

package org.timyates ;

import groovy.lang.GroovyClassLoader ;

public class GroovyTest {
  public static void main( String[] args ) throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader( GroovyTest.class.getClassLoader() ) ;
    String src = "import org.timyates.*\n" +
                 "class Arf implements Fooable {\n" +
                 "  public String foo() {\n" +
                 "    new ThingyDoodle().doStuff( 'tim' )\n" +
                 "  }\n" +
                 "}" ;
    Class clazz = gcl.parseClass( src, "AppleSauce.groovy" ) ;
    Fooable myObject = (Fooable)clazz.newInstance() ;
    System.out.println( myObject.foo() ) ;
  }
}

然后,当我gradle run在命令行输入时,我得到输出:

$ gradle run
:compileJava
:processResources UP-TO-DATE
:classes
:run
Hi tim

BUILD SUCCESSFUL
于 2012-04-26T08:07:03.610 回答