Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Dynamic EMF
Dynamic EMF [message #400487] Fri, 21 April 2006 10:24 Go to next message
Jens Bartelheimer is currently offline Jens BartelheimerFriend
Messages: 44
Registered: July 2009
Member
Hello,

I am just playing around with Dynamic EMF. I try to add an attribute to
the UML2 message class.

The Syso statement returns the added attribute, but calling eSet(...)
raises an exception. Why?

EAttribute attr = EcoreFactory.eINSTANCE.createEAttribute();
attr.setName("testAttribute");
attr.setEType(EcorePackage.eINSTANCE.getEString());

EClass messageClass = UML2Package.eINSTANCE.getMessage();
messageClass.getEStructuralFeatures().add(attr);

Message m = UML2Factory.eINSTANCE.createMessage();
System.out.println(m.eClass().getEStructuralFeature("testAttribute "));
m.eSet(attr,"foo");


Exception in thread "main" java.lang.NullPointerException
at
org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDerivedStructur alFeatureID(BasicEObjectImpl.java:780)
at org.eclipse.uml2.impl.MessageImpl.eGet(MessageImpl.java:614)
at
org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:483)
at
org.eclipse.emf.ecore.impl.BasicEObjectImpl.eOpenSet(BasicEO bjectImpl.java:549)
at
org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDynamicSet(Basi cEObjectImpl.java:532)
at org.eclipse.uml2.impl.MessageImpl.eSet(MessageImpl.java:720)
Re: Dynamic EMF [message #400491 is a reply to message #400487] Fri, 21 April 2006 11:40 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Jens,

Ouch. You can't directly modify a generated class. You can extend any
class with a new class, add attributes to that new class, and then
create instances of that. (And once you've created an instance of a
class, you can no longer modify that class either.)


Jens wrote:
> Hello,
>
> I am just playing around with Dynamic EMF. I try to add an attribute
> to the UML2 message class.
>
> The Syso statement returns the added attribute, but calling eSet(...)
> raises an exception. Why?
>
> EAttribute attr = EcoreFactory.eINSTANCE.createEAttribute();
> attr.setName("testAttribute");
> attr.setEType(EcorePackage.eINSTANCE.getEString());
>
> EClass messageClass = UML2Package.eINSTANCE.getMessage();
> messageClass.getEStructuralFeatures().add(attr);
>
> Message m = UML2Factory.eINSTANCE.createMessage();
>
> System.out.println(m.eClass().getEStructuralFeature("testAttribute "));
> m.eSet(attr,"foo");
>
>
> Exception in thread "main" java.lang.NullPointerException
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDerivedStructur alFeatureID(BasicEObjectImpl.java:780)
>
> at org.eclipse.uml2.impl.MessageImpl.eGet(MessageImpl.java:614)
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:483)
>
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eOpenSet(BasicEO bjectImpl.java:549)
>
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDynamicSet(Basi cEObjectImpl.java:532)
>
> at org.eclipse.uml2.impl.MessageImpl.eSet(MessageImpl.java:720)


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Dynamic EMF [message #400492 is a reply to message #400491] Fri, 21 April 2006 12:12 Go to previous messageGo to next message
Jens Bartelheimer is currently offline Jens BartelheimerFriend
Messages: 44
Registered: July 2009
Member
ok, but then I am wondering why
m.eClass().getEStructuralFeature("testAttribute") returns the attribute
of my extension.

Jens

Ed Merks wrote:
> Jens,
>
> Ouch. You can't directly modify a generated class. You can extend any
> class with a new class, add attributes to that new class, and then
> create instances of that. (And once you've created an instance of a
> class, you can no longer modify that class either.)
>
>
> Jens wrote:
>> Hello,
>>
>> I am just playing around with Dynamic EMF. I try to add an attribute
>> to the UML2 message class.
>>
>> The Syso statement returns the added attribute, but calling eSet(...)
>> raises an exception. Why?
>>
>> EAttribute attr = EcoreFactory.eINSTANCE.createEAttribute();
>> attr.setName("testAttribute");
>> attr.setEType(EcorePackage.eINSTANCE.getEString());
>> EClass messageClass = UML2Package.eINSTANCE.getMessage();
>> messageClass.getEStructuralFeatures().add(attr);
>> Message m = UML2Factory.eINSTANCE.createMessage();
>>
>> System.out.println(m.eClass().getEStructuralFeature("testAttribute "));
>> m.eSet(attr,"foo");
>>
>>
>> Exception in thread "main" java.lang.NullPointerException
>> at
>> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDerivedStructur alFeatureID(BasicEObjectImpl.java:780)
>>
>> at org.eclipse.uml2.impl.MessageImpl.eGet(MessageImpl.java:614)
>> at
>> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:483)
>>
>> at
>> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eOpenSet(BasicEO bjectImpl.java:549)
>>
>> at
>> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDynamicSet(Basi cEObjectImpl.java:532)
>>
>> at org.eclipse.uml2.impl.MessageImpl.eSet(MessageImpl.java:720)
Re: Dynamic EMF [message #400493 is a reply to message #400492] Fri, 21 April 2006 12:37 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Jens,

I guess you interpreted my "you can't do that" answer as "it's
impossible to do that" when what I meant is "it's a really bad thing
that you should never do because you've just corrupted the one shared
instance of the meta data for the UML2 model that the whole JVM is using
and it won't really do all the things you are expecting."


Jens wrote:
> ok, but then I am wondering why
> m.eClass().getEStructuralFeature("testAttribute") returns the
> attribute of my extension.
>
> Jens
>
> Ed Merks wrote:
>> Jens,
>>
>> Ouch. You can't directly modify a generated class. You can extend
>> any class with a new class, add attributes to that new class, and
>> then create instances of that. (And once you've created an instance
>> of a class, you can no longer modify that class either.)
>>
>>
>> Jens wrote:
>>> Hello,
>>>
>>> I am just playing around with Dynamic EMF. I try to add an attribute
>>> to the UML2 message class.
>>>
>>> The Syso statement returns the added attribute, but calling
>>> eSet(...) raises an exception. Why?
>>>
>>> EAttribute attr = EcoreFactory.eINSTANCE.createEAttribute();
>>> attr.setName("testAttribute");
>>> attr.setEType(EcorePackage.eINSTANCE.getEString());
>>> EClass messageClass = UML2Package.eINSTANCE.getMessage();
>>> messageClass.getEStructuralFeatures().add(attr);
>>> Message m = UML2Factory.eINSTANCE.createMessage();
>>>
>>> System.out.println(m.eClass().getEStructuralFeature("testAttribute "));
>>> m.eSet(attr,"foo");
>>>
>>>
>>> Exception in thread "main" java.lang.NullPointerException
>>> at
>>> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDerivedStructur alFeatureID(BasicEObjectImpl.java:780)
>>>
>>> at org.eclipse.uml2.impl.MessageImpl.eGet(MessageImpl.java:614)
>>> at
>>> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:483)
>>>
>>> at
>>> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eOpenSet(BasicEO bjectImpl.java:549)
>>>
>>> at
>>> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDynamicSet(Basi cEObjectImpl.java:532)
>>>
>>> at org.eclipse.uml2.impl.MessageImpl.eSet(MessageImpl.java:720)


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Dynamic EMF [message #1692277 is a reply to message #400493] Tue, 14 April 2015 16:59 Go to previous messageGo to next message
Edouard R. Batot is currently offline Edouard R. BatotFriend
Messages: 100
Registered: March 2015
Senior Member
Hi Ed,

what if we want to do it anyway ?

Suppose I'm not playing with the UML2 blahblah but with a personnal Metamodel I want to evolve during an execution....

How to get rid of that null pointer ?
Re: Dynamic EMF [message #1692309 is a reply to message #1692277] Wed, 15 April 2015 04:19 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Edoaurd,

Comments below.

On 14/04/2015 6:59 PM, Edoaurd Batot wrote:
> Hi Ed,
>
> what if we want to do it anyway ?
It doesn't work.
>
> Suppose I'm not playing with the UML2 blahblah but with a personnal
> Metamodel I want to evolve during an execution....
Use a dynamic model all the time.
> How to get rid of that null pointer ?
>
Java supports things like hot replace when debugging, but even there,
you can't add a field to a Java class and still hot replace it. You can
want to do it, but that desire itself doesn't make it possible in practice.


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:The feature is not a valid feature. (java.lang.IllegalArgumentException)
Next Topic:Importing EMF models into Java through code
Goto Forum:
  


Current Time: Sat Apr 20 03:24:16 GMT 2024

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

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

Back to the top