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 <[email protected]> Authored: Sat Mar 3 17:53:40 2018 -0800 Committer: John Wagenleitner <[email protected]> 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<String> boundVars = ScriptVariableAnalyzer.getBoundVars(current.join(Parser.NEWLINE)) + Set<String> 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<String> getBoundVars(final String scriptText) { + getBoundVars(scriptText, null); + } + + static Set<String> 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() {
