简体   繁体   English

Java + Groovy脚本 - 继承

[英]Java + Groovy Script - Inheritance

I have a problem with inheritance in Groovy script. 我在Groovy脚本中有继承问题。 I want my Groovy script to inherit methods from Java class that I invoke this script. 我希望我的Groovy脚本从我调用此脚本的Java类继承方法。

For example, I have something like this: 例如,我有这样的事情:

public class SimpleTest extends TestCase {

public void test(){
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.setScriptBaseClass(this.getClass().getName());
    GroovyShell shell = new GroovyShell(this.getClass().getClassLoader(), new Binding(), configuration);
    shell.evaluate("println sayHello()");
}

public String sayHello(){
    return "Hello";
}
}

And the error is: 错误是:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 1: Declared type com.test.SimpleTest does not extend groovy.lang.Script class! org.codehaus.groovy.control.MultipleCompilationErrorsException:startup failed:Script1.groovy:1:声明类型com.test.SimpleTest不扩展groovy.lang.Script类! @ line 1, column 1. println sayHello() ^ 1 error @第1行,第1列.println sayHello()^ 1错误

How can I do this if I cannot inherit any other class? 如果我不能继承任何其他类,我怎么能这样做? I want to invoke method just only like from superclass. 我只想从超类中调用方法。

Edit 编辑

I changed my class to something like this: 我改变了我的课程:

public class CmTest extends TestCase {

public void test(){
    GroovyHandler handler = new GroovyHandler();
    handler.run();
}

public String sayHello(){
    return "Hello";
}

public class GroovyHandler extends Script {

    public GroovyHandler(){
    }

    @Override
    public Object run() {
        CompilerConfiguration configuration = new CompilerConfiguration();
        configuration.setScriptBaseClass(this.getClass().getName());
        GroovyShell shell = new GroovyShell(CmTest.class.getClassLoader(), new Binding(), configuration);
        return shell.evaluate("println sayHello()");
    }
}
}

Now the error is: 现在的错误是:

java.lang.NoSuchMethodError: com.test.SimpleTest$GroovyHandler: method < init >()V not found at Script1.(Script1.groovy) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308) at org.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:429) at groovy.lang.GroovyShell.parse(GroovyShell.java:704) at groovy.lang.GroovyShell.evaluate(GroovyShell.java:588) at groovy.lang.GroovyShell.evaluate(GroovyShell.java:627) at groovy.lang.GroovyShell.evaluate(GroovyShell.java:598) ... java.lang.NoSuchMethodError:com.test.SimpleTest $ GroovyHandler:方法<init>()在sun.reflect.NativeConstructorAccessConstructorAccessorImpl的sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)的Script1。(Script1.groovy)中找不到V。 newInstance(NativeConstructorAccessorImpl.java:39)位于java.lang.Class.newInstance(Delegator.java:513)的sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)java.lang.Class.newInstance0(Class .java:355)在java.lang.Class.newInstance(Class.java:308)atg.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:429)at groovy.lang.GroovyShell.parse(GroovyShell。 java:704)at groovy.lang.GroovyShell.evaluate(GroovyShell.java:588)at groovy.lang.GroovyShell.evaluate(GroovyShell.java:627)at groovy.lang.GroovyShell.evaluate(GroovyShell.java:598)。 ..

The class that is going to be used in the script must extend Script 将在脚本中使用的类必须扩展脚本

Example: 例:

class ScriptTest extends Script {
    def SayHello() {
        return "Hello"
    }
}

Then you setScriptBaseClass to this particular class 然后将setScriptBaseClass设置为此特定类

I have solved my problem. 我已经解决了我的问题。 The solution is class DelegatingScript. 解决方案是DelegatingScript类。 Link to doc: http://docs.groovy-lang.org/latest/html/gapi/groovy/util/DelegatingScript.html 链接到doc: http//docs.groovy-lang.org/latest/html/gapi/groovy/util/DelegatingScript.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM