Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to merge custom code into generated methods?
How to merge custom code into generated methods? [message #909019] Thu, 06 September 2012 12:30 Go to next message
Eclipse UserFriend
Hello,
I want to let the model code be generated via the normal genmodel code
generation. But, afterwards, I want to add a variable to some classes
and want to add some custom code to already generated methods, such as,
e.g., setMyUnaryFeature(..). This means, before the method is closed
some additional code should be added/merged into it. Can I use JET as it
is done by Ecore itself since I want to merge code and not only to add code?

best regards,
Gilbert
Re: How to merge custom code into generated methods? [message #909061 is a reply to message #909019] Thu, 06 September 2012 13:15 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi, Gilbert,

For "one-off" cases where you just need to decorate the generated
method's behaviour with some before and/or after code, it is often
sufficient to rename the generated method with a "Gen" suffix and
replace the original method with your code, delegating to the "Gen"
method at the right place.

For example, if you have

/** @generated */
public void setFoo(Foo newValue) {
...
}

you can change this to:

/** @generated */
public void setFooGen(Foo newValue) {
...
}

public void setFoo(Foo newValue) {
// do something before
setFooGen(newValue);
// do something after
}

EMF will maintain the *Gen method in subsequent regeneration of the model.

HTH,

Christian


On 2012-09-06 12:30:58 +0000, Gilbert Mirenque said:

> Hello,
> I want to let the model code be generated via the normal genmodel code
> generation. But, afterwards, I want to add a variable to some classes
> and want to add some custom code to already generated methods, such as,
> e.g., setMyUnaryFeature(..). This means, before the method is closed
> some additional code should be added/merged into it. Can I use JET as it
> is done by Ecore itself since I want to merge code and not only to add code?
>
> best regards,
> Gilbert
Re: How to merge custom code into generated methods? [message #909091 is a reply to message #909061] Thu, 06 September 2012 14:12 Go to previous messageGo to next message
Eclipse UserFriend
Hi Christian,
thanks for your reply. I didn't know the *Gen feature. But, since I want
to make the generation I intend available for colleagues and several
metamodels I need to customize the generation process anyway. I don't
want to add setFooGen manually because depending on the structural
features it can be a lot. Is there any possibility to hook into the
default generation?

best regards,
Gilbert

Christian W. Damus wrote:
> Hi, Gilbert,
>
> For "one-off" cases where you just need to decorate the generated
> method's behaviour with some before and/or after code, it is often
> sufficient to rename the generated method with a "Gen" suffix and
> replace the original method with your code, delegating to the "Gen"
> method at the right place.
>
> For example, if you have
>
> /** @generated */
> public void setFoo(Foo newValue) {
> ...
> }
>
> you can change this to:
>
> /** @generated */
> public void setFooGen(Foo newValue) {
> ...
> }
>
> public void setFoo(Foo newValue) {
> // do something before
> setFooGen(newValue);
> // do something after
> }
>
> EMF will maintain the *Gen method in subsequent regeneration of the model.
>
> HTH,
>
> Christian
>
>
> On 2012-09-06 12:30:58 +0000, Gilbert Mirenque said:
>
>> Hello,
>> I want to let the model code be generated via the normal genmodel code
>> generation. But, afterwards, I want to add a variable to some classes
>> and want to add some custom code to already generated methods, such as,
>> e.g., setMyUnaryFeature(..). This means, before the method is closed
>> some additional code should be added/merged into it. Can I use JET as it
>> is done by Ecore itself since I want to merge code and not only to add
>> code?
>>
>> best regards,
>> Gilbert
>
>
Re: How to merge custom code into generated methods? [message #909119 is a reply to message #909091] Thu, 06 September 2012 15:07 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Gilbert,

Yes, with dynamic templates (see FAQ) you can modify the templates or
make use of the "<%@ include" points to specify snippets that are
inserted into the standard templates.

On 06/09/2012 4:12 PM, Gilbert Mirenque wrote:
> Hi Christian,
> thanks for your reply. I didn't know the *Gen feature. But, since I want
> to make the generation I intend available for colleagues and several
> metamodels I need to customize the generation process anyway. I don't
> want to add setFooGen manually because depending on the structural
> features it can be a lot. Is there any possibility to hook into the
> default generation?
>
> best regards,
> Gilbert
>
> Christian W. Damus wrote:
>> Hi, Gilbert,
>>
>> For "one-off" cases where you just need to decorate the generated
>> method's behaviour with some before and/or after code, it is often
>> sufficient to rename the generated method with a "Gen" suffix and
>> replace the original method with your code, delegating to the "Gen"
>> method at the right place.
>>
>> For example, if you have
>>
>> /** @generated */
>> public void setFoo(Foo newValue) {
>> ...
>> }
>>
>> you can change this to:
>>
>> /** @generated */
>> public void setFooGen(Foo newValue) {
>> ...
>> }
>>
>> public void setFoo(Foo newValue) {
>> // do something before
>> setFooGen(newValue);
>> // do something after
>> }
>>
>> EMF will maintain the *Gen method in subsequent regeneration of the model.
>>
>> HTH,
>>
>> Christian
>>
>>
>> On 2012-09-06 12:30:58 +0000, Gilbert Mirenque said:
>>
>>> Hello,
>>> I want to let the model code be generated via the normal genmodel code
>>> generation. But, afterwards, I want to add a variable to some classes
>>> and want to add some custom code to already generated methods, such as,
>>> e.g., setMyUnaryFeature(..). This means, before the method is closed
>>> some additional code should be added/merged into it. Can I use JET as it
>>> is done by Ecore itself since I want to merge code and not only to add
>>> code?
>>>
>>> best regards,
>>> Gilbert
>>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to merge custom code into generated methods? [message #909497 is a reply to message #909119] Fri, 07 September 2012 08:38 Go to previous messageGo to next message
Eclipse UserFriend
Ok, I tried to install JET via Amalgamation but I only get the info that
it is not available anymore. When I look at the category "Modeling" at
http://download.eclipse.org/releases/juno/ it really is not available.
Where to get it?

best regards,
Gilbert

Ed Merks wrote:
> Gilbert,
>
> Yes, with dynamic templates (see FAQ) you can modify the templates or
> make use of the "<%@ include" points to specify snippets that are
> inserted into the standard templates.
>
> On 06/09/2012 4:12 PM, Gilbert Mirenque wrote:
>> Hi Christian,
>> thanks for your reply. I didn't know the *Gen feature. But, since I want
>> to make the generation I intend available for colleagues and several
>> metamodels I need to customize the generation process anyway. I don't
>> want to add setFooGen manually because depending on the structural
>> features it can be a lot. Is there any possibility to hook into the
>> default generation?
>>
>> best regards,
>> Gilbert
>>
>> Christian W. Damus wrote:
>>> Hi, Gilbert,
>>>
>>> For "one-off" cases where you just need to decorate the generated
>>> method's behaviour with some before and/or after code, it is often
>>> sufficient to rename the generated method with a "Gen" suffix and
>>> replace the original method with your code, delegating to the "Gen"
>>> method at the right place.
>>>
>>> For example, if you have
>>>
>>> /** @generated */
>>> public void setFoo(Foo newValue) {
>>> ...
>>> }
>>>
>>> you can change this to:
>>>
>>> /** @generated */
>>> public void setFooGen(Foo newValue) {
>>> ...
>>> }
>>>
>>> public void setFoo(Foo newValue) {
>>> // do something before
>>> setFooGen(newValue);
>>> // do something after
>>> }
>>>
>>> EMF will maintain the *Gen method in subsequent regeneration of the
>>> model.
>>>
>>> HTH,
>>>
>>> Christian
>>>
>>>
>>> On 2012-09-06 12:30:58 +0000, Gilbert Mirenque said:
>>>
>>>> Hello,
>>>> I want to let the model code be generated via the normal genmodel code
>>>> generation. But, afterwards, I want to add a variable to some classes
>>>> and want to add some custom code to already generated methods, such as,
>>>> e.g., setMyUnaryFeature(..). This means, before the method is closed
>>>> some additional code should be added/merged into it. Can I use JET
>>>> as it
>>>> is done by Ecore itself since I want to merge code and not only to add
>>>> code?
>>>>
>>>> best regards,
>>>> Gilbert
>>>
>
Re: How to merge custom code into generated methods? [message #909510 is a reply to message #909119] Fri, 07 September 2012 09:01 Go to previous messageGo to next message
Eclipse UserFriend
Hi Ed,

Ed Merks wrote:
> Yes, with dynamic templates (see FAQ) you can modify the templates or
> make use of the "<%@ include" points to specify snippets that are
> inserted into the standard templates.
Do you mean to copy the templates from org.eclipse.emf.codegen.ecore and
to modify them? I think a better way would be to hook into them in case
the templates change in future.
Re: How to merge custom code into generated methods? [message #909540 is a reply to message #909510] Fri, 07 September 2012 09:42 Go to previous messageGo to next message
Eclipse UserFriend
I had a look at Class.javajet from the org.eclipse.emf.codegen.ecore
plugin. I recognized that there are a lot of such includes:

<%@ include file="Class/basicSetGenFeature.override.javajetinc"
fail="alternative" %>

The URI of the file attribute seems to follow some conventions because
in the project there is no path named "Class". So is this such an
include point, Ed, what you were talking about? Does this mean I need to
provide a template called basicSetGenFeature.override.javajetinc? If so,
how can I register it?

best regards,
Gilbert
Re: How to merge custom code into generated methods? [message #909555 is a reply to message #909540] Fri, 07 September 2012 10:21 Go to previous messageGo to next message
Tim Schaefer is currently offline Tim SchaeferFriend
Messages: 49
Registered: June 2011
Location: Marburg, Germany
Member
Hi Gilbert,

in your Genmodel you need to set "Dynamic Templates" (under "Templates & Merge") to true and specify the "Template Directory", for example "Projectname/templates". Then in your project, create a directory "templates" and a subdirectory "Class" and put your basicSetGenFeature.override.javajetinc in it.
If you generate code now, you should see a project ".JETEmitters" automatically created in your workspace (you may need to remove the default *.resources filter). This way you can see if you have any errors in your template and help you fix it.

Regards,
Tim
Re: How to merge custom code into generated methods? [message #909572 is a reply to message #909555] Fri, 07 September 2012 10:53 Go to previous messageGo to next message
Eclipse UserFriend
Hi Tim,
thanks for your reply. Is it possible to refer to a plugin URI for the
"Template Directory"?

best regards,
Gilbert

Tim Schaefer wrote:
> Hi Gilbert,
>
> in your Genmodel you need to set "Dynamic Templates" (under "Templates &
> Merge") to true and specify the "Template Directory", for example
> "Projectname/templates". Then in your project, create a directory
> "templates" and a subdirectory "Class" and put your
> basicSetGenFeature.override.javajetinc in it.
> If you generate code now, you should see a project ".JETEmitters"
> automatically created in your workspace (you may need to remove the
> default *.resources filter). This way you can see if you have any errors
> in your template and help you fix it.
>
> Regards,
> Tim
Re: How to merge custom code into generated methods? [message #909573 is a reply to message #909572] Fri, 07 September 2012 10:56 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Gilbert,

Yes, it will even look for compiled versions of the templates in that
bundle. I'd suggest you start out testing it in the workspace though.


On 07/09/2012 12:53 PM, Gilbert Mirenque wrote:
> Hi Tim,
> thanks for your reply. Is it possible to refer to a plugin URI for the
> "Template Directory"?
>
> best regards,
> Gilbert
>
> Tim Schaefer wrote:
>> Hi Gilbert,
>>
>> in your Genmodel you need to set "Dynamic Templates" (under "Templates &
>> Merge") to true and specify the "Template Directory", for example
>> "Projectname/templates". Then in your project, create a directory
>> "templates" and a subdirectory "Class" and put your
>> basicSetGenFeature.override.javajetinc in it.
>> If you generate code now, you should see a project ".JETEmitters"
>> automatically created in your workspace (you may need to remove the
>> default *.resources filter). This way you can see if you have any errors
>> in your template and help you fix it.
>>
>> Regards,
>> Tim


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to merge custom code into generated methods? [message #909582 is a reply to message #909573] Fri, 07 September 2012 11:16 Go to previous messageGo to next message
Eclipse UserFriend
Thanks Ed. This are good news. Another question: How do I access the
already defined variables (such as genModel) of the base template (such
as Class.javajet)?

best regards,
Gilbert

Ed Merks wrote:
> Gilbert,
>
> Yes, it will even look for compiled versions of the templates in that
> bundle. I'd suggest you start out testing it in the workspace though.
>
>
> On 07/09/2012 12:53 PM, Gilbert Mirenque wrote:
>> Hi Tim,
>> thanks for your reply. Is it possible to refer to a plugin URI for the
>> "Template Directory"?
>>
>> best regards,
>> Gilbert
>>
>> Tim Schaefer wrote:
>>> Hi Gilbert,
>>>
>>> in your Genmodel you need to set "Dynamic Templates" (under "Templates &
>>> Merge") to true and specify the "Template Directory", for example
>>> "Projectname/templates". Then in your project, create a directory
>>> "templates" and a subdirectory "Class" and put your
>>> basicSetGenFeature.override.javajetinc in it.
>>> If you generate code now, you should see a project ".JETEmitters"
>>> automatically created in your workspace (you may need to remove the
>>> default *.resources filter). This way you can see if you have any errors
>>> in your template and help you fix it.
>>>
>>> Regards,
>>> Tim
>
Re: How to merge custom code into generated methods? [message #909590 is a reply to message #909582] Fri, 07 September 2012 11:28 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Gilbert,

Those snippets are compiled/interpretted in the context of the including
template, so anything in scope at the include point is visible in your
snippet.


On 07/09/2012 1:16 PM, Gilbert Mirenque wrote:
> Thanks Ed. This are good news. Another question: How do I access the
> already defined variables (such as genModel) of the base template (such
> as Class.javajet)?
>
> best regards,
> Gilbert
>
> Ed Merks wrote:
>> Gilbert,
>>
>> Yes, it will even look for compiled versions of the templates in that
>> bundle. I'd suggest you start out testing it in the workspace though.
>>
>>
>> On 07/09/2012 12:53 PM, Gilbert Mirenque wrote:
>>> Hi Tim,
>>> thanks for your reply. Is it possible to refer to a plugin URI for the
>>> "Template Directory"?
>>>
>>> best regards,
>>> Gilbert
>>>
>>> Tim Schaefer wrote:
>>>> Hi Gilbert,
>>>>
>>>> in your Genmodel you need to set "Dynamic Templates" (under "Templates &
>>>> Merge") to true and specify the "Template Directory", for example
>>>> "Projectname/templates". Then in your project, create a directory
>>>> "templates" and a subdirectory "Class" and put your
>>>> basicSetGenFeature.override.javajetinc in it.
>>>> If you generate code now, you should see a project ".JETEmitters"
>>>> automatically created in your workspace (you may need to remove the
>>>> default *.resources filter). This way you can see if you have any errors
>>>> in your template and help you fix it.
>>>>
>>>> Regards,
>>>> Tim


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to merge custom code into generated methods? [message #909614 is a reply to message #909590] Fri, 07 September 2012 12:22 Go to previous message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi, Gilbert,

You might want to have a look at the templates in the
org.eclipse.uml2.codegen.ecore plug-in from the Eclipse UML2 project.
They are a fairly comprehensive, if somewhat overwhelming, example of
what you need to do.

These templates are injected by an extended generator model, which is
different from dynamic templates, but the mechanism of overriding and
inserting custom templates is the same.

HTH,

Christian


On 2012-09-07 11:28:52 +0000, Ed Merks said:

> Gilbert,
>
> Those snippets are compiled/interpretted in the context of the
> including template, so anything in scope at the include point is
> visible in your snippet.
>
>
> On 07/09/2012 1:16 PM, Gilbert Mirenque wrote:
>> Thanks Ed. This are good news. Another question: How do I access the
>> already defined variables (such as genModel) of the base template (such
>> as Class.javajet)?
>>
>> best regards,
>> Gilbert
>>
>> Ed Merks wrote:
>>> Gilbert,
>>>
>>> Yes, it will even look for compiled versions of the templates in that
>>> bundle. I'd suggest you start out testing it in the workspace though.
>>>
>>>
>>> On 07/09/2012 12:53 PM, Gilbert Mirenque wrote:
>>>> Hi Tim,
>>>> thanks for your reply. Is it possible to refer to a plugin URI for the
>>>> "Template Directory"?
>>>>
>>>> best regards,
>>>> Gilbert
>>>>
>>>> Tim Schaefer wrote:
>>>>> Hi Gilbert,
>>>>>
>>>>> in your Genmodel you need to set "Dynamic Templates" (under "Templates &
>>>>> Merge") to true and specify the "Template Directory", for example
>>>>> "Projectname/templates". Then in your project, create a directory
>>>>> "templates" and a subdirectory "Class" and put your
>>>>> basicSetGenFeature.override.javajetinc in it.
>>>>> If you generate code now, you should see a project ".JETEmitters"
>>>>> automatically created in your workspace (you may need to remove the
>>>>> default *.resources filter). This way you can see if you have any errors
>>>>> in your template and help you fix it.
>>>>>
>>>>> Regards,
>>>>> Tim
Previous Topic:[Xcore] How to wrap an enum defined in an interface?
Next Topic:Understanding EFactoryImpl.create(EClass)
Goto Forum:
  


Current Time: Fri Apr 26 00:52:26 GMT 2024

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

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

Back to the top