Skip to content

Commit 23af6c6

Browse files
committed
GROOVY-8162: Update Groovysh to JLine3 (clarify duplicate method handling)
1 parent b5622c6 commit 23af6c6

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyEngine.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public interface Cloner {
164164
public GroovyEngine() {
165165
sharedData = new Binding();
166166
// for debugging
167-
// sharedData.setVariable("engine", this);
167+
sharedData.setVariable("engine", this);
168168
classLoader = new EngineClassLoader();
169169
shell = new GroovyShell(classLoader, sharedData);
170170
for (String s : DEFAULT_IMPORTS) {
@@ -577,7 +577,12 @@ private boolean methodDef(String statement) throws Exception {
577577
methodNames.add(m.group(3));
578578
if (isInterpreterMode()) {
579579
String code = removeTrailingSemi(m.group(0));
580-
methods.put(m.group(2), addSnippet(SnippetType.METHOD, code));
580+
Integer existing = methods.get(m.group(2));
581+
if (existing != null) {
582+
snippets.get(existing).setSnippet(code);
583+
} else {
584+
methods.put(m.group(2), addSnippet(SnippetType.METHOD, code));
585+
}
581586
} else {
582587
String body = m.group(5).substring(1);
583588
put(m.group(3), execute("{" + m.group(4) + "->" + body));
@@ -654,8 +659,10 @@ public void removeMethod(String name) {
654659
String k = it.next();
655660
if (k.equals(name) || k.startsWith(name + "(")) {
656661
Integer gone = methods.get(k);
657-
if (gone != null) snippets.set(gone, null);
658-
it.remove();
662+
if (gone != null) {
663+
snippets.set(gone, null);
664+
it.remove();
665+
}
659666
}
660667
}
661668
}

subprojects/groovy-groovysh/src/spec/doc/groovysh.adoc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ This behavior can be changed by activating <<GroovyShell-InterpreterMode,interpr
248248
249249
Methods can be defined in the shell, and will be saved for later use.
250250
251-
Defining a function is easy:
251+
Defining a method is easy:
252252
253253
[source,jshell]
254254
----------------------------------
@@ -265,9 +265,15 @@ groovy> hello "Jason"
265265
Hello Jason
266266
--------------
267267
268-
Internally the shell creates a closure to encapsulate the function and
269-
then binds the closure to a variable. So variables and functions share
270-
the same namespace.
268+
If a method definition has the same signature as an existing definition,
269+
the old definition will be replaced with the new one.
270+
271+
When in _interpreterMode_, methods are remembered and given as extra statements
272+
when executing the next input.
273+
274+
When not in _interpreterMode_, the shell internally creates a closure to encapsulate
275+
the method and stores it in the shared variables.
276+
In this case, variables and methods share the same namespace.
271277
272278
[[GroovyShell-Exceptions]]
273279
==== Exceptions

0 commit comments

Comments
 (0)