Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Customizing generated java classes(Xtext generates a number of EMF classes. How can I custoize them?)
Customizing generated java classes [message #657064] Tue, 01 March 2011 14:01 Go to next message
Balint Torok is currently offline Balint TorokFriend
Messages: 7
Registered: July 2009
Junior Member
Hello!

I have a nice little grammar that works very well. After running the workflow a number of classes are generated for representing the language elements. What is the proper way of customizing these generated classes? If I just add some code to them, my modifications will be gone after running the workflow again. All I want to do is to add some methods to these classes.

Thanks,
Balint
Re: Customizing generated java classes [message #657153 is a reply to message #657064] Tue, 01 March 2011 17:39 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Balint,

I'd recommend the generation gap pattern:
see http://en.wikipedia.org/wiki/Generation_gap or
http://heikobehrens.net/2009/04/23/generation-gap-pattern/

You may also want to read the chapter "Output configuration" in the
Xpand Eclipse help.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 01.03.11 15:01, schrieb Balint Torok:
> Hello!
>
> I have a nice little grammar that works very well. After running the
> workflow a number of classes are generated for representing the language
> elements. What is the proper way of customizing these generated classes?
> If I just add some code to them, my modifications will be gone after
> running the workflow again. All I want to do is to add some methods to
> these classes.
>
> Thanks,
> Balint
Re: Customizing generated java classes [message #657163 is a reply to message #657064] Tue, 01 March 2011 18:48 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

if Sebastian's generation gap pattern approach does not work for you, here are some other "workarounds."
You might have a look at the metamodel postprocessing section of the documentation. Maybe you can cause the changes you want even before the model classes are generated.
If your grammar is somewhat stable, switch to the use of imported meta models (instead of generating them from the grammar). Then you have full control over the classes. Afterwards, you will have to keep grammar and metamodel in synch, if you want to change something, though.
One possible hack would be to write your own workflow component that walks over the generated model files and adds the code you want.

Alex
Re: Customizing generated java classes [message #658070 is a reply to message #657163] Sat, 05 March 2011 22:43 Go to previous messageGo to next message
Balint Torok is currently offline Balint TorokFriend
Messages: 7
Registered: July 2009
Junior Member
Hi,

great answers. I had success with the imported metamodel. But I'm curious about this generation gap pattern. I think I got the message, but I'm not sure how to use it. I have the generated classes for the model. I have my "manually modified" classes that are subclasses of the generated classes. Now how can I make the xtext-generated editor use my "manual" classes instead of the generated classes?

Thanks,
Balint
Re: Customizing generated java classes [message #658076 is a reply to message #658070] Sun, 06 March 2011 00:27 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Since the imported model is generated from ecore, you could just use the
'@generated not' pattern and let the emf code generator handle your
changes. Then you do not have to do any additional work in your xtext
grammar.

As a recommendation, I would keep the imported model as anemic as
possible, and with as few manually modified methods as possible. Place
all other code (processing, runtime state, etc) elsewhere using suitable
patterns.

In my first language implementation with Xtext I started out with far
too much implementation inside the classes used in the grammar, and this
turned out to be a bad idea - not so much from a code generation
standpoint, but more that functionality tended to be spread out and
require lots of editing in lots of places. I was much happier with an
anemic model and using Xtext polymorphic dispatchers to keep related
logic together. I also found adapters to be very useful to keep the
model lean (I used them for things like type inference information).

BTW, I am also curious about best practices when using the "generation
gap" pattern for an imported model. I can imagine customizing the
MyDSLFactory and returning instances of the concrete impl classes
instead of the generated "abstract" classes - is that how it is supposed
to be done?

Regards
- henrik



On 3/5/11 11:43 PM, Balint Torok wrote:
> Hi,
>
> great answers. I had success with the imported metamodel. But I'm
> curious about this generation gap pattern. I think I got the message,
> but I'm not sure how to use it. I have the generated classes for the
> model. I have my "manually modified" classes that are subclasses of the
> generated classes. Now how can I make the xtext-generated editor use my
> "manual" classes instead of the generated classes?
>
> Thanks,
> Balint
Re: Customizing generated java classes [message #658211 is a reply to message #658076] Mon, 07 March 2011 12:27 Go to previous message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
You could also use the MWE component
org.eclipse.emf.mwe2.ecore.EcoreGenerator
to generate your EMF classes. This one can be configured to
automatically pick up manually enhanced implementation classes following
a naming convention.


Am 06.03.11 01:27, schrieb Henrik Lindberg:
> Since the imported model is generated from ecore, you could just use the
> '@generated not' pattern and let the emf code generator handle your
> changes. Then you do not have to do any additional work in your xtext
> grammar.
>
> As a recommendation, I would keep the imported model as anemic as
> possible, and with as few manually modified methods as possible. Place
> all other code (processing, runtime state, etc) elsewhere using suitable
> patterns.
>
> In my first language implementation with Xtext I started out with far
> too much implementation inside the classes used in the grammar, and this
> turned out to be a bad idea - not so much from a code generation
> standpoint, but more that functionality tended to be spread out and
> require lots of editing in lots of places. I was much happier with an
> anemic model and using Xtext polymorphic dispatchers to keep related
> logic together. I also found adapters to be very useful to keep the
> model lean (I used them for things like type inference information).
>
> BTW, I am also curious about best practices when using the "generation
> gap" pattern for an imported model. I can imagine customizing the
> MyDSLFactory and returning instances of the concrete impl classes
> instead of the generated "abstract" classes - is that how it is supposed
> to be done?
>
> Regards
> - henrik
>
>
>
> On 3/5/11 11:43 PM, Balint Torok wrote:
>> Hi,
>>
>> great answers. I had success with the imported metamodel. But I'm
>> curious about this generation gap pattern. I think I got the message,
>> but I'm not sure how to use it. I have the generated classes for the
>> model. I have my "manually modified" classes that are subclasses of the
>> generated classes. Now how can I make the xtext-generated editor use my
>> "manual" classes instead of the generated classes?
>>
>> Thanks,
>> Balint
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Previous Topic:Build and add hidden EObjects to XtextEditor at runtime...
Next Topic:Deploying plugin succeeds, editor does not show
Goto Forum:
  


Current Time: Thu Apr 25 03:44:52 GMT 2024

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

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

Back to the top