/** * Converts this Object to a dynamic object with a new type that is a subtype of the current type, * preserving any existing property values. * * @param newEClass the new type for this Object * @throws IllegalArgumentException if newEClass is not a subtype of this Object's current type * @see org.eclipse.emf.ecore.InternalEObject#eSetClass(org.eclipse.emf.ecore.EClass) */ public void narrow(EClass newEClass) throws IllegalArgumentException { if (!eClass().equals(EcorePackage.eINSTANCE.getEObject()) && !eClass().isSuperTypeOf(newEClass)) { throw new IllegalArgumentException( "The class " + newEClass.getName() + " is not a subtype of the Object's current type " + eClass().getName()); } EPropertiesHolder props = eProperties(); if (props.getEClass() != null && props.hasSettings()) { int staticSize = eStaticFeatureCount(); int oldSize = eClass().getEAllStructuralFeatures().size() - staticSize; if (oldSize > 0) { int newSize = newEClass.getEAllStructuralFeatures().size() - staticSize; if (newSize > oldSize) { List oldValues = new ArrayList(oldSize); for (int i = 0; i < oldSize; i++) { oldValues.add(props.dynamicGet(i)); } props.allocateSettings(newSize); for (int i = 0; i < oldSize; i++) { props.dynamicSet(i, oldValues.get(i)); } } } } eSetClass(newEClass); }
public EObject getStaticInstance() { if (null == staticObj) { EClass eClass = eClass(); if (eClass.isAbstract()) { throw new IllegalArgumentException( "Cannot create an instance of the abstract type: " + eClass.getName()); } staticObj = eClass.getEPackage().getEFactoryInstance().create(eClass); // System.out.println("New static instance: " + staticObj.hashCode() + " of type " // + eClass.getName()); // Copy over all property values EPropertiesHolder props = eProperties(); if (props.getEClass() != null && props.hasSettings()) { List features = eClass.getEAllStructuralFeatures(); for (Iterator itr = features.iterator(); itr.hasNext(); ) { EStructuralFeature feature = (EStructuralFeature) itr.next(); staticObj.eSet(feature, eGet(feature)); // System.out.println(" F: " + feature.getName()); } } // Update all external references for (Iterator itr = actions.iterator(); itr.hasNext(); ) { Action action = (Action) itr.next(); action.doIt(staticObj); } // Update containing resource // List contents = eResource().getContents(); // contents.remove(this); // contents.add(staticObj); } return staticObj; }