Skip to main content



      Home
Home » Modeling » EMF » Dynamic EMF
Dynamic EMF [message #400487] Fri, 21 April 2006 06:24 Go to next message
Eclipse UserFriend
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 07:40 Go to previous messageGo to next message
Eclipse UserFriend
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 #400492 is a reply to message #400491] Fri, 21 April 2006 08:12 Go to previous messageGo to next message
Eclipse UserFriend
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 08:37 Go to previous messageGo to next message
Eclipse UserFriend
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)
Re: Dynamic EMF [message #1692277 is a reply to message #400493] Tue, 14 April 2015 12:59 Go to previous messageGo to next message
Eclipse UserFriend
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 00:19 Go to previous message
Eclipse UserFriend
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.
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: Fri Jul 04 22:43:05 EDT 2025

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

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

Back to the top