However strange it sounds I need to modify some Groovy code by modifying how variables are being created.
I have previously solved some other related problem with ASTTransformation when I needed to “mimic” the Use/Category-functionality by modifying the code to invoke my custom special method.
What I need now to do is this:
I need the following type of code:
def a = "X";
Integer b = 1;
to behave as this code:
a = "X";
b = 1;
The difference is that the second example is “stored” in the binding-scope.
Some context:
I’m writing a groovy template engine, where I have some code-fragment (provided by the user) that gets placed into a method in a groovy class / file, which I then compile. When I run the code-fragment/method I need its created variables to be accessible via the Binding-mechanisme.
I hope someone can help me in the correct direction..
Thanks in advance.
UPDATE:
(thanks for the great answer)
I have another question:
I think it can work when I modify all declarations, but it would be better if I only modified the ones in the most outer block. An example:
Object aMethod() {
def a = 1; //should be modified
def b = 2; //should be modified
if (expression) {
def c = 2; //does not need to be modified
}
}
Do you know how it is possible to detect if the declaration is in the most outmost block (main block) in the method body?
-Morten
You will have to replace the DeclarationExpressions in the AST with a BinaryExpression. You will notice the DeclarationExpression is a BinaryExpression, so you simply have to copy over everything. Also don’t forget to call setSourcePosition on the new node using the old node to ensure the line numbers are right. The Expressions are stored in ExpressionStatements, making it not too difficult to replace them.
As for in which phase to operate… I strongly recommend to do this before the variable scopes are set, then you have less work and you won’t have to delete the many references to the variables in AST later. You could do this for example in CONVERSION.
One more word on the semantic changes you introduce with this:
(1) String a = 1; a = 2; in this code example you will get two times a String being stored in a. This is essentially because for every assignment Groovy will do a Groovy style cast and that includes the transformation to String here. This is done because the variable is typed. If it is not typed such transformations will not happen. That is the case for “def”, but also for variables in the binding. Meaning your binding would store the integers 1 and 2, not the Strings.
(2) The other point is scopes. If I understand you right, then you want to make all variables global. Programs written with the context of having only a block wide scope for the variable may then behave strange. This can happen especially for variables in Groovy Closures if the Groovy Closures are executed not synchronously, but for example a later, more detached point in your program
(3) The last point is a change in the usage of the MOP. The get the value of a local variable we don’t use the MOP, instead we do it directly. If you move the local variables into the global scope, you will have to go through the complete MOP to get the values. If at some point you have a program element capturing the call and repeating on its own, you will have get a different program.
Maybe that doesn’t matter in your case… I just wanted you to be aware of these things.