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
I found this bit of code on SO on how to create an EObject programatically:

EClass eClass = EcoreFactory.eINSTANCE.createEClass();
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?

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: 27
Registered: July 2009
Junior Member

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();

then use the .getEClassifiers().add()
EClass eClass = EcoreFactory.eINSTANCE.createEClass();
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,

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 ;-)
Previous Topic:Edapt sych on Ecore editor
Next Topic:[EMF Forms] Editing optional domain elements
Goto Forum:

Current Time: Wed Oct 27 10:13:35 GMT 2021

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

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

Back to the top