Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Adapting the generated java code from xtend(How to change body of generated method using active annotations)
Adapting the generated java code from xtend [message #1849903] Tue, 08 February 2022 09:44 Go to next message
Wilbert Alberts is currently offline Wilbert AlbertsFriend
Messages: 205
Registered: June 2010
Senior Member
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 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14394
Registered: July 2009
Senior Member
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
}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Adapting the generated java code from xtend [message #1849905 is a reply to message #1849904] Tue, 08 February 2022 10:29 Go to previous message
Wilbert Alberts is currently offline Wilbert AlbertsFriend
Messages: 205
Registered: June 2010
Senior Member
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.
Previous Topic:Disable validation rule if editor state is dirty
Next Topic:Cascading Xtext Generators
Goto Forum:
  


Current Time: Fri Dec 02 00:48:58 GMT 2022

Powered by FUDForum. Page generated in 0.02725 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top