<p>I'm trying to use Groovy to create an interactive scripting / macro mode for my application. The application is OSGi and much of the information the scripts may need is not know up front. I figured I could use GroovyShell and call eval() multiple times continually appending to the namespace as OSGi bundles are loaded. GroovyShell maintains variable state over multiple eval calls, but not class definitions or methods.</p>

<p>goal: Create a base class during startup. As OSGi bundles load, create derived classes as needed. </p>

[{'answer_id': 46115, 'author': 'toolkit', 'author_id': 3295, 'author_profile': 'https://Stackoverflow.com/users/3295', 'pm_score': 0, 'selected': False, 'text': '<p>This might be what you are looking for?</p>\n\n<p>From <a href="https://rads.stackoverflow.com/amzn/click/com/1932394842" rel="nofollow noreferrer" rel="nofollow noreferrer">Groovy in Action</a></p>\n\n<pre><code>def binding = new Binding(x: 6, y: 4)\ndef shell = new GroovyShell(binding)\ndef expression = \'\'\'f = x * y\'\'\'\nshell.evaluate(expression)\nassert binding.getVariable("f") == 24\n</code></pre>\n\n<p>An appropriate use of Binding will allow you to maintain state?</p>\n'}, {'answer_id': 64063, 'author': 'shemnon', 'author_id': 8020, 'author_profile': 'https://Stackoverflow.com/users/8020', 'pm_score': 2, 'selected': False, 'text': '<p>I am not sure about what you mean about declared classes not existing between evals, the following two scripts work as expected when evaled one after another:</p>\n\n<pre><code>class C {{println \'hi\'}}\nnew C()\n</code></pre>\n\n<p>... </p>\n\n<pre><code>new C()\n</code></pre>\n\n<p>However methods become bound to the class that declared them, and GroovyShell creates a new class for each instance.  If you do not need the return value of any of the scripts and they are truly scripts (not classes with main methods) you can attach the following to the end of every evaluated scrips.</p>\n\n<pre><code>Class klass = this.getClass()\nthis.getMetaClass().getMethods().each {\n  if (it.declaringClass.cachedClass == klass) {\n    binding[it.name] = this.&amp;"$it.name"\n  }\n}\n</code></pre>\n\n<p>If you depend on the return value you can hand-manage the evaluation and run the script as part of your parsing (warning, untested code follows, for illustrative uses only)...</p>\n\n<pre><code>String scriptText = ...\nScript script = shell.parse(scriptText)\ndef returnValue = script.run()\nClass klass = script.getClass()\nscript.getMetaClass().getMethods().each {\n  if (it.declaringClass.cachedClass == klass) {\n    shell.context[it.name] = this.&amp;"$it.name"\n  }\n}\n// do whatever with returnValue...\n</code></pre>\n\n<p>There is one last caveat I am sure you are aware of.  Statically typed variables are not kept between evals as they are not stored in the binding.  So in the previous script the variable \'klass\' will not be kept between script invocations and will disappear.  To rectify that simply remove the type declarations on the first use of all variables, that means they will be read and written to the binding.</p>\n'}, {'answer_id': 1380478, 'author': 'basszero', 'author_id': 287, 'author_profile': 'https://Stackoverflow.com/users/287', 'pm_score': 2, 'selected': True, 'text': '<p>Ended up injecting code before each script compilation. End goal is that the user written script has a domain-specific-language available for use.</p>\n'}]
['https://Stackoverflow.com/questions/45582', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/287/']