バックグラウンド :
私は最近 Groovy を使い始め、Eclipse ベースの製品内で顧客が独自の GUI 拡張機能を開発できるように、Eclipse プラグインに Groovy スクリプト エンジンを埋め込もうとしています。これは、codehaus の Web サイトで公開されているサクセス ストーリーと非常によく似ています。
問題
GroovyScriptEngine によって Eclipse プラグインから実行される groovy スクリプト (「main_eclipse.groovy」と呼びましょう) は、groovy クラス (「SwtGuiBuilder」) をロードしようとすると、次のエラーでスローします。
バグ!すでに反復している間に新しいソースをキューに入れています。キューに入れられたソースは「file:/home/nicolas/workspace/groovy-test/src/gui/SwtGuiBuilder.groovy」です
質問
誰かが同じ問題に遭遇しましたか? どうすれば修正できますか?どんな助けでも大歓迎です!
いくつかの観察:
GroovyScriptEngine Java オブジェクトの代わりに groovy インタープリターを使用する場合、SwtGuiBuilder クラスを問題なく使用できます (以下のスクリプト「main_groovy」を参照)。
SwtGuiBuilder クラスを含むファイルがスローされた例外に記載されているため、私の問題はクラスパスの問題ではないようです。
このエラー メッセージは、報告された 2 つの 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 の org.codehaus.groovy.control.CompilationUnit.addSource(CompilationUnit.java:460) の「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
グルーヴィーなバージョン: 2.3.2
JVM: 1.7.0_55
Eclipse ケプラー SR2 - ビルド 20140224-0627
Eclipse Groovy プラグイン v2.0.7