From commits-return-5744-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Mon Mar 5 00:55:36 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 8A890180676 for ; Mon, 5 Mar 2018 00:55:35 +0100 (CET) Received: (qmail 40104 invoked by uid 500); 4 Mar 2018 23:55:34 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 40093 invoked by uid 99); 4 Mar 2018 23:55:34 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 04 Mar 2018 23:55:34 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 8691DF351F; Sun, 4 Mar 2018 23:55:34 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: jwagenleitner@apache.org To: commits@groovy.apache.org Date: Sun, 04 Mar 2018 23:55:35 -0000 Message-Id: <751a3be51b5f44f8adddfd9fc80773bf@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/2] groovy git commit: backport fix GROOVY-7562 for the 2_4_X branch backport fix GROOVY-7562 for the 2_4_X branch Groovysh: Fix imports not working at all in interpreter mode Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/2aa0a54e Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/2aa0a54e Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/2aa0a54e Branch: refs/heads/GROOVY_2_4_X Commit: 2aa0a54ed0944bd06e7b52af6a9b810c16b6d419 Parents: ba00a0a Author: John Wagenleitner Authored: Sat Mar 3 20:21:16 2018 -0800 Committer: John Wagenleitner Committed: Sun Mar 4 13:35:57 2018 -0800 ---------------------------------------------------------------------- .../codehaus/groovy/tools/shell/Groovysh.groovy | 26 +++++++++++++------- .../groovy/tools/shell/GroovyshTest.groovy | 11 +++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/2aa0a54e/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Groovysh.groovy ---------------------------------------------------------------------- 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 da81429..af93404 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 @@ -196,7 +196,7 @@ class Groovysh extends Shell { setLastResult(result = interp.evaluate(buff)) } else { // Evaluate Buffer wrapped with code storing bounded vars - result = evaluateWithStoredBoundVars(current) + result = evaluateWithStoredBoundVars(importsSpec, current) } buffers.clearSelected() @@ -232,14 +232,14 @@ class Groovysh extends Shell { * to simulate an interpreter mode, this method wraps the statements into a try/finally block that * stores bound variables like unbound variables */ - private Object evaluateWithStoredBoundVars(final List current) { + private Object evaluateWithStoredBoundVars(String importsSpec, final List current) { Object result - String variableBlocks = '' - // To make groovysh behave more like an interpreter, we need to retrive all bound + String variableBlocks = null + // To make groovysh behave more like an interpreter, we need to retrieve all bound // vars at the end of script execution, and then update them into the groovysh Binding context. - Set boundVars = ScriptVariableAnalyzer.getBoundVars(current.join(Parser.NEWLINE), interp.classLoader) - variableBlocks += "$COLLECTED_BOUND_VARS_MAP_VARNAME = new HashMap();" + Set boundVars = ScriptVariableAnalyzer.getBoundVars(importsSpec + Parser.NEWLINE + current.join(Parser.NEWLINE), interp.classLoader) if (boundVars) { + variableBlocks = "$COLLECTED_BOUND_VARS_MAP_VARNAME = new HashMap();" boundVars.each({ String varname -> // bound vars can be in global or some local scope. // We discard locally scoped vars by ignoring MissingPropertyException @@ -250,12 +250,20 @@ try {$COLLECTED_BOUND_VARS_MAP_VARNAME[\"$varname\"] = $varname; } // Evaluate the current buffer w/imports and dummy statement - List buff = imports + ['try {', 'true'] + current + ['} finally {' + variableBlocks + '}'] + List buff + if (variableBlocks) { + buff = [importsSpec] + ['try {', 'true'] + current + ['} finally {' + variableBlocks + '}'] + } else { + buff = [importsSpec] + ['true'] + current + } setLastResult(result = interp.evaluate(buff)) - Map boundVarValues = interp.context.getVariable(COLLECTED_BOUND_VARS_MAP_VARNAME) - boundVarValues.each({ String name, Object value -> interp.context.setVariable(name, value) }) + if (variableBlocks) { + Map boundVarValues = interp.context.getVariable(COLLECTED_BOUND_VARS_MAP_VARNAME) + boundVarValues.each({ String name, Object value -> interp.context.setVariable(name, value) }) + } + return result } http://git-wip-us.apache.org/repos/asf/groovy/blob/2aa0a54e/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy index 7a0f6e1..7791827 100644 --- a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy +++ b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/GroovyshTest.groovy @@ -315,6 +315,17 @@ class GroovyshTest extends GroovyTestCase { } catch (ArithmeticException e) {} } + void testImports() { + Groovysh groovysh = new Groovysh(testio) + groovysh.execute('import java.rmi.Remote ') + assert mockOut.toString().length() > 0 + assert 'java.rmi.Remote\n' == mockOut.toString().normalize()[-('java.rmi.Remote\n'.length())..-1] + groovysh.execute('Remote r') + assert mockOut.toString().length() > 0 + // mostly assert no exception + assert 'null\n' == mockOut.toString().normalize()[-5..-1] + } + static File createTemporaryGroovyScriptFile(content) { String testName = 'GroovyshTest' + System.currentTimeMillis() File groovyCode = new File(System.getProperty('java.io.tmpdir'), testName)