This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 23af6c6270c9794550e58a74be6e0113a73ffa9c
Author: Paul King <[email protected]>
AuthorDate: Tue Aug 5 21:29:13 2025 +1000

    GROOVY-8162: Update Groovysh to JLine3 (clarify duplicate method handling)
---
 .../org/apache/groovy/groovysh/jline/GroovyEngine.java    | 15 +++++++++++----
 subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc    | 14 ++++++++++----
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git 
a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyEngine.java
 
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyEngine.java
index 91714c7917..3338b020c2 100644
--- 
a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyEngine.java
+++ 
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyEngine.java
@@ -164,7 +164,7 @@ public class GroovyEngine implements ScriptEngine {
     public GroovyEngine() {
         sharedData = new Binding();
 // for debugging
-//        sharedData.setVariable("engine", this);
+        sharedData.setVariable("engine", this);
         classLoader = new EngineClassLoader();
         shell = new GroovyShell(classLoader, sharedData);
         for (String s : DEFAULT_IMPORTS) {
@@ -577,7 +577,12 @@ public class GroovyEngine implements ScriptEngine {
             methodNames.add(m.group(3));
             if (isInterpreterMode()) {
                 String code = removeTrailingSemi(m.group(0));
-                methods.put(m.group(2), addSnippet(SnippetType.METHOD, code));
+                Integer existing = methods.get(m.group(2));
+                if (existing != null) {
+                    snippets.get(existing).setSnippet(code);
+                } else {
+                    methods.put(m.group(2), addSnippet(SnippetType.METHOD, 
code));
+                }
             } else {
                 String body = m.group(5).substring(1);
                 put(m.group(3), execute("{" + m.group(4) + "->" + body));
@@ -654,8 +659,10 @@ public class GroovyEngine implements ScriptEngine {
                 String k = it.next();
                 if (k.equals(name) || k.startsWith(name + "(")) {
                     Integer gone = methods.get(k);
-                    if (gone != null) snippets.set(gone, null);
-                    it.remove();
+                    if (gone != null) {
+                        snippets.set(gone, null);
+                        it.remove();
+                    }
                 }
             }
         }
diff --git a/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc 
b/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc
index ba2dd49304..e094a7c994 100644
--- a/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc
+++ b/subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc
@@ -248,7 +248,7 @@ This behavior can be changed by activating 
<<GroovyShell-InterpreterMode,interpr
 
 Methods can be defined in the shell, and will be saved for later use.
 
-Defining a function is easy:
+Defining a method is easy:
 
 [source,jshell]
 ----------------------------------
@@ -265,9 +265,15 @@ groovy> hello "Jason"
 Hello Jason
 --------------
 
-Internally the shell creates a closure to encapsulate the function and
-then binds the closure to a variable. So variables and functions share
-the same namespace.
+If a method definition has the same signature as an existing definition,
+the old definition will be replaced with the new one.
+
+When in _interpreterMode_, methods are remembered and given as extra statements
+when executing the next input.
+
+When not in _interpreterMode_, the shell internally creates a closure to 
encapsulate
+the method and stores it in the shared variables.
+In this case, variables and methods share the same namespace.
 
 [[GroovyShell-Exceptions]]
 ==== Exceptions

Reply via email to