Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to change Code generation Template
How to change Code generation Template [message #1453633] Mon, 27 October 2014 08:33 Go to next message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 450
Registered: July 2009
Senior Member
Hello,

a quick question regarding the code that EMF generates.

where/how do i change the code generation so it produces other output.
as exampe currently EMF generates a setter like this:
	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public void setId(Integer newId) {
		Integer oldId = id;
		id = newId;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, BioofficePackage.CONTACT__ID, oldId, id));
	}


but i'd like to generate a setter like this:
	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public void setId(Integer newId) {
		Integer oldId = id;
		id = newId;
		if (eNotificationRequired(oldId, newId))
			eNotify(new ENotificationImpl(this, Notification.SET, BioofficePackage.CONTACT__ID, oldId, id));
	}

this would be only a modification of a template (i guess) by hand this tages ages for a whole model

thanks in advance

[Updated on: Mon, 27 October 2014 08:37]

Report message to a moderator

Re: How to change Code generation Template [message #1453669 is a reply to message #1453633] Mon, 27 October 2014 09:44 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 28968
Registered: July 2009
Senior Member
Ludwig,

Comments below.


On 27/10/2014 9:33 AM, Ludwig Moser wrote:
> Hello,
>
> a quick question regarding the code that EMF generates.
>
> where/how do i change the code generation so it produces other output.
Dynamic templates can be used to modify pretty much anything. Many
templates have insertion hooks, but some types of change really involve
copying the whole template and modifying its parts...
> as exampe currently EMF generates a setter like this:
> /**
> * <!-- begin-user-doc --> <!-- end-user-doc -->
> * @generated
> */
> public void setId(Integer newId) {
> Integer oldId = id;
> id = newId;
> if (eNotificationRequired())
> eNotify(new ENotificationImpl(this, Notification.SET,
> BioofficePackage.CONTACT__ID, oldId, id));
> }
>
> but i'd like to generate a setter like this:
>
> /**
> * <!-- begin-user-doc --> <!-- end-user-doc -->
> * @generated
> */
> public void setId(Integer newId) {
> Integer oldId = id;
> id = newId;
> if (eNotificationRequired(oldId, newId))
> eNotify(new ENotificationImpl(this, Notification.SET,
> BioofficePackage.CONTACT__ID, oldId, id));
> }
> this would be only a modification of a template (i guess) by hand this
> tages ages for a whole model
I wonder if this is really the best way to achieve this. Are you
looking to do this for all features, or just this specific feature?
Another approach to consider is to override eNotificationRequired to
always return true, and to modify eNotify to intercept all notifications
before calling super.eNotify. I.e., essentially instrument the
implementation so that notifications are always produced and use that to
"self monitor" all notifications. This has the advantage that no
generated code is changed and it works for multi-valued features as well.
>
> thanks in advance
Re: How to change Code generation Template [message #1453684 is a reply to message #1453669] Mon, 27 October 2014 10:11 Go to previous message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 450
Registered: July 2009
Senior Member
my aim is to modify ALL setters for single valued entries (not lists/sets nor maps)
because i noticed the following.
imagine an EClass with an EString (or EIntegerObject)
the value is '123' if you set the value to the new value '123' i still get an notification whereat i do not need one, because the actual value does not change at all.
(Eg a input field gets its data set to 123, user types 123 again, notification gets raised... none needed...)

the method i wrote "eNotificationRequired(oldId, newId)"
does check if a notification is required in the manner of - did the actual value change - AND "eNotificationRequired()" is true

	public boolean eNotificationRequired(Object oldValue, Object newValue) {
		return eNotificationRequired() && !equals(oldValue, newValue);
	}
	
	private boolean equals(Object oldValue, Object newValue) {
		if (oldValue == null) {
			if (newValue == null) {
				return true;
			}
			return false;
		} else {
			if (newValue == null) {
				return true;
			}
		}
		return oldValue.equals(newValue);
	}


this way you prevent dozens of notifications to be processed where they are not needed at all (possible performance gain)

my model has 50 Classes with about average of 25(?) attributes/references
updating all the setters would be a (long &) boring task

what do you think about this 'idea' of saving notifications?
shall i do it with dynamic templates or just process the not needed notifications?
Previous Topic:[EMF + CDO] Strategy for restricting duplicate EAttributes values
Next Topic:[CDO] Security Manager and Package Filter
Goto Forum:
  


Current Time: Tue Nov 21 01:07:02 GMT 2017

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

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