I'm trying to write a transformation which does explicitly what any Java compiler does while compiling: I want to replace string concatenation by creation and adding to a StringBuilder.
I'm running into two problems:
* how can I distinguish between the case above an a case like "x = y + 1", which will also appear as a "Plus" in the AST. I don't see a way I can query for y's type when I see its VariableReference. Am I overlooking something?
In the AST, when I see a Plus, I want to replace both the children with calls to the stringbuilder I add in. If a child is also a Plus, I need to recurse. How do I get the TreeWalker to continue with the next node after the first Plus I encounter?
Thanks in advance for any insight!
~David
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ok, on a second thought about the typing of a VariableReference I encounter, I guess I can build up my own "symbol table" for the types. But obviously that'd be quite a bit of trouble since I'd need to keep track of scope. I guess my hope is something like that already exists :-D.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The first problem is quite easy. If you have the ServiceConfiguration (in variable sc), this will help:
Plus plus = ...;
Type t = sc.getSourceInfo().getType(plus);
This doesn't necessarily mean that both children of "plus" are of type String. You can check if you need to create calls to "toString()" by doing this:
Type lhs = sc.getSourceInfo().getType(plus.getChildAt(0)); // left-hand site
Type rhs = sc.getSourceInfo().getType(plus.getChildAt(1)); // right-hand site
In general, you can easily check if a variable is of type java.lang.String:
if (t == sc.getNameInfo().getJavaLangString()) {
// it is java.lang.String
} else {
// no...
}
For the second problem I only have a suggestion that you can try out: Use CustomTreeWalker instead of TreeWalker. CustomTreeWalker supports doing an action when finishing the traversal of the subtree, i.e., it's something like an "endVisit" instead of a "beginVisit" of an AST note.
Hope I could help!
/Tobias
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, another question from me :-).
I'm trying to write a transformation which does explicitly what any Java compiler does while compiling: I want to replace string concatenation by creation and adding to a StringBuilder.
For example:
String foo = "bar";
System.out.println ("foo = " + foo);
String foo = "bar";
StringBuilder sb = new StringBuilder();
sb.add ("foo = ");
sb.add (foo);
System.out.println (sb.toString());
I'm running into two problems:
* how can I distinguish between the case above an a case like "x = y + 1", which will also appear as a "Plus" in the AST. I don't see a way I can query for y's type when I see its VariableReference. Am I overlooking something?
Thanks in advance for any insight!
~David
Thanks! Yes, that was very helpful information.
Ok, on a second thought about the typing of a VariableReference I encounter, I guess I can build up my own "symbol table" for the types. But obviously that'd be quite a bit of trouble since I'd need to keep track of scope. I guess my hope is something like that already exists :-D.
Hej,
The first problem is quite easy. If you have the ServiceConfiguration (in variable sc), this will help:
Plus plus = ...;
Type t = sc.getSourceInfo().getType(plus);
This doesn't necessarily mean that both children of "plus" are of type String. You can check if you need to create calls to "toString()" by doing this:
Type lhs = sc.getSourceInfo().getType(plus.getChildAt(0)); // left-hand site
Type rhs = sc.getSourceInfo().getType(plus.getChildAt(1)); // right-hand site
In general, you can easily check if a variable is of type java.lang.String:
if (t == sc.getNameInfo().getJavaLangString()) {
// it is java.lang.String
} else {
// no...
}
For the second problem I only have a suggestion that you can try out: Use CustomTreeWalker instead of TreeWalker. CustomTreeWalker supports doing an action when finishing the traversal of the subtree, i.e., it's something like an "endVisit" instead of a "beginVisit" of an AST note.
Hope I could help!
/Tobias