|
So what I want to do is make an AST transformation for a closure(grails controller action) that add code around the closure body. My first instinct was to use new AstBuilder().buildFromSpec make a new closure with the dsl and just copy everything from the old closure something like this:
ClosureExpression annotatedClosure = (ClosureExpression)astNodes[1]
Statement closureStatement = annotatedClosure.getCode()
def ast = new AstBuilder().buildFromSpec {
closure {
parameters {
parameter 'parms': Object.class // need to transfer prams from original here
}
block {
//add some code here
owner.expression.addAll closureStatement //add the original statement
//add some code here
}
}
}
Now I'm not sure if any of that is correct at this point, but the problem that I'm going to get into is how to copy all the parameters over, in the context of the dsl. Or is there another/better way to do it. Keep in mind that for now I'm on grails 1.3.4 which means groovy 1.7.4 and the ClosureExpression does not have a set Method. Any suggestions would be appreciated, I've been trying to make sense of various tutorials, and the documentation. So I thought maybe I could just ask, and someone could point me in the right direction.
|
|
Hi,
The easiest way is to retrieve the code from the closure and add what you want inside. For example : ClosureExpression annotatedClosure = (ClosureExpression)astNodes[1]
Statement code = annotatedClosure.getCode()
BlockStatement newCode = new BlockStatement()
// add your "before" statements here
// newCode.addStatement(...)
if (code instanceof BlockStatement) {
BlockStatement oldCode = (BlockStatement) code
oldCode.statements.each { newCode.addStatement() }
}
// add your "after" statements here
// newCode.addStatement (...)
annotatedClosure.code = newCode
That's all ;)Le 27/01/2012 18:25, virtualdogbert a écrit : So what I want to do is make an AST transformation for a closure(grails controller action) that add code around the closure body. My first instinct was to use new AstBuilder().buildFromSpec make a new closure with the dsl and just copy everything from the old closure something like this: -- Cédric Champeau SpringSource - A Division Of VMware http://www.springsource.com/ http://twitter.com/CedricChampeau |
|
Am 27.01.2012 18:33, schrieb Cédric Champeau:
> Hi, > > The easiest way is to retrieve the code from the closure and add what > you want inside. For example : > > ClosureExpression annotatedClosure = (ClosureExpression)astNodes[1] > Statement code = annotatedClosure.getCode() > BlockStatement newCode = new BlockStatement() > // add your "before" statements here > // newCode.addStatement(...) > > if (code instanceof BlockStatement) { > BlockStatement oldCode = (BlockStatement) code > oldCode.statements.each { newCode.addStatement() } > } > > // add your "after" statements here > // newCode.addStatement (...) > > annotatedClosure.code = newCode > > That's all ;) I want to add one more thing here... if you "copy" code from one Closure to another you need to make a deepcopy. Reusing statements in several Closures can lead to problems, since there is data like the variable scope or generics on it, that may not apply to your code and then be linked up wrong. bye blackdrag -- Jochen "blackdrag" Theodorou - Groovy Project Tech Lead blog: http://blackdragsview.blogspot.com/ german groovy discussion newsgroup: de.comp.lang.misc For Groovy programming sources visit http://groovy-lang.org --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by virtualdogbert
You can have a look at the GContracts source (https://github.com/andresteingress/gcontracts). The project is all about reading and analyzing closure expressions (from Groovy 1.8 annotation closure expressions):
org.gcontracts.ast.visitor.AnnotationClosureVisitor or org.gcontracts.util.ExpressionUtils contain code in that direction... Maybe that helps, Cheers Andre |
|
Thanks for the info everybody, I've got further in my AST endeavors and found
that while I can get the annotation working, I'm not actually get at the closure. I have to figure out how to identify, and pull out the closure, and that looks like where Andre's post comes in. Unfortunately I'm going to have to put this on my back burner(in my spare time, that I don't have any of), as the AST for this is would be a nice to have, making the code a bit more DRY. -- View this message in context: http://groovy.329449.n5.nabble.com/Trying-to-figure-out-AST-on-a-Closure-tp5436087p5441757.html Sent from the groovy - user mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Powered by Nabble | See how NAML generates this page |
