Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

[groovy-user] A good enhancement to annotations support?

Marc Palmer

2010-08-26


Author LoginPost Reply
Hi all,

I was wondering if a future tweak to annotation support could make some really nice things possible in Groovy.

Use case: We have custom DSLs that we take as text snippets, push into a groovy template string we have internally and then eval the script, get the closure property and clone() and execute it on demand.

So the user's input text might look like:

def s = new URL('http://grails.org').text
render stripHTML(s)

...but internally after processing we turn it into something like:

code = {
 def s = new URL('http://grails.org').text
 render stripHTML(s)
}

...which we eval as a script, and get "code" from the binding.

Anyway so far so good - the problem here is that what if the user needs to import classes? Yes they can declare them fully qualified, and assign them to vars etc, but this is a bit of a workaround and can hide dependencies. You can't use "import" here because import is not valid inside a Closure (sensibly).

However, what if Groovy permitted annotations inline in Closure bodies, and bound those annotations to the Closure's class? Our code could then support an annotation for import, and extract the list of annotations for the closure before building up the internal representation of the code for evaluation:

@Import('com.somecorp.usefulstuff.*')

def s = new UsefulClass('http://grails.org').text
render stripHTML(s)

...we'd then be able to extract that info from the closure instance and create source like:

import com.somecorp.usefulstuff.*
code = {
 def s = new UsefulClass('http://grails.org').text
 render stripHTML(s)
}

They could also potentially use @Grab etc. After all, a Closure compiles to a Class, so why shouldn't annotations *inside* the closure be applied to the Closure class? We could have all kinds of things like annotations for preconditions that apply to the code when executed etc.

Some people will no doubt puke over this idea, but perhaps it has mileage?

Getting metadata out of DSLs seems a tricky problem - because you don't want to have to execute them to be able to extract the metadata.

Also, I don't like to have DSLs as fully-fledged Groovy Scripts because I haven't yet found a way to effectively supply a custom Script base class / support delegation in the way you can so easily with Closures.


Marc
~ ~ ~
Marc Palmer
Blog      > http://www.anyware.co.uk
Twitter    > http://twitter.com/wangjammer5
Grails Rocks > http://www.grailsrocks.com







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

  http://xircles.codehaus.org/manage_email


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