Java называется groovy script (с оценкой) и возвращает ошибку - PullRequest
0 голосов
/ 18 ноября 2018

Я потратил много времени, чтобы найти ответ.Я нашел что-то вроде решения здесь , но это было неправильно.Это не работает для меня.
Ситуация :
У меня есть 2 отличных скрипта и приложение Java.
Another.groovy (автотест / источники)

class Another
{
    protected String name="";
    public Another() {}
    public main(String[] args) {}
    public boolean getResult() {return true;}
    public String getName() {return name;}
    public void setName(String value) {name=value;}
}

test.groovy (автотест / кейсы)

evaluate(new File("autotest/sources/Another.groovy"))
import support.tool.AutotestResult;
public class Another2 extends Another
{
    public Another2()
    {
        this.setName(this.name+"N");
    }
    public AutotestResult run()
    {
        return new AutotestResult(this.name+"123",this.getResult(),null,null)
    }
}
Another2 a = new Another2()
a.run()

Javaкласс под названием "test.groovy"

String[] paths = {"autotest\\cases\\test.groovy"};
GroovyScriptEngine gse = new GroovyScriptEngine(paths);
Binding binding = new Binding();
binding.setVariable("args",null);
System.out.println(((AutotestResult)gse.run("test.groovy", binding)).toJSON());

Отлично работает, если Another.groovy и test.groovy находятся в одной папке.Но если Another.groovy находится в другой папке, он не работает.Java вернула ошибку:

Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
file:/.../autotest/cases/test.groovy: 6: unable to resolve class Another 
 @ line 6, column 1.
   public class Another2 extends Another
   ^

Поэтому у меня есть вопросы:

  • Может ли помочь совет?
  • Можно ли это сделать (Класс из одного скрипта расширяет классиз другого скрипта который где-то)?
  • Есть другой способ?

PS.Извините за плохой английский.

1 Ответ

0 голосов
/ 19 ноября 2018

Проблема решена с помощью импорта
Решение :

  1. Измените класс GroovyScriptEngine на класс GroovyShell
  2. Используя класс CompilerConfiguration и setClassPath ("корневая папка")
  3. Добавление пакета и импорт в скрипты

Another.groovy (автотест / исходники)

package sources
public class Another
{
    protected String name="AnotherNama";
    public Another() {}
    public main(String[] args) {}
    public boolean getResult() {return true;}
    public String getName() {return name;}
    public void setName(String value) {name=value;}
}

test.groovy(автотест / кейсы)

package cases

import support.tool.AutotestResult;
import sources.Another

public class Another2 extends Another
{
    public Another2()
    {
        this.setName(this.name+"N");
    }
    public AutotestResult run()
    {
        return new AutotestResult(this.name+"123",this.getResult(),null,null)
    }
}
Another2 a = new Another2()
a.run()

Java-код:

CompilerConfiguration config=new CompilerConfiguration();
config.setClasspath("autotest");
config.addCompilationCustomizers(new ImportCustomizer());
GroovyShell shell=new GroovyShell(config);
Binding binding = new Binding();
binding.setVariable("args",null);
System.out.println(((AutotestResult)shell.run(new File("autotest/cases/test.groovy"),new ArrayList())).toJSON());
...