反思在Groovy脚本中声明的类

时间:2015-01-20 10:15:07

标签: java groovy

有没有办法获取在Groovy脚本中声明的类的反射数据,该脚本已经通过GroovyClassLoader parseClass方法进行了评估?

我究竟如何从java代码创建Customer实例?

package project

public class Customer extends Entity {
    public String name
    String description
    Date updateDate
}

def run() {
    Workflow workflow = new Workflow();
}

1 个答案:

答案 0 :(得分:0)

GroovyClassLoader gcl = new GroovyClassLoader();

Class compiledClass = gcl.parseClass(src);

// usually we wait for "contract" class implemented in groovy, i.e Entity
Entity groovyEntity = (Entity ) compiledClass.newInstance();

// as yet, we don't know, implementation has name

String name = compiledClass.getName();