2015-05-21 7 views
5

Mam problem z dziedziczeniem w skrypcie Groovy. Chcę, aby mój skrypt Groovy dziedziczył metody z klasy Java, którą wywołuję w tym skrypcie.Java + Groovy Script - Inheritance

Na przykład, mam coś takiego:

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

a błąd jest:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 1: Declared type com.test.SimpleTest does not extend groovy.lang.Script class! @ line 1, column 1. println sayHello() ^ 1 error

Jak mogę to zrobić, jeśli nie można dziedziczyć inne klasy? Chcę wywołać metodę tylko jak z nadklasy.

Edit

zmieniłem klasę coś takiego:

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

Teraz tego błędu jest:

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) ...

Odpowiedz

0

Klasa, która ma być używana w skrypcie musi przedłużyć Script

Przykład:

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

Wtedy setScriptBaseClass do tej konkretnej klasy

+0

od powyższego jest imho odpowiedzi na pierwotne pytanie po prostu dodać tutaj: 'com.test.SimpleTest $ GroovyHandler' wskazuje, że klasa bazowa skrypt jest klasa wewnętrzna i zakładam go nie jest statyczną klasą wewnętrzną. W takim przypadku Java nie doda konstruktora bez parametrów, ponieważ klasa wewnętrzna musi pobrać zewnętrzną instancję klasy. Stwórz więc klasę statyczną i wszystko jest w porządku – blackdrag