1

背景 :

我最近开始使用 Groovy,并试图在 eclipse 插件中嵌入一个 groovy 脚本引擎,让我的客户在我的基于 eclipse 的产品中开发自己的 GUI 扩展。这与codehaus 网站上发布的成功故事非常相似。

问题

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

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

问题

有没有人遇到同样的问题?如何解决?任何帮助将不胜感激!

一些观察:

  • 当使用 groovy 解释器而不是 GroovyScriptEngine java 对象时,使用我的 SwtGuiBuilder 类没有问题(请参阅下面的脚本“main_groovy”)。

  • 我的问题似乎不是类路径问题,因为在抛出的异常中提到了包含我的 SwtGuiBuilder 类的文件。

  • 错误消息在两个报告的 groovy 错误 GRECLIPSE-429 和 GRECLIPSE-1037 中提到。我没有完全了解技术细节,但这些错误似乎与加载大量类时的性能问题有关,这与我的情况无关......

细节

SampleView.java

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

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

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

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

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

堆栈跟踪

漏洞!在已经迭代的同时排队新源。排队的源是 org.codehaus.groovy.control.CompilationUnit.addSource(CompilationUnit.java:460) 的 org.codehaus.groovy 的 'file:/home/nicolas/workspace/groovy-test/src/gui/SwtGuiBuilder.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)在 org.codehaus.groovy.control.ResolveVisitor.resolveToOuter(ResolveVisitor.java:863) 在 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.

我的配置:

  • Linux Ubuntu 14.04 x86

  • Groovy 版本:2.3.2

  • JVM:1.7.0_55

  • Eclipse Kepler SR2 - 内部版本 20140224-0627

  • Eclipse Groovy 插件 v2.0.7

4

1 回答 1

0

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

    def config = new CompilerConfiguration(classpath: classpath)
    def binding = new Binding()

    def result = new GroovyShell(binding, config).evaluate("""
def foo='bar'
""")
于 2014-06-09T19:12:54.190 回答