[161647] Ensuring all related stereotype applications are destroyed when a stereotype is unapplied or a stereotype application is destroyed.
diff --git a/plugins/org.eclipse.uml2.common/src/org/eclipse/uml2/common/util/UML2Util.java b/plugins/org.eclipse.uml2.common/src/org/eclipse/uml2/common/util/UML2Util.java
index b093e55..fb628e7 100644
--- a/plugins/org.eclipse.uml2.common/src/org/eclipse/uml2/common/util/UML2Util.java
+++ b/plugins/org.eclipse.uml2.common/src/org/eclipse/uml2/common/util/UML2Util.java
@@ -8,7 +8,7 @@
  * Contributors:
  *   IBM - initial API and implementation
  *
- * $Id: UML2Util.java,v 1.23.2.1 2006/10/18 18:43:54 khussey Exp $
+ * $Id: UML2Util.java,v 1.23.2.2 2006/10/25 16:52:17 khussey Exp $
  */
 package org.eclipse.uml2.common.util;
 
@@ -1091,60 +1091,78 @@
 			: crossReferenceAdapter.getInverseReferences(eObject);
 	}
 
-	protected static void destroy(EObject eObject) {
+	protected static void removeReferences(EObject eObject,
+			EObject ancestorEObject) {
+		List nonNavigableInverseReferences = new ArrayList(
+			getNonNavigableInverseReferences(eObject));
 
-		for (Iterator allContents = getAllContents(eObject, true, false); allContents
+		for (Iterator nnir = nonNavigableInverseReferences.iterator(); nnir
 			.hasNext();) {
 
-			EObject containedEObject = (EObject) allContents.next();
-			List nonNavigableInverseReferences = new ArrayList(
-				getNonNavigableInverseReferences(containedEObject));
+			EStructuralFeature.Setting setting = (EStructuralFeature.Setting) nnir
+				.next();
 
-			for (Iterator nnir = nonNavigableInverseReferences.iterator(); nnir
-				.hasNext();) {
+			if (setting.getEStructuralFeature().isChangeable()
+				&& (ancestorEObject == null || !EcoreUtil.isAncestor(
+					ancestorEObject, setting.getEObject()))) {
 
-				EStructuralFeature.Setting setting = (EStructuralFeature.Setting) nnir
-					.next();
-
-				if (setting.getEStructuralFeature().isChangeable()
-					&& !EcoreUtil.isAncestor(eObject, setting.getEObject())) {
-
-					EcoreUtil.remove(setting, containedEObject);
-				}
+				EcoreUtil.remove(setting, eObject);
 			}
+		}
 
-			for (Iterator eAllReferences = containedEObject.eClass()
-				.getEAllReferences().iterator(); eAllReferences.hasNext();) {
+		for (Iterator eAllReferences = eObject.eClass().getEAllReferences()
+			.iterator(); eAllReferences.hasNext();) {
 
-				EReference eReference = (EReference) eAllReferences.next();
+			EReference eReference = (EReference) eAllReferences.next();
 
-				if (eReference.isChangeable() && !eReference.isContainer()
-					&& !eReference.isContainment()
-					&& containedEObject.eIsSet(eReference)) {
+			if (eReference.isChangeable() && !eReference.isContainer()
+				&& !eReference.isContainment() && eObject.eIsSet(eReference)) {
 
-					if (eReference.isMany()) {
+				if (eReference.isMany()) {
 
-						for (Iterator values = ((List) containedEObject
-							.eGet(eReference)).iterator(); values.hasNext();) {
+					for (Iterator values = ((List) eObject.eGet(eReference))
+						.iterator(); values.hasNext();) {
 
-							if (!EcoreUtil.isAncestor(eObject, (EObject) values
-								.next())) {
+						Object value = values.next();
 
-								values.remove();
-							}
+						if (ancestorEObject == null
+							|| !EcoreUtil.isAncestor(ancestorEObject,
+								(EObject) value)) {
+
+							values.remove();
 						}
-					} else {
+					}
+				} else {
 
-						if (!EcoreUtil.isAncestor(eObject,
-							(EObject) containedEObject.eGet(eReference))) {
+					if (ancestorEObject == null
+						|| !EcoreUtil.isAncestor(ancestorEObject,
+							(EObject) eObject.eGet(eReference))) {
 
-							containedEObject.eUnset(eReference);
-						}
+						eObject.eUnset(eReference);
 					}
 				}
 			}
+		}
+	}
 
-			containedEObject.eAdapters().clear();
+	protected static void destroy(EObject eObject) {
+
+		if (eObject.eContents().isEmpty()) {
+			removeReferences(eObject, eObject);
+			eObject.eAdapters().clear();
+		} else {
+
+			for (Iterator allContents = getAllContents(eObject, true, false); allContents
+				.hasNext();) {
+
+				removeReferences((EObject) allContents.next(), eObject);
+			}
+
+			for (Iterator allContents = getAllContents(eObject, true, false); allContents
+				.hasNext();) {
+
+				((EObject) allContents.next()).eAdapters().clear();
+			}
 		}
 
 		EcoreUtil.remove(eObject);
diff --git a/plugins/org.eclipse.uml2.uml/src/org/eclipse/uml2/uml/internal/operations/ElementOperations.java b/plugins/org.eclipse.uml2.uml/src/org/eclipse/uml2/uml/internal/operations/ElementOperations.java
index f305cc2..560cce8 100644
--- a/plugins/org.eclipse.uml2.uml/src/org/eclipse/uml2/uml/internal/operations/ElementOperations.java
+++ b/plugins/org.eclipse.uml2.uml/src/org/eclipse/uml2/uml/internal/operations/ElementOperations.java
@@ -8,7 +8,7 @@
  * Contributors:
  *   IBM - initial API and implementation
  *
- * $Id: ElementOperations.java,v 1.44.2.6 2006/10/20 21:06:49 khussey Exp $
+ * $Id: ElementOperations.java,v 1.44.2.7 2006/10/25 16:52:18 khussey Exp $
  */
 package org.eclipse.uml2.uml.internal.operations;
 
@@ -44,7 +44,6 @@
 
 import org.eclipse.emf.ecore.util.EcoreUtil;
 
-import org.eclipse.uml2.common.util.UML2Util;
 import org.eclipse.uml2.uml.Association;
 import org.eclipse.uml2.uml.DirectedRelationship;
 import org.eclipse.uml2.uml.Element;
@@ -1471,19 +1470,54 @@
 		destroy((EObject) element);
 	}
 
-	protected static void destroy(EObject eObject) {
+	protected static void destroy(EObject ancestorEObject) {
 
-		for (Iterator allContents = getAllContents(eObject, true, false); allContents
-			.hasNext();) {
+		if (ancestorEObject.eContents().isEmpty()) {
 
-			Object object = allContents.next();
+			if (ancestorEObject instanceof Element) {
+				destroyAll(((Element) ancestorEObject)
+					.getStereotypeApplications());
+				removeReferences(ancestorEObject, ancestorEObject);
+				ancestorEObject.eAdapters().clear();
+			} else {
+				removeReferences(ancestorEObject, null);
+			}
+		} else {
 
-			if (object instanceof Element) {
-				destroyAll(((Element) object).getStereotypeApplications());
+			for (Iterator allContents = getAllContents(ancestorEObject, true,
+				false); allContents.hasNext();) {
+
+				Object object = allContents.next();
+
+				if (object instanceof Element) {
+					destroyAll(((Element) object).getStereotypeApplications());
+				}
+			}
+
+			for (Iterator allContents = getAllContents(ancestorEObject, true,
+				false); allContents.hasNext();) {
+
+				EObject eObject = (EObject) allContents.next();
+
+				if (eObject instanceof Element) {
+					removeReferences(eObject, ancestorEObject);
+				} else {
+					removeReferences(eObject, null);
+				}
+			}
+
+			for (Iterator allContents = getAllContents(ancestorEObject, true,
+				false); allContents.hasNext();) {
+
+				Object object = allContents.next();
+
+				if (object instanceof Element) {
+					((Element) object).eAdapters().clear();
+				}
 			}
 		}
 
-		UML2Util.destroy(eObject);
+		EcoreUtil.remove(ancestorEObject);
 	}
 
 	protected static void destroyAll(Collection eObjects) {