Package org.kissweb
Class GroovyUtils
java.lang.Object
org.kissweb.GroovyUtils
Miscellaneous Groovy utilities
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectrunGroovyCode(String pgm) Executes the given Groovy code and returns the result.static ObjectrunGroovyCode(String pgm, Map<String, Object> args) Executes the given Groovy code with the specified arguments and returns the result.
-
Constructor Details
-
GroovyUtils
public GroovyUtils()
-
-
Method Details
-
runGroovyCode
Executes the given Groovy code with the specified arguments and returns the result.This method allows dynamic execution of Groovy code provided as a string. Variables can be passed to the Groovy script via a map of arguments, making them accessible within the script's context.
- Parameters:
pgm- the Groovy code to execute, provided as aStringargs- aMap<String, Object>containing variable names and their values to be passed into the Groovy script; may benullif no variables are needed- Returns:
- the result of the Groovy script execution, which can be any Object returned by the script
- Throws:
groovy.lang.GroovyRuntimeException- if an error occurs during compilation or execution of the Groovy codeIllegalArgumentException- if thepgmparameter isnullor empty- See Also:
-
GroovyShell.evaluate(String)- Example Usage:
public static void main(String[] args) { String groovyCode = "return 'Hello, ' + name + '!'"; Map<String, Object> arguments = new HashMap<>(); arguments.put("name", "World"); Object result = runGroovyCode(groovyCode, arguments); System.out.println(result); // Outputs: Hello, World! }
-
runGroovyCode
Executes the given Groovy code and returns the result.This method allows dynamic execution of Groovy code provided as a string, without passing any variables into the script. It is a convenience method that calls
runGroovyCode(String, Map)with anullarguments map.- Parameters:
pgm- the Groovy code to execute, provided as aString- Returns:
- the result of the Groovy script execution, which can be any
Objectreturned by the script - Throws:
groovy.lang.GroovyRuntimeException- if an error occurs during compilation or execution of the Groovy codeIllegalArgumentException- if thepgmparameter isnullor empty- See Also:
-