From commits-return-5743-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Mon Mar 5 00:55:35 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 3B4DB180649 for ; Mon, 5 Mar 2018 00:55:35 +0100 (CET) Received: (qmail 40051 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 40042 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 35DC1F351F; 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:34 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/2] groovy git commit: GROOVY-8475: unable to instantiate objects using the "new" keyword in groovysh Repository: groovy Updated Branches: refs/heads/GROOVY_2_4_X 730dc5a8c -> 2aa0a54ed GROOVY-8475: unable to instantiate objects using the "new" keyword in groovysh Backport fix "GROOVY-7562 Groovysh: Fix custom class instantiation impossible with Interpreter Mode" for the 2_4_X branch. Retain binary compatibilty by retaining and deprecating methods removed in the original fix that was applied to 2_5_X. Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ba00a0ad Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ba00a0ad Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ba00a0ad Branch: refs/heads/GROOVY_2_4_X Commit: ba00a0ad107ab3c10edc1c53de557b47c52c28e6 Parents: 730dc5a Author: John Wagenleitner Authored: Sat Mar 3 17:53:40 2018 -0800 Committer: John Wagenleitner Committed: Sun Mar 4 13:35:51 2018 -0800 ---------------------------------------------------------------------- .../codehaus/groovy/tools/shell/Groovysh.groovy | 2 +- .../shell/util/ScriptVariableAnalyzer.groovy | 19 ++++++++++++++++++- .../groovy/tools/shell/GroovyshTest.groovy | 6 +++++- 3 files changed, 24 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/ba00a0ad/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 21f17e6..da81429 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 @@ -237,7 +237,7 @@ class Groovysh extends Shell { String variableBlocks = '' // To make groovysh behave more like an interpreter, we need to retrive 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)) + Set boundVars = ScriptVariableAnalyzer.getBoundVars(current.join(Parser.NEWLINE), interp.classLoader) variableBlocks += "$COLLECTED_BOUND_VARS_MAP_VARNAME = new HashMap();" if (boundVars) { boundVars.each({ String varname -> http://git-wip-us.apache.org/repos/asf/groovy/blob/ba00a0ad/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/ScriptVariableAnalyzer.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/ScriptVariableAnalyzer.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/ScriptVariableAnalyzer.groovy index 27635be..294720f 100644 --- a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/ScriptVariableAnalyzer.groovy +++ b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/ScriptVariableAnalyzer.groovy @@ -93,10 +93,19 @@ class ScriptVariableAnalyzer { static class VisitorClassLoader extends GroovyClassLoader { final GroovyClassVisitor visitor + /** + * @deprecated will be removed in 2.5.0, use {@link #VisitorClassLoader(GroovyClassVisitor, ClassLoader)} + */ + @Deprecated VisitorClassLoader(final GroovyClassVisitor visitor) { this.visitor = visitor } + VisitorClassLoader(final GroovyClassVisitor visitor, ClassLoader parent) { + super(parent == null ? Thread.currentThread().getContextClassLoader() : parent) + this.visitor = visitor + } + @Override protected CompilationUnit createCompilationUnit(final CompilerConfiguration config, final CodeSource source) { CompilationUnit cu = super.createCompilationUnit(config, source) @@ -105,10 +114,18 @@ class ScriptVariableAnalyzer { } } + /** + * @deprecated will be removed in 2.5.0, use {@link #getBoundVars(java.lang.String, java.lang.ClassLoader)} + */ + @Deprecated static Set getBoundVars(final String scriptText) { + getBoundVars(scriptText, null); + } + + static Set getBoundVars(final String scriptText, ClassLoader parent) { assert scriptText != null GroovyClassVisitor visitor = new VariableVisitor() - VisitorClassLoader myCL = new VisitorClassLoader(visitor) + VisitorClassLoader myCL = new VisitorClassLoader(visitor, parent) // simply by parsing the script with our classloader // our visitor will be called and will visit all the variables myCL.parseClass(scriptText) http://git-wip-us.apache.org/repos/asf/groovy/blob/ba00a0ad/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 4cfe851..7a0f6e1 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 @@ -62,9 +62,13 @@ class GroovyshTest extends GroovyTestCase { void testClassDef() { Groovysh groovysh = createGroovysh() - groovysh.execute('class Foo {}') + groovysh.execute('class MyFooTestClass{ String foo }') assert mockOut.toString().length() > 0 assert ' true\n' == mockOut.toString().normalize()[-6..-1] + groovysh.execute('m = new MyFooTestClass()') + assert mockOut.toString().length() > 0 + // mostly assert no exception + assert mockOut.toString().normalize().contains('MyFooTestClass@') } void testmethodDef() {