首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GroovyScriptEngine无法加载正在运行的groovy脚本的导入

GroovyScriptEngine无法加载正在运行的groovy脚本的导入
EN

Stack Overflow用户
提问于 2014-06-08 18:24:16
回答 1查看 1.7K关注 0票数 1

背景:

我最近开始使用Groovy,并试图在eclipse插件中嵌入一个groovy脚本引擎,以便我的客户在基于eclipse的产品中开发自己的GUI扩展。这与在可德豪斯网站上发布的http://groovy.codehaus.org/Integrating+Groovy+in+an+application+-+a+success+story非常相似。

问题

在尝试加载groovy类( "main_eclipse.groovy")时,GroovyScriptEngine抛出从eclipse插件运行的groovy脚本(我们称之为“SwtGuiBuilder”),并出现以下错误:

虫子!在已经迭代的同时排队新的源。排队的源是'file:/home/nicolas/workspace/groovy-test/src/gui/SwtGuiBuilder.groovy‘

问题

有人遇到过同样的问题吗?怎么能修好呢?任何帮助都将不胜感激!

一些意见:

  • 当使用groovy解释器而不是GroovyScriptEngine java对象时,使用SwtGuiBuilder类没有问题(请参阅下面的脚本"main_groovy“)。
  • 我的问题似乎不是类路径问题,因为在抛出的异常中提到了包含我的SwtGuiBuilder类的文件。
  • 在两个报告的groovy错误中提到了错误消息,GRECLIPSE-429和GRECLIPSE-1037。我没有完全了解技术细节,但这些but似乎与加载大量类时的性能问题有关,这与我的情况无关.

详细信息

SampleView.java

代码语言:javascript
复制
public class SampleView
{
public SampleView() { super(); }

public void createPartControl(Composite parent) 
{
    String    groovyScript = null;
    String [] groovyPath   = null;

    boolean shall_exit = false;
    do
    {      // ask user for params
        GroovyLocationDialog groovyLocationDialog= new GroovyLocationDialog(parent.getShell() );
        int return_code = groovyLocationDialog.open();
        if ( return_code != Window.OK )
            shall_exit = true;
        else 
        {
            groovyScript= groovyLocationDialog.getInputScriptName();
            groovyPath  = groovyLocationDialog.getInputScriptPath();

            // run it
            ScriptConnector scriptConnector = new ScriptConnector(parent);
            try                 { scriptConnector.runGuiComponentScript( groovyPath, groovyScript); }
            catch (Exception e) { e.printStackTrace(); }
            System.out.println("script finished");
        }
    }
    while ( ! shall_exit );
}

ScriptConnector.java

代码语言:javascript
复制
public class ScriptConnector
{
    private String[]  roots;
    private Composite window;
    private Binding   binding;

    public ScriptConnector( Composite window )
    {
         this.window    = window;
         Binding  scriptenv = new Binding();    // A new Binding is created ...
         scriptenv.setVariable("SDE", this); 
         scriptenv.setVariable("WINDOW", this.window); // ref to current window

         this.binding = scriptenv;
    }

    public void runGuiComponentScript(final String[] groovyPath, final String scriptName) 
    {
        GroovyScriptEngine gse = null;
        this.roots     = groovyPath;
        try 
        {
            // instanciating the script engine with current classpath
            gse = new GroovyScriptEngine( roots, this.getClass().getClassLoader() );
            gse.run(scriptName, binding);      // ... and run specified script
        }
        catch (Exception e) { e.printStackTrace(); }
        catch (Throwable t) { t.printStackTrace(); }
    }
}

main_eclipse.groovy

代码语言:javascript
复制
package launcher;

import org.eclipse.swt.SWT
import org.eclipse.swt.widgets.*
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.RowLayout as Layout

// This import will fail...
import gui.SwtGuiBuilder;


WINDOW.layout = new Layout(SWT.VERTICAL);
def builder = new SwtGuiBuilder(WINDOW);
builder.Label ( style=SWT.NONE, text = 'Simple demo of Groovy and SWT')
builder.Button( style=SWT.PUSH, text = 'Click me' , action = { println "Click !" } )

SwtGuiBuilder.groovy

代码语言:javascript
复制
package gui;

import org.eclipse.swt.events.*
import org.eclipse.swt.widgets.Button
import org.eclipse.swt.widgets.Composite
import org.eclipse.swt.widgets.Label


class SwtGuiBuilder
{
    private Composite _parent

    public SwtGuiBuilder(Composite parent) { _parent = parent }

    public void Button( style = SWT.PUSH, text= null, action = null )
    {
        def btn = new Button(_parent, style)
        if ( text != null )
            btn.text = text
        if (action != null)
            btn.addSelectionListener( new SelectionAdapter() { void widgetSelected( SelectionEvent event ) { action(); } } );
    }

    public void Label( style = SWT.NONE, text = '' )
    {
        def lbl = new Label(_parent, style)
        lbl.text = text
    }
}

main_groovy.groovy

代码语言:javascript
复制
package launcher;

import org.eclipse.swt.SWT
import org.eclipse.swt.widgets.*
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.RowLayout as Layout

// ... But this import is handled properly !
import gui.SwtGuiBuilder;

def display = new Display()
def WINDOW = new Shell(display)
WINDOW.text = 'Groovy / SWT Test';

WINDOW.layout = new Layout(SWT.VERTICAL);
def builder = new SwtGuiBuilder(WINDOW);
builder.Label ( style=SWT.NONE, text = 'Simple demo of Groovy and SWT')
builder.Button( style=SWT.PUSH, text = 'Click me' , action = { println "Ya clicked me !" } )

WINDOW.pack();
WINDOW.open();

while (!WINDOW.disposed) {
    if (!WINDOW.display.readAndDispatch())
        WINDOW.display.sleep();
}

堆栈跟踪

虫子!在已经迭代的同时排队新的源。排队的源是'file:/home/nicolas/workspace/groovy-test/src/gui/SwtGuiBuilder.groovy‘at org.codehaus.groovy.control.CompilationUnit.addSource(CompilationUnit.java:460) at org.codehaus.groovy.control.CompilationUnit.addSource(CompilationUnit.java:433) at groovy.util.GroovyScriptEngine$ScriptClassLoader$3.findClassNode(GroovyScriptEngine.java:195) at org.codehaus.groovy.control.ClassNodeResolver.resolveName(ClassNodeResolver.java:124) at org.codehaus.groovy.control.ResolveVisitor.resolveToOuter(在org.codehaus.groovy.control.ResolveVisitor.resolve(ResolveVisitor.java:377),org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1407),org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:202),org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:713),org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:1015),org.codehaus.groovy。control.CompilationUnit.doPhaseOperation(CompilationUnit.java:647) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:596) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:279) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:258) at groovy.util.GroovyScriptEngine$ScriptClassLoader.doParseClass(GroovyScriptEngine.java:247) at groovy.util.GroovyScriptEngine$ScriptClassLoader.parseClass(GroovyScriptEngine.java:229) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:244)在groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:202) at groovy.util.GroovyScriptEngine.loadScriptByName(GroovyScriptEngine.java:514) at groovy.util.GroovyScriptEngine.createScript(GroovyScriptEngine.java:564) at groovy.util.GroovyScriptEngine.run(GroovyScriptEngine.java:551)

我的配置:

  • LinuxUbuntu14.04 x86
  • Groovy版本: 2.3.2
  • JVM: 1.7.0_55
  • 日食开普勒SR2 -构建20140224-0627
  • Eclipse插件v2.0.7
EN

回答 1

Stack Overflow用户

发布于 2014-06-09 19:12:54

我没有使用GroovyScriptEngine,而是使用了GroovyShell类(下面是groovy代码,但很容易更改为java),CompilerConfiguration允许您指定类路径。

代码语言:javascript
复制
    def config = new CompilerConfiguration(classpath: classpath)
    def binding = new Binding()

    def result = new GroovyShell(binding, config).evaluate("""
def foo='bar'
""")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24109399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档