Search code examples
javajenkinsgroovyscriptingjava-scripting-engine

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


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


Solution

  • Improvement for the existing solution given here https://stackoverflow.com/a/56306660/3158217 -

    def defaultIfInexistent(varName, defaultValue){
        try{
            varName.toString()
            return varName
        }catch(ex){
            return defaultValue
        }
    }