Adapting the generated java code from xtend [message #1849903] |
Tue, 08 February 2022 09:44  |
Eclipse User |
|
|
|
Hi,
My goal is to change the xtend compiler such that methods returning a string return a slightly altered string. This should only happen on methods that I annotate with a specific annotation. I came across https://www.eclipse.org/xtend/documentation/204_activeannotations.html#active-annotations-processor and think that this is the way to proceed.
So a method like
@CodeGeneration
def String generateFunctionName() {
'''Foo'''
}
should generate similar to:
@CodeGeneration
public String generateFunctionName() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Foo");
return "__" + _builder.toString() + "__";
}
I started with extending the AbstractMethodProcessor. Its doTransform method allows access to the annotatedMethod. I can even get the body of that method. However, annotatedMethod.body returns an Expression but I don't know how to modify that expression. I can imagine that different subtypes of Expression exist, like a BlockExpression that contains a list of subexpressions. If so, how could I add an additional expression to such block?
Or am I at the wrong track here?
Regards,
Wilbert.
|
|
|
Re: Adapting the generated java code from xtend [message #1849904 is a reply to message #1849903] |
Tue, 08 February 2022 09:49   |
Eclipse User |
|
|
|
you cannot alter the code, but you can infer a second method that has manual code and calls the original one. you might be able to rename the original method by calling setSimpleName but i have not tested that
so the target structure would be
@CodeGeneration
public String generateFunctionNameOriginal() {
xxxx
}
public String generateFunctionName() {
bla
generateFunctionNameOriginal();
blubb
}
|
|
|
Re: Adapting the generated java code from xtend [message #1849905 is a reply to message #1849904] |
Tue, 08 February 2022 10:29  |
Eclipse User |
|
|
|
Hi Christian,
Thanks! This works.
For a moment, I was afraid that changing the name of the original method would also modify any invocations of it. That wasn't the case so indeed, any existing invocations now call the newly added method (which calls the original method and adapts the result).
Regards,
Wilbert.
|
|
|
Powered by
FUDForum. Page generated in 0.02577 seconds