Adapting the generated java code from xtend [message #1849903] |
Tue, 08 February 2022 04: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 #1849905 is a reply to message #1849904] |
Tue, 08 February 2022 05: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 1.07696 seconds