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 2015/09/01 20:39:11 UTC

[3/3] incubator-groovy git commit: GROOVY-7562: Groovysh: Fix custom class instantiation impossible with Interpreter Mode (closes #100)

GROOVY-7562: Groovysh: Fix custom class instantiation impossible with Interpreter Mode (closes #100)


Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/6b80966a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/6b80966a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/6b80966a

Branch: refs/heads/master
Commit: 6b80966a4e50b0e24b86832eaa01be96d2dded3d
Parents: 1d98838
Author: Thibault Kruse <th...@gmx.de>
Authored: Fri Aug 28 12:19:38 2015 +0200
Committer: pascalschumacher <pa...@gmx.net>
Committed: Tue Sep 1 20:38:42 2015 +0200

----------------------------------------------------------------------
 .../org/codehaus/groovy/tools/shell/Groovysh.groovy   |  2 +-
 .../tools/shell/util/ScriptVariableAnalyzer.groovy    |  7 ++++---
 .../codehaus/groovy/tools/shell/GroovyshTest.groovy   |  7 ++++++-
 .../shell/util/ScriptVariableAnalyzerTest.groovy      | 14 +++++++++-----
 4 files changed, 20 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/6b80966a/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 8ea3dba..9343dbd 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
@@ -245,7 +245,7 @@ class Groovysh extends Shell {
         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<String> boundVars = ScriptVariableAnalyzer.getBoundVars(importsSpec + Parser.NEWLINE + current.join(Parser.NEWLINE))
+        Set<String> 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 ->

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/6b80966a/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 3bed8b2..ed3bab4 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
@@ -89,7 +89,8 @@ class ScriptVariableAnalyzer {
     static class VisitorClassLoader extends GroovyClassLoader {
         final GroovyClassVisitor visitor
 
-        VisitorClassLoader(final GroovyClassVisitor visitor) {
+        VisitorClassLoader(final GroovyClassVisitor visitor, ClassLoader parent) {
+            super(parent == null ?  Thread.currentThread().getContextClassLoader() : parent)
             this.visitor = visitor
         }
 
@@ -101,10 +102,10 @@ class ScriptVariableAnalyzer {
         }
     }
 
-    static Set<String> getBoundVars(final String scriptText) {
+    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/incubator-groovy/blob/6b80966a/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 6e49a80..d497aec 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
@@ -51,11 +51,16 @@ class GroovyshTest extends GroovyTestCase {
 
     void testClassDef() {
         Groovysh groovysh = new Groovysh(testio)
-        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() {
         Groovysh groovysh = new Groovysh(testio)
         groovysh.execute('int foo() {42}')

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/6b80966a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/util/ScriptVariableAnalyzerTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/util/ScriptVariableAnalyzerTest.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/util/ScriptVariableAnalyzerTest.groovy
index e595eca..1ce5178 100644
--- a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/util/ScriptVariableAnalyzerTest.groovy
+++ b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/util/ScriptVariableAnalyzerTest.groovy
@@ -25,7 +25,11 @@ package org.codehaus.groovy.tools.shell.util
 class ScriptVariableAnalyzerTest extends GroovyTestCase {
 
     void testEmptyScript() {
-        assert [] as Set == ScriptVariableAnalyzer.getBoundVars('')
+        assert [] as Set == ScriptVariableAnalyzer.getBoundVars('', Thread.currentThread().contextClassLoader)
+    }
+
+    void testEmptyScriptNullLOader() {
+        assert [] as Set == ScriptVariableAnalyzer.getBoundVars('', null)
     }
 
     void testBound() {
@@ -33,7 +37,7 @@ class ScriptVariableAnalyzerTest extends GroovyTestCase {
    int a = 6
    String b = "7"
 '''
-        assert ['a', 'b'] as Set == ScriptVariableAnalyzer.getBoundVars(scriptText)
+        assert ['a', 'b'] as Set == ScriptVariableAnalyzer.getBoundVars(scriptText, Thread.currentThread().contextClassLoader)
     }
 
     void testUnBound() {
@@ -41,7 +45,7 @@ class ScriptVariableAnalyzerTest extends GroovyTestCase {
    a = 6
    b = "7"
 '''
-        assert [] as Set == ScriptVariableAnalyzer.getBoundVars(scriptText)
+        assert [] as Set == ScriptVariableAnalyzer.getBoundVars(scriptText, Thread.currentThread().contextClassLoader)
     }
 
     void testMixed() {
@@ -58,6 +62,6 @@ class ScriptVariableAnalyzerTest extends GroovyTestCase {
    }
    assert b
 '''
-        assert ['b', 'c'] as Set == ScriptVariableAnalyzer.getBoundVars(scriptText)
+        assert ['b', 'c'] as Set == ScriptVariableAnalyzer.getBoundVars(scriptText, Thread.currentThread().contextClassLoader)
     }
-}
\ No newline at end of file
+}