Persist EMF adapter with EObject

Viewed 38

Question: how to persist adapters in EMF?

Context: EMF adapters can be used to add ad-hoc data to an eObject without changing the model, e.g. if MyAdapter extends org.eclipse.emf.common.notify.impl.AdapterImpl and it holds a single String, than I can do

EObject original = EcorePackage.eINSTANCE.getEcoreFactory().createEObject();
original.eAdapters().add(new MyAdapter("hello"));

to basically add a String label to original. When I persist original, however, the adapter is lost:

ByteArrayOutputStream ostream = new ByteArrayOutputStream();
EObjectPersister.save(ostream, List.of(original));
byte[] vault = ostream.toByteArray();
ByteArrayInputStream istream = new ByteArrayInputStream(vault);
EObject recall = EObjectPersister.load(istream).get(0);
assertEquals(1, recall.eAdapters().size()); //fails!

What configurations should be performed so that adapters are persisted as well?

The save/load methods are defined as follows:

public static void save(OutputStream outputStream, List<EObject> eObjects) {
   Resource resource = createResource();
   eObjects.forEach(resource.getContents()::add);
   resource.save(outputStream, options);
}
public static EList<EObject> load(InputStream inputStream) {
   Resource resource = createResource();
   resource.load(inputStream, options);
   return resource.getContents();
}       
private static Resource createResource() {
   ResourceSet resourceSet = new ResourceSetImpl();
   Resource.Factory resourceFactory = __ -> new BinaryResourceImpl();
   resourceSet.getResourceFactoryRegistry().getContentTypeToFactoryMap().put("*", resourceFactory);
   return resourceSet.createResource(null);
}
0 Answers
Related