/** constructor does not copy GroupRepresentative */ public UnitGroup(UnitGroup g) { this.id = g.id; this.ownerId = g.ownerId; this.type = g.type; try { // clone Units for (Unit u : g.units) { units.add((Unit) u.clone()); } // clone unit requirements for (Argument a : g.unitTypeReqs) { unitTypeReqs.add((Argument) a.clone()); } } catch (CloneNotSupportedException ex) { throw new RuntimeException(ex); } }
/** * Clone the object into the specified workspace. The new object is <i>not</i> added to the * directory of that workspace (you must do this yourself if you want it there). The result is a * new entity with clones of the ports of the original entity. The ports are set to the ports of * the new entity. * * @param workspace The workspace for the cloned object. * @exception CloneNotSupportedException If cloned ports cannot have as their container the cloned * entity (this should not occur), or if one of the attributes cannot be cloned. * @return The new Entity. */ public Object clone(Workspace workspace) throws CloneNotSupportedException { GenericJNIActor newEntity = (GenericJNIActor) super.clone(workspace); newEntity._argumentsList = new NamedList(newEntity); // Clone the ports. Iterator arguments = argumentsList().iterator(); while (arguments.hasNext()) { Argument argument = (Argument) arguments.next(); Argument newArgument = (Argument) argument.clone(workspace); // Assume that since we are dealing with clones, // exceptions won't occur normally (the original was successfully // incorporated, so this one should be too). If they do, throw an // InvalidStateException. try { newArgument.setContainer(newEntity); } catch (KernelException ex) { workspace.remove(newEntity); throw new InvalidStateException(this, "Failed to clone an Entity: " + ex.getMessage()); } } Class myClass = getClass(); Field[] fields = myClass.getFields(); for (int i = 0; i < fields.length; i++) { try { if (fields[i].get(newEntity) instanceof Argument) { fields[i].set(newEntity, newEntity.getArgument(fields[i].getName())); } } catch (IllegalAccessException e) { throw new CloneNotSupportedException(e.getMessage() + ": " + fields[i].getName()); } } return newEntity; }