Package org.kissweb

Class GroovyUtils

java.lang.Object
org.kissweb.GroovyUtils

public class GroovyUtils extends Object
Miscellaneous Groovy utilities
  • Constructor Details

    • GroovyUtils

      public GroovyUtils()
  • Method Details

    • runGroovyCode

      public static Object runGroovyCode(String pgm, Map<String,Object> args)
      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 a String
      args - a Map<String, Object> containing variable names and their values to be passed into the Groovy script; may be null if 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 code
      IllegalArgumentException - if the pgm parameter is null or 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

      public static Object runGroovyCode(String pgm)
      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 a null arguments map.

      Parameters:
      pgm - the Groovy code to execute, provided as a String
      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 code
      IllegalArgumentException - if the pgm parameter is null or empty
      See Also: