/** * * {@inheritDoc} * * @see org.eclipse.emf.emfstore.internal.common.model.IdEObjectCollection#deleteModelElement(org.eclipse.emf.ecore.EObject) */ public void deleteModelElement(final EObject modelElement) { if (!this.contains(modelElement)) { throw new IllegalArgumentException("Cannot delete a model element that is not contained in this project."); } // remove cross references ModelUtil.deleteOutgoingCrossReferences(this, modelElement); final Collection<Setting> crossReferences = UsageCrossReferencer.find(modelElement, this); ModelUtil.deleteIncomingCrossReferencesFromParent(crossReferences, modelElement); // remove containment final EObject containerModelElement = ModelUtil.getContainerModelElement(modelElement); if (containerModelElement == null) { // removeModelElementAndChildrenFromCache(modelElement); // getEobjectsIdMap().remove(modelElement); getModelElements().remove(modelElement); } else { final EReference containmentFeature = modelElement.eContainmentFeature(); if (containmentFeature.isMany()) { final EList<?> containmentList = (EList<?>) containerModelElement.eGet(containmentFeature); containmentList.remove(modelElement); } else { containerModelElement.eSet(containmentFeature, null); } removeModelElementAndChildrenFromResource(modelElement); } }
public void replaceBy(EObject oldObject, EObject newObject) { EObject eObject = oldObject; // Copied from EcoreUtil#delete EObject rootEObject = EcoreUtil.getRootContainer(eObject); Resource resource = rootEObject.eResource(); Collection<EStructuralFeature.Setting> usages; if (resource == null) { usages = UsageCrossReferencer.find(eObject, rootEObject); } else { ResourceSet resourceSet = resource.getResourceSet(); if (resourceSet == null) { usages = UsageCrossReferencer.find(eObject, resource); } else { usages = UsageCrossReferencer.find(eObject, resourceSet); } } for (EStructuralFeature.Setting setting : usages) { if (setting.getEStructuralFeature().isChangeable()) { EObject holder = setting.getEObject(); EStructuralFeature f = setting.getEStructuralFeature(); if ( f.isMany() ) { EList<EObject> list = (EList<EObject>) holder.eGet(f); list.set(list.indexOf(oldObject), newObject); } else { holder.eSet(f, newObject); } } } EcoreUtil.replace(oldObject, newObject); allCreatedObjectsIntoResource.remove(oldObject); }
/** * * Recursively count all "external" references. * * @param rootDesc Descriptor in question. * @param desc Descriptor used for recursion. It must be equal to @rootDesc. */ private static synchronized boolean isDescriptorReferencedElsewhere(Descriptor rootDesc, Descriptor desc){ //remove descriptor only if it is not referenced elsewhere //TODO: check if it works as expected EObject root = EcoreUtil.getRootContainer(desc); int descriptroRefCounter = 0; for(Object object : UsageCrossReferencer.find(desc, root)){ //check if reference is marked for release boolean isInsideRoot = false; if(object instanceof EObject){ EObject ref = (EObject)object; while(ref != null){ if(ref == rootDesc){ isInsideRoot = true; break; } ref = ref.eContainer(); } } if(!isInsideRoot){ descriptroRefCounter++; } } if(descriptroRefCounter > 1){ return true; } for(EObject child : desc.eContents()){ if(child instanceof Descriptor){ if(isDescriptorReferencedElsewhere(rootDesc, (Descriptor)child)){ return true; } } } return false; }
/** * Returns the {@link ECrossReferenceAdapter}, if available. * * @param modelElement * the model element for which to find inverse cross references * * @return the {@link ECrossReferenceAdapter} */ public Collection<Setting> findInverseCrossReferences(EObject modelElement) { if (crossReferenceAdapter != null) { return crossReferenceAdapter.getInverseReferences(modelElement); } return UsageCrossReferencer.find(modelElement, resourceSet); }