Class GroovyEngine

java.lang.Object
org.apache.groovy.groovysh.jline.GroovyEngine
All Implemented Interfaces:
org.jline.console.ScriptEngine

public class GroovyEngine extends Object implements org.jline.console.ScriptEngine
Implements Groovy ScriptEngine. You must be very careful when using GroovyEngine in a multithreaded environment. The Binding instance is not thread safe, and it is shared by all scripts.
  • Field Details

    • CANONICAL_NAMES

      public static final String CANONICAL_NAMES
      Option key for using canonical class names in descriptions
      See Also:
    • NANORC_SYNTAX

      public static final String NANORC_SYNTAX
      Option key for nanorc syntax file location
      See Also:
    • NANORC_VALUE

      public static final String NANORC_VALUE
      Option key for nanorc value configuration
      See Also:
    • GROOVY_COLORS

      public static final String GROOVY_COLORS
      Environment variable for Groovy color settings
      See Also:
    • NO_SYNTAX_CHECK

      public static final String NO_SYNTAX_CHECK
      Option key to disable syntax checking
      See Also:
    • RESTRICTED_COMPLETION

      public static final String RESTRICTED_COMPLETION
      Option key for restricted completion mode
      See Also:
    • ALL_FIELDS_COMPLETION

      public static final String ALL_FIELDS_COMPLETION
      Option key to enable completion for all fields including non-public
      See Also:
    • ALL_METHODS_COMPLETION

      public static final String ALL_METHODS_COMPLETION
      Option key to enable completion for all methods including non-public
      See Also:
    • ALL_CONSTRUCTORS_COMPLETION

      public static final String ALL_CONSTRUCTORS_COMPLETION
      Option key to enable completion for all constructors including non-public
      See Also:
    • ALL_CLASSES_COMPLETION

      public static final String ALL_CLASSES_COMPLETION
      Option key to enable completion for all classes including non-public
      See Also:
    • IDENTIFIERS_COMPLETION

      public static final String IDENTIFIERS_COMPLETION
      Option key to enable identifier completion
      See Also:
    • META_METHODS_COMPLETION

      public static final String META_METHODS_COMPLETION
      Option key to enable meta-method completion
      See Also:
    • SYNTHETIC_METHODS_COMPLETION

      public static final String SYNTHETIC_METHODS_COMPLETION
      Option key to enable synthetic method completion
      See Also:
    • sharedData

      protected Binding sharedData
      Shared binding that stores variables visible to interactive scripts.
    • classLoader

      protected final GroovyEngine.EngineClassLoader classLoader
      Class loader for dynamically loaded Groovy classes
  • Constructor Details

    • GroovyEngine

      public GroovyEngine()
      Constructs a new GroovyEngine with default configuration.
    • GroovyEngine

      @Incubating public GroovyEngine(CompilerConfiguration configuration)
      Constructs a GroovyEngine that evaluates with the given compiler configuration.

      Embedders that previously installed an ImportCustomizer or ASTTransformationCustomizer (for example ThreadInterrupt) on groovysh's CompilerConfiguration should pass that configuration here. CompilerConfiguration.DEFAULT is immutable; start from new CompilerConfiguration() when adding customizers.

      Parameters:
      configuration - compiler configuration, or null for CompilerConfiguration.DEFAULT
      Since:
      7.0.0
    • GroovyEngine

      @Incubating public GroovyEngine(ClassLoader parent, Binding binding, CompilerConfiguration configuration)
      Constructs a GroovyEngine with an explicit parent class loader, binding, and compiler configuration.

      As with GroovyShell, an GroovyEngine.EngineClassLoader whose configuration is the same instance as configuration becomes the engine class loader itself rather than its parent; any other loader is wrapped. Two engines sharing one loader that way also share its class cache, so a cache purge from either — /reset and the type-redefinition paths do this — discards the other's classes, and their script classes occupy one namespace. Pass a plain ClassLoader, or a configuration this loader does not already hold, to get a child loader instead.

      Parameters:
      parent - class loader to use as parent, or to adopt as described above; null uses the thread context class loader
      binding - shared binding, or null to create a new one
      configuration - compiler configuration, or null for CompilerConfiguration.DEFAULT
      Since:
      7.0.0
  • Method Details

    • createShell

      @Incubating protected GroovyShell createShell(ClassLoader classLoader, Binding binding, CompilerConfiguration configuration)
      Creates the GroovyShell used to evaluate statements.

      Called from the constructor, so overrides must not rely on subclass fields that are assigned after super(...) returns. Primitive fields and static state are safe; instance initializers of the subclass have not run yet. The returned shell must use classLoader as its loader (the default implementation does this via GroovyClassLoader.hasCompatibleConfiguration(CompilerConfiguration)), otherwise engine internals that cast to GroovyEngine.EngineClassLoader will fail.

      Parameters:
      classLoader - the engine class loader
      binding - the shared binding
      configuration - the compiler configuration, never null
      Returns:
      a shell that uses classLoader as its loader
      Since:
      7.0.0
    • getClassLoader

      Returns the class loader used to parse and load scripts for this engine.
      Returns:
      the engine class loader
      Since:
      7.0.0
    • getCompilerConfiguration

      @Incubating public CompilerConfiguration getCompilerConfiguration()
      Returns the compiler configuration this engine was constructed with.
      Returns:
      the compiler configuration, never null
      Since:
      7.0.0
    • getTypes

      public Map<String,String> getTypes()
      Returns all defined types mapped to their source definitions.
      Returns:
      map of type names to their source code
    • getVariables

      public Map<String,String> getVariables()
      Returns all defined variables mapped to their source definitions.
      Returns:
      map of variable names to their source code
    • getMethods

      public Map<String,String> getMethods()
      Returns all defined methods mapped to their source definitions.
      Returns:
      map of method signatures to their source code
    • getMethodNames

      public Set<String> getMethodNames()
      Returns the set of defined method names.
      Returns:
      unmodifiable set of method names
    • getImports

      public Map<String,String> getImports()
      Returns all import statements mapped to their source definitions.
      Returns:
      map of import identifiers to their source code
    • getScriptCompleter

      public org.jline.reader.Completer getScriptCompleter()
      Returns the completer for Groovy script completions.
      Specified by:
      getScriptCompleter in interface org.jline.console.ScriptEngine
      Returns:
      a completer instance
    • hasVariable

      public boolean hasVariable(String name)
      Checks if a variable with the given name exists in the binding.
      Specified by:
      hasVariable in interface org.jline.console.ScriptEngine
      Parameters:
      name - the variable name to check
      Returns:
      true if the variable exists, false otherwise
    • put

      public void put(String name, Object value)
      Stores a variable in the binding.
      Specified by:
      put in interface org.jline.console.ScriptEngine
      Parameters:
      name - the variable name
      value - the value to store
    • get

      public Object get(String name)
      Retrieves a variable from the binding.
      Specified by:
      get in interface org.jline.console.ScriptEngine
      Parameters:
      name - the variable name
      Returns:
      the variable value, or null if not found
    • find

      public Map<String,Object> find(String name)
      Finds variables matching the given pattern.
      Specified by:
      find in interface org.jline.console.ScriptEngine
      Parameters:
      name - the variable name pattern, or null to return all variables
      Returns:
      a map of matching variable names to their values
    • getSerializationFormats

      public List<String> getSerializationFormats()
      Returns the list of supported serialization formats.
      Specified by:
      getSerializationFormats in interface org.jline.console.ScriptEngine
      Returns:
      list of format names
    • getDeserializationFormats

      public List<String> getDeserializationFormats()
      Returns the list of supported deserialization formats.
      Specified by:
      getDeserializationFormats in interface org.jline.console.ScriptEngine
      Returns:
      list of format names
    • deserialize

      public Object deserialize(String value, String formatStr)
      Deserializes a string value according to the specified format.
      Specified by:
      deserialize in interface org.jline.console.ScriptEngine
      Parameters:
      value - the string to deserialize
      formatStr - the format name (JSON, GROOVY, NONE, or AUTO). AUTO parses only: it recognises JSON and otherwise returns the text unchanged, and never evaluates the value as Groovy. Use GROOVY to evaluate.
      Returns:
      the deserialized object
    • persist

      public void persist(Path file, Object object)
      Persists an object to a file using the default serialization format.
      Specified by:
      persist in interface org.jline.console.ScriptEngine
      Parameters:
      file - the target file path
      object - the object to persist
    • persist

      public void persist(Path file, Object object, String format)
      Persists an object to a file using the specified serialization format.
      Specified by:
      persist in interface org.jline.console.ScriptEngine
      Parameters:
      file - the target file path
      object - the object to persist
      format - the format name to use
    • execute

      public Object execute(File script, Object[] args) throws Exception
      Executes a Groovy script from a file.
      Specified by:
      execute in interface org.jline.console.ScriptEngine
      Parameters:
      script - the script file to execute
      args - arguments to pass to the script
      Returns:
      the result of script execution
      Throws:
      Exception - if script execution fails
    • getBuffer

      public String getBuffer()
      Returns the current executable buffer reconstructed from the recorded snippets.
      Returns:
      buffer content for the current session
    • execute

      public Object execute(String statement) throws Exception
      Executes a Groovy statement.
      Specified by:
      execute in interface org.jline.console.ScriptEngine
      Parameters:
      statement - the Groovy statement to execute
      Returns:
      the result of statement execution
      Throws:
      Exception - if statement execution fails
    • execute

      public Object execute(Object closure, Object... args)
      Executes a Groovy closure with the given arguments.
      Specified by:
      execute in interface org.jline.console.ScriptEngine
      Parameters:
      closure - the closure to execute
      args - arguments to pass to the closure
      Returns:
      the result of closure execution
    • getEngineName

      public String getEngineName()
      Returns the name of this script engine.
      Specified by:
      getEngineName in interface org.jline.console.ScriptEngine
      Returns:
      the engine name
    • getExtensions

      public List<String> getExtensions()
      Returns the file extensions supported by this engine.
      Specified by:
      getExtensions in interface org.jline.console.ScriptEngine
      Returns:
      list of supported file extensions
    • reset

      public void reset()
      Resets the engine state, clearing all variables, methods, types, and imports.
    • removeMethod

      public void removeMethod(String name)
      Removes a method definition by name or signature.
      Parameters:
      name - method name or full signature to remove
    • removeImport

      public void removeImport(String name)
      Removes an import statement by name.
      Parameters:
      name - the import identifier to remove
    • removeType

      public void removeType(String name)
      Removes a type definition by name.
      Parameters:
      name - the type name to remove
    • removeVariable

      public void removeVariable(String name)
      Removes a variable definition by name.
      Parameters:
      name - the variable name to remove
    • del

      public void del(String... vars)
      Deletes multiple variables from the binding.
      Specified by:
      del in interface org.jline.console.ScriptEngine
      Parameters:
      vars - variable names to delete
    • toJson

      public String toJson(Object obj)
      Converts an object to JSON format.
      Specified by:
      toJson in interface org.jline.console.ScriptEngine
      Parameters:
      obj - the object to convert
      Returns:
      JSON representation
    • toString

      public String toString(Object obj)
      Converts an object to Groovy string format.
      Specified by:
      toString in interface org.jline.console.ScriptEngine
      Parameters:
      obj - the object to convert
      Returns:
      Groovy string representation
    • toMap

      public Map<String,Object> toMap(Object obj)
      Converts an object to a map representation.
      Specified by:
      toMap in interface org.jline.console.ScriptEngine
      Parameters:
      obj - the object to convert
      Returns:
      map representation
    • setObjectCloner

      public void setObjectCloner(GroovyEngine.Cloner objectCloner)
      Sets the object cloner to use for copying session values.
      Parameters:
      objectCloner - the cloner implementation
    • getObjectCloner

      public GroovyEngine.Cloner getObjectCloner()
      Returns the current object cloner.
      Returns:
      the cloner implementation
    • scriptDescription

      public org.jline.console.CmdDesc scriptDescription(org.jline.console.CmdLine line)
      Generates a description for the given command line.
      Parameters:
      line - the command line to describe
      Returns:
      the command description
    • groovyOptions

      protected Map<String,Object> groovyOptions()
      Returns the current Groovy options map.
      Returns:
      map of option keys to values
    • groovyOption

      protected <T> T groovyOption(String option, T defval)
      Returns the value of a specific Groovy option.
      Type Parameters:
      T - the type of the option value
      Parameters:
      option - the option key
      defval - the default value if option not found
      Returns:
      the option value or default
    • groovyOption

      protected static <T> T groovyOption(Map<String,Object> options, String option, T defval)
      Returns the value of a specific option from the given options map.
      Type Parameters:
      T - the type of the option value
      Parameters:
      options - the options map
      option - the option key
      defval - the default value if option not found
      Returns:
      the option value or default
    • refresh

      public boolean refresh()
      Refreshes the syntax highlighter, forcing a reload on next use.
      Returns:
      true if refresh was successful
    • getSyntaxHighlighter

      protected org.jline.builtins.SyntaxHighlighter getSyntaxHighlighter()
      Returns the syntax highlighter for Groovy code.
      Returns:
      the syntax highlighter instance
    • purgeClassCache

      public void purgeClassCache(String regex)
      Purges classes from the class cache matching the given regex.
      Parameters:
      regex - the pattern to match class names, or null to clear all