Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » Dynamic creation of EObject(I am missing something wrt EPackages)
Dynamic creation of EObject [message #1773030] Wed, 20 September 2017 12:25 Go to next message
Geoffry Roberts is currently offline Geoffry RobertsFriend
Messages: 52
Registered: July 2016
Member
I found this bit of code on SO on how to create an EObject programatically:

EClass eClass = EcoreFactory.eINSTANCE.createEClass();
eClass.setName("Interaction1");
eClass.setInterface(true);
EObject obj = EcoreUtil.create(eClass);



When run it throws an NPE because EClass dosen't have a package. From EcoreUtil:

  public static EObject create(EClass eClass)
  {
    return eClass.getEPackage().getEFactoryInstance().create(eClass);
  }


There is in EClass getEPackage() but no setter.

How does the package get set?

btw,
The class I am trying to instantiate is generated and on the classpath.
Re: Dynamic creation of EObject [message #1773035 is a reply to message #1773030] Wed, 20 September 2017 14:27 Go to previous message
Didier Vojtisek is currently offline Didier VojtisekFriend
Messages: 23
Registered: July 2009
Junior Member
Hi


about the way to set the EPackage this is because you must set the reference the other way round: create the EPackage
EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
pack.setName("something'); 
pack.setNsURI("something');

then use the .getEClassifiers().add()
EClass eClass = EcoreFactory.eINSTANCE.createEClass();
eClass.setName("Interaction1");
eClass.setInterface(true);
pack.getEClassifiers().add(eClass);
EObject obj = EcoreUtil.create(eClass);



However, except if this is on purpose (for example if you want to create both the metamodel and model completely dynamically), I think you should not try to recreate you metamodel in order to instantiate model elements.

You should probably consider retrieving the package in the registry,
EPackage.Registry.INSTANCE.getEPackage(YourMetamodelPackage.eNS_URI)

then find you class in the package (navigate in the Eclassifiers and search the one with the expectec name) and instanciate it.

a look to the ecore diagram may help ;-) http://download.eclipse.org/modeling/emf/emf/javadoc/2.9.0/org/eclipse/emf/ecore/doc-files/EcoreRelations.gif
Previous Topic:Edapt sych on Ecore editor
Next Topic:[EMF Forms] Editing optional domain elements
Goto Forum:
  


Current Time: Fri Sep 21 05:37:22 GMT 2018

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

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

Back to the top