You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2019/05/06 22:22:55 UTC

[groovy] branch master updated: GROOVY-9018: Make Interpreter changeable in Groovysh (closes #884)

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


The following commit(s) were added to refs/heads/master by this push:
     new c8e19b0  GROOVY-9018: Make Interpreter changeable in Groovysh (closes #884)
c8e19b0 is described below

commit c8e19b0a6988dbbda83e4774b9513e98ee4ed113
Author: Jan Lukavsky <je...@seznam.cz>
AuthorDate: Mon Feb 25 16:23:49 2019 +0100

    GROOVY-9018: Make Interpreter changeable in Groovysh (closes #884)
---
 .../org/codehaus/groovy/tools/shell/Groovysh.groovy      | 16 +++++++++-------
 .../org/codehaus/groovy/tools/shell/Interpreter.groovy   |  4 ++++
 .../groovy/tools/shell/CompletorTestSupport.groovy       |  2 +-
 .../groovy/tools/shell/ShellRunnerTestSupport.groovy     |  2 +-
 4 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Groovysh.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Groovysh.groovy
index 3186d45..db152a8 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Groovysh.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Groovysh.groovy
@@ -98,18 +98,21 @@ class Groovysh extends Shell {
     }
 
     Groovysh(final ClassLoader classLoader, final Binding binding, final IO io, final Closure registrar, CompilerConfiguration configuration) {
+       this(classLoader, binding, io, registrar, configuration,  new Interpreter(classLoader, binding, configuration))
+    }
+    
+    Groovysh(final ClassLoader classLoader, final Binding binding, final IO io, final Closure registrar, CompilerConfiguration configuration, Interpreter interpreter) {
         super(io)
         assert classLoader
         assert binding
-        assert registrar
+        def actualRegistrar = registrar ?: createDefaultRegistrar(classLoader)
         parser = new Parser()
-        interp = new Interpreter(classLoader, binding, configuration)
-        registrar.call(this)
+        interp = interpreter
+        actualRegistrar.call(this)
         this.packageHelper = new PackageHelperImpl(classLoader)
     }
 
     private static Closure createDefaultRegistrar(final ClassLoader classLoader) {
-
         return {Groovysh shell ->
             URL xmlCommandResource = getClass().getResource('commands.xml')
             if (xmlCommandResource != null) {
@@ -122,7 +125,7 @@ class Groovysh extends Shell {
     }
 
     Groovysh(final ClassLoader classLoader, final Binding binding, final IO io) {
-        this(classLoader, binding, io, createDefaultRegistrar(classLoader))
+        this(classLoader, binding, io, null)
     }
 
     Groovysh(final Binding binding, final IO io) {
@@ -134,8 +137,7 @@ class Groovysh extends Shell {
     }
 
     Groovysh(final IO io, CompilerConfiguration configuration) {
-        this(Thread.currentThread().contextClassLoader, new Binding(), io,
-                createDefaultRegistrar(Thread.currentThread().contextClassLoader), configuration)
+        this(Thread.currentThread().contextClassLoader, new Binding(), io, null, configuration)
     }
 
     Groovysh() {
diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Interpreter.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Interpreter.groovy
index 91e4c0e..31a01ca 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Interpreter.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Interpreter.groovy
@@ -58,6 +58,10 @@ class Interpreter implements Evaluator
         return shell.classLoader
     }
 
+    GroovyShell getShell() {
+        return shell
+    }
+
     @Override
     def evaluate(final Collection<String> buffer) {
         assert buffer
diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/CompletorTestSupport.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/CompletorTestSupport.groovy
index cf714eb..7b5db87 100644
--- a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/CompletorTestSupport.groovy
+++ b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/CompletorTestSupport.groovy
@@ -54,12 +54,12 @@ abstract class CompletorTestSupport extends GroovyTestCase {
         idCompletorMocker = new MockFor(IdentifierCompletor)
 
         groovyshMocker = new MockFor(Groovysh)
+        groovyshMocker.demand.getClass(0..1) { Groovysh }
         groovyshMocker.demand.createDefaultRegistrar { { shell -> null } }
         groovyshMocker.demand.getIo(0..2) { testio }
         packageHelperMocker = new MockFor(PackageHelperImpl)
         def registry = new CommandRegistry()
         groovyshMocker.demand.getRegistry(0..1) { registry }
-        groovyshMocker.demand.getClass(0..1) { Groovysh }
         packageHelperMocker.demand.getContents(6) { ['java', 'test'] }
         groovyshMocker.demand.getIo(0..2) { testio }
         for (i in 1..19) {
diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/ShellRunnerTestSupport.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/ShellRunnerTestSupport.groovy
index d86550f..d74c6f3 100644
--- a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/ShellRunnerTestSupport.groovy
+++ b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/ShellRunnerTestSupport.groovy
@@ -44,9 +44,9 @@ abstract class ShellRunnerTestSupport extends GroovyTestCase {
         // setup mock and stub with calls expected from InteractiveShellRunner Constructor
 
         shellMocker = new MockFor(Groovysh)
-        shellMocker.demand.createDefaultRegistrar(1) { {Shell shell -> null} }
         // when run with compileStatic
         shellMocker.demand.getClass(0..1) {Groovysh}
+        shellMocker.demand.createDefaultRegistrar(1) { {Shell shell -> null} }
         shellMocker.demand.getIo(2) { testio }
         shellMocker.demand.getRegistry(1) {new Object() {def commands() {[]} }}
         shellMocker.demand.getHistory(1) {new Serializable(){def size() {0}; def getMaxSize() {1}}}