EMF对象的deepCopy

因为EcoreUtil.copy()发现是shadow copy,后来解决了。在某开源代码中找到,实现方法如下:

public static <T extends Core> T deepCopy(T core) {
		Stack<EObject> work = new Stack<EObject>();
		work.push(core);
		Collection<EObject> collection = new LinkedHashSet<EObject>();
		while (!work.isEmpty()) {
			EObject o = work.pop();
			if (collection.contains(o)) {
				continue;
			}
			collection.add(o);
			List<EObject> list = o.eContents();
			for (EObject eo : list) {
				if (!collection.contains(eo)) {
					work.push(eo);
				}
			}
			list = o.eCrossReferences();
			for (EObject eo : list) {
				if (!collection.contains(eo)) {
					work.push(eo);
				}
			}
		}
		EcoreUtil.Copier copier = new EcoreUtil.Copier();
		collection = copier.copyAll(collection);
		copier.copyReferences();
		T copy = (T) collection.iterator().next();

		// The name is not part of the known EMF attribute set.
		copy.setName(core.getName());

		return copy;
	}
 

你可能感兴趣的:(copy)