Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

[groovy-user] Dynamic invocation: useful but broken

GroovyFlow

2008-08-01

Replies: Find Java Web Hosting

Author LoginPost Reply

It's possible to dynamically instantiate an object from inside a script
that's being run by the GroovyScriptEngine. To do so, you set up the
GroovyScriptEngine like this:

String[] roots = new String[] { "some/path" };
GroovyScriptEngine gse = new GroovyScriptEngine(roots)
Binding binding= new Binding();
binding.setVariable("className", someClassName);
gse.run("theScript.groovy", binding);

And inside theScript (which is located inside some/path), your code looks
like this:
Class clazz = Class.forName(className, true, getClass().getClassLoader()) ;
def obj = clazz.newInstance()

This is an extremely useful technique. A variant of it appears in the
GroovyFlow project. There the technique allows GroovyFlow to parse a URL
and figure out which controller to invoke, all from inside a script
(assuming you're running in dev mode). It turns out that this results in
an entire source tree of code that reliably reloads when your application
server is running, no matter the extent or type of modifications you make.

Unfortunately, this technique depends on fixing a bug in the
GroovyScriptEngine. The fix is trivial. (I posted it in
http://jira.codehaus.org/browse/GROOVY-2861). but it hasn't been implemented
yet.

That, in itself, isn't a huge deal. (And it's not a problem for GroovyFlow,
which comes with a bug-fixed version of the GroovyScriptEngine.) What I'm
worried about, though, is that if the bug isn't fixed in the Groovy
distribution, that surely means there's no Groovy test case that excercises
dynamic invocation inside a script. If there's no test case, then it's very
possible that, in future Groovy versions, dynamic invocation may become
irrevocably lost.

So I'm asking the Groovy developers if it would be possible to add one or
more such test cases. To my mind, this kind of dynamic invocation is a key
to the growth of Groovy as a truly dynamic language. Therefore, I hope that
this functionality is protected in future Groovy releases.


--
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.