オブジェクトをコピーする代わりに、Bindingオブジェクトがオブジェクトへの参照を格納する方法があるかどうか疑問に思っていますか?
Binding b = new Binding()
String test = "test"
b.setVariable("sth", test)
test = "blah"
GroovyShell gs = new GroovyShell(b)
gs.evaluate("print(sth)")
残念ながら、「テスト」を出力します。
Groovyでそれを行う方法はありますか?
編集:
私が挙げた例は間違っていて、あまりにも単純すぎました。
あるスレッドでオブジェクトをインスタンス化し、別のスレッドでスクリプトを実行するため、問題が発生すると思います。
class Test {
[...] // field declarations
public Test(String name, String url, def params, String validateScript, String afterTestScript, GroovyShell shell) {
[...] //just assigning params to fields
shell.setVariable("current", this)
}
void action() {
response = "something"
}
void validate() {
//shell.setVariable("current", this)
}
void afterTest() {
if (afterTestScript) shell.evaluate(afterTestScript)
}
}
action
したがって、Testオブジェクトは1つのスレッドで作成され、Runnableオブジェクトへの参照として渡します。Runnableオブジェクトは、メソッドvalidate
を呼び出しafterTest
ます。setVariable
コンストラクターでの呼び出しをコメントアウトし、コメントを外すと、正常にvalidate
機能します。
並行性の「問題」である可能性はありますか?afterTestScriptでは、応答文字列を出力したいだけです。