オブジェクトをコピーするのではなく、オブジェクトへの参照を格納するためのオブジェクトをバインドする方法があるのでしょうか?GroovyShellバインディングは、setVariableを実行するときにオブジェクトへの参照を渡すことができますか?
Binding b = new Binding()
String test = "test"
b.setVariable("sth", test)
test = "blah"
GroovyShell gs = new GroovyShell(b)
gs.evaluate("print(sth)")
残念ながら、「テスト」が印刷されます。
Groovyで行う方法はありますか?
EDIT:
私が与えた例が間違ったとあまりにも簡単でした。
私は1つのスレッドでオブジェクトをインスタンス化し、別のスレッドでスクリプトを実行するため、私の問題が発生すると思います。
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)
}
}
だからテストオブジェクトは1つのスレッドで作成され、その後、私は、action
、validate
とafterTest
メソッドを呼び出すRunnableオブジェクトへの参照として渡します。 コンストラクタ内のsetVariable
へのコールをコメントアウトし、validate
でコールのコメントを外しても正常に動作します。
同時実行性「問題」は可能でしょうか? afterTestScriptでは、レスポンス文字列を出力したいだけです。