java-scripting-engine

How can I determine if a variable exists from within the Groovy code running in the Scripting Engine?

此生再无相见时 提交于 2020-12-04 16:01:46
问题 How can I determine if a variable exists from within the Groovy code running in the Scripting Engine? The variable was put by ScriptEngine's put method 回答1: In the groovy.lang.Script there is a method public Binding getBinding() . See also groovy.lang.Binding with method public boolean hasVariable(String name) . Thus you can simple check variable existence like if (binding.hasVariable('superVariable')) { // your code here } 回答2: // Example usage: defaultIfInexistent({myVar}, "default") def

How can I determine if a variable exists from within the Groovy code running in the Scripting Engine?

你。 提交于 2020-12-04 16:00:39
问题 How can I determine if a variable exists from within the Groovy code running in the Scripting Engine? The variable was put by ScriptEngine's put method 回答1: In the groovy.lang.Script there is a method public Binding getBinding() . See also groovy.lang.Binding with method public boolean hasVariable(String name) . Thus you can simple check variable existence like if (binding.hasVariable('superVariable')) { // your code here } 回答2: // Example usage: defaultIfInexistent({myVar}, "default") def