id = StateManagerId.newInstance(this);
StateManagerImpl sm = newStateManagerImpl(id, meta);
sm.setOwner((StateManagerImpl) owner, ownerMeta);
PersistenceCapable copy;
PCState state;
Class<?> type = meta.getDescribedType();
if (obj != null) {
// give copy and the original instance the same state manager
// so that we can copy fields from one to the other
StateManagerImpl copySM;
PersistenceCapable pc;
if (orig == null) {
copySM = sm;
pc = assertPersistenceCapable(obj);
pc.pcReplaceStateManager(sm);
} else {
copySM = orig;
pc = orig.getPersistenceCapable();
}
try {
// copy the instance. we do this even if it doesn't already
// have a state manager in case it is later assigned to a
// PC field; at that point it's too late to copy
copy = PCRegistry.newInstance(type, copySM, false);
int[] fields = new int[meta.getFields().length];
for (int i = 0; i < fields.length; i++)
fields[i] = i;
copy.pcCopyFields(pc, fields);
state = PCState.ECOPY;
copy.pcReplaceStateManager(null);
} finally {
// if the instance didn't have a state manager to start,
// revert it to being transient
if (orig == null)
pc.pcReplaceStateManager(null);
}
} else {
copy = PCRegistry.newInstance(type, sm, false);
if ((_flags & FLAG_ACTIVE) != 0 && !_optimistic)
state = PCState.ECLEAN;