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

ycomp :

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

Aliaksandr Pyrkh :

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
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=448617&siteId=1