/** * Copies all the properties from one object to anoter. * * @param source The object to copy from. * @param target The object to copy to. */ public static void copy(EObject source, EObject target) { for (Iterator i = source.eClass().getEStructuralFeatures().iterator(); i.hasNext(); ) { EStructuralFeature feature = (EStructuralFeature) i.next(); target.eSet(feature, source.eGet(feature)); } } }
protected void log(Object obj, int level, StringBuffer log) { EObject object = (EObject) obj; List properties = object.eClass().getEAllStructuralFeatures(); for (Iterator p = properties.iterator(); p.hasNext(); ) { EStructuralFeature property = (EStructuralFeature) p.next(); Object value = object.eGet(property); // skip empty properties if (value == null || (value instanceof Collection && ((Collection) value).isEmpty()) || (value instanceof Map && ((Map) value).isEmpty())) { continue; } log.append("\n"); for (int i = 0; i < level; i++) log.append(" "); if (value instanceof EObject && (level < 3)) { log.append(property.getName()); log.append(":"); log((EObject) value, level + 1, log); } else if (value instanceof Collection) { log(property.getName(), (Collection) value, level + 1, log); } else { log.append(property.getName()); log.append(" = " + value); } } }
static void set(EObject object, String featureName, Object value) { EStructuralFeature feature = object.eClass().getEStructuralFeature(featureName); if (feature != null) { object.eSet(feature, value); } }
@Override public void addTo(EObject container, EStructuralFeature feature, Object element, int index) throws ModelManipulationException { EClass containerClass = container.eClass(); Preconditions.checkArgument(containerClass.getEAllStructuralFeatures().contains(feature), UNDEFINED_ESTRUCTURAL_FEATURE_FOR_CONTAINER_MESSAGE, containerClass.getName(), feature.getName()); Preconditions.checkArgument(feature.isMany(), "The EStructuralFeature %s must have an upper bound larger than 1.", feature.getName()); Preconditions.checkArgument(!(feature instanceof EReference && ((EReference) feature).isContainment()), "Adding existing elements into the containment reference %s is not supported.", feature.getName()); doAdd(container, feature, element, index); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public static void fillDataObject(BiMap<? extends Long, ? extends EObject> mapResult, EObject eObject, DataObject dataObject) { for (EStructuralFeature eStructuralFeature : eObject.eClass().getEAllStructuralFeatures()) { Object eGet = eObject.eGet(eStructuralFeature); if (eStructuralFeature.getEAnnotation("hidden") == null && !eStructuralFeature.isDerived()) { if (eStructuralFeature instanceof EAttribute) { if (eStructuralFeature.isMany()) { dataObject.getValues().add(listDataValue); if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDoubleObject() || eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDouble()) { EStructuralFeature asStringFeature = eObject.eClass().getEStructuralFeature(eStructuralFeature.getName() + "AsString"); List list = (List) eObject.eGet(asStringFeature); for (Object o : list) { SimpleDataValue dataValue = StoreFactory.eINSTANCE.createSimpleDataValue(); dataValue.setStringValue(o.toString()); listDataValue.getValues().add(dataValue); if (eObject.eIsSet(eStructuralFeature)) { if (eGet instanceof byte[]) { dataValue.setStringValue(new String(((byte[])eGet), Charsets.UTF_8)); List list = (List)eObject.eGet(eObject.eClass().getEStructuralFeature(eStructuralFeature.getName() + "AsString")); if (item.eClass().getEAnnotation("wrapped") != null) { EObject referenceEObject = item; SimpleDataValue simpleDataValue = StoreFactory.eINSTANCE.createSimpleDataValue(); simpleDataValue.setStringValue(referenceEObject.eGet(referenceEObject.eClass().getEStructuralFeature("wrappedValue")).toString()); referenceDataValue.setTypeName(item.eClass().getName());
/** * Returns whether this factory is applicable for the type of the object. * <!-- begin-user-doc --> * This implementation returns <code>true</code> if the object is either the model's package or is an instance object of the model. * <!-- end-user-doc --> * @return whether this factory is applicable for the type of the object. * @generated */ @Override public boolean isFactoryForType(Object object) { if (object == modelPackage) { return true; } if (object instanceof EObject) { return ((EObject)object).eClass().getEPackage() == modelPackage; } return false; }
@Override public EObject createChild(EObject container, EReference reference, EClass clazz) throws ModelManipulationException { EClass containerClass = container.eClass(); Preconditions .checkArgument( !(containerClass.getEAllReferences().contains(container)), "The container of EClass %s does neither define or inherit an EReference %s.", containerClass.getName(), reference.getName()); Preconditions .checkArgument(reference.isContainment(), "Created elements must be inserted directly into the containment hierarchy."); Preconditions.checkArgument(!clazz.isAbstract(), "Cannot instantiate abstract EClass %s.", clazz.getName()); return doCreate(container, reference, clazz); }
protected void validate() { if (value != null && !eStructuralFeature.getEType().isInstance(value)) { String valueClass = value instanceof EObject ? ((EObject)value).eClass().getName() : value.getClass().getName(); throw new ClassCastException ("The feature '" + eStructuralFeature.getName() + "'s type '" + eStructuralFeature.getEType().getName() + "' does not permit a value of type '" + valueClass + "'"); } }
/** * This returns the default create children help text. */ public String getCreateChildrenToolTipText(Object object) { EObject refObject = (EObject) object; Collection localChildrenReferences = getChildrenReferences(object); if (localChildrenReferences.size() != 1) { return CommonEditResourceHandler.getString("19concat_UI_", (new Object[]{refObject.eClass().getName()})); //$NON-NLS-1$ } return CommonEditResourceHandler.getString("20concat_UI_", (new Object[]{((EReference) localChildrenReferences.iterator().next()).getEType().getName(), refObject.eClass().getName()})); //$NON-NLS-1$ = "Create two new children of type {0} for the selected {1}." }
@SuppressWarnings("unchecked") private void writeTable(PrintWriter out, EObject eObject) { out.println("<h1>" + eObject.eClass().getName() + "</h1>"); out.println("<table>"); for (EStructuralFeature eStructuralFeature : eObject.eClass().getEAllStructuralFeatures()) { if (eStructuralFeature.getEAnnotation("hidden") == null) { out.println("<tr>"); out.println("<td>" + eStructuralFeature.getName() + "</td>"); Object eGet = eObject.eGet(eStructuralFeature); if (eStructuralFeature instanceof EAttribute) { if (eStructuralFeature.getUpperBound() == 1) { out.println("<td>" + eGet + "</td>"); } else { if (eStructuralFeature.getUpperBound() == 1) { if (eStructuralFeature.getEType().getEAnnotation("wrapped") != null) { EObject value = (EObject) eGet; if (value != null) { out.println("<td>" + value.eGet(value.eClass().getEStructuralFeature("wrappedValue")) + "</td>"); if (eStructuralFeature.getEType().getEAnnotation("wrapped") != null) { List<EObject> list = (List<EObject>) eGet; out.println("<td>"); for (EObject object : list) { out.println("<td>" + object.eGet(object.eClass().getEStructuralFeature("wrappedValue")) + "</td>");
for (Long key : map.keySet()) { EObject object = map.get(key); System.out.println("#" + key + " " + object.eClass().getName()); for (EAttribute attribute : object.eClass().getEAllAttributes()) { System.out.println("\t" + attribute.getName() + "(" + attribute.getEType().getName() + "): " + object.eGet(attribute)); for (EReference reference : object.eClass().getEAllReferences()) { System.out.println("\t" + reference.getName() + "(" + reference.getEReferenceType().getName() + "): "); if (object.eGet(reference) instanceof EObjectResolvingEList) { EObjectResolvingEList<EObject> referencedObjectList = (EObjectResolvingEList<EObject>)object.eGet(reference); if (referencedObjectList != null && referencedObjectList.size() > 0) { if (reference.getEReferenceType().getEStructuralFeature("wrappedValue") != null) { for (Object obj : referencedObjectList) { Object wrappedValue = ((EObject)obj).eGet(reference.getEReferenceType().getEStructuralFeature("wrappedValue")); System.out.println(wrappedValue.toString()); if (referencedObjectList.get(referencedObjectList.size()-1) != obj) { if (object.eGet(reference) != null) { System.out.println("=>"); } else {
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected void doMoveTo(EObject what, EObject newContainer, EReference reference, int index) throws ModelManipulationException { if (reference.isMany()) { ((EList)newContainer.eGet(reference)).add(index, what); } else{ newContainer.eSet(reference, what); } }
public void validate(Object value) { if (value != null && !eStructuralFeature.getEType().isInstance(value)) { String valueClass = value instanceof EObject ? ((EObject)value).eClass().getName() : value.getClass().getName(); throw new ClassCastException ("The feature '" + eStructuralFeature.getName() + "'s type '" + eStructuralFeature.getEType().getName() + "' does not permit a value of type '" + valueClass + "'"); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) private EObject from(JsonParser jp, EClass eClass) throws JsonParseException, IOException { EObject result = eClass.getEPackage().getEFactoryInstance().create(eClass); Map<String, EStructuralFeature> fieldFeatureMap = JsonMetadata.INSTANCE.getJsonFeatures(eClass); EDataType dataType = (EDataType) feature.getEType(); if (feature.isMany()) { if (nextToken == JsonToken.START_ARRAY) { EList values = new BasicEList(); while (jp.nextToken() != JsonToken.END_ARRAY) { values.add(parseValueForDataType(jp, dataType)); result.eSet(feature, values); result.eSet(feature, parseValueForDataType(jp, dataType)); if (!eRef.isContainment()) { continue; // Non-containment references are currently ignored if (feature.isMany() && nextToken == JsonToken.START_ARRAY) { EList values = new BasicEList(); while (jp.nextToken() != JsonToken.END_ARRAY) { values.add(from(jp, eRef.getEReferenceType())); result.eSet(feature, values); result.eSet(feature, from(jp, eRef.getEReferenceType()));
@SuppressWarnings("unchecked") public void updateNode(T expressId, EClass ec, IdEObject eObject) throws DeserializeException { for (WaitingObject waitingObject : waitingObjects.get(expressId)) { if (waitingObject.getEReference().isMany()) { AbstractEList<EObject> list = (AbstractEList<EObject>) waitingObject.getObject().eGet(waitingObject.getEReference()); if (waitingObject instanceof SingleWaitingObject) { list.addUnique(eObject); } else { ListWaitingObject listWaitingObject = (ListWaitingObject)waitingObject; if (((EClass) waitingObject.getEReference().getEType()).isSuperTypeOf(eObject.eClass())) { while (list.size() <= listWaitingObject.getIndex()) { EObject create = ec.getEPackage().getEFactoryInstance().create(eObject.eClass()); ((IdEObjectImpl)create).setOid(-2); list.addUnique(create); throw new DeserializeException(waitingObject.getLineNumber(), "Field " + waitingObject.getEReference().getName() + " of " + waitingObject.getEReference().getEContainingClass().getName() + " cannot contain a " + eObject.eClass().getName()); if (((EClass) waitingObject.getEReference().getEType()).isSuperTypeOf(eObject.eClass())) { waitingObject.getObject().eSet(waitingObject.getEReference(), eObject); } else { throw new DeserializeException(waitingObject.getLineNumber(), "Field " + waitingObject.getEReference().getName() + " of "
/** * @since 2.4 */ protected String getImportedNamespace(EObject object) { EStructuralFeature feature = object.eClass().getEStructuralFeature("importedNamespace"); if (feature != null && String.class.equals(feature.getEType().getInstanceClass())) { return (String) object.eGet(feature); } return null; }
private void addToList(EStructuralFeature eStructuralFeature, int index, AbstractEList list, EObject referencedObject) throws DeserializeException { EClass referenceEClass = referencedObject.eClass(); if (((EClass) eStructuralFeature.getEType()).isSuperTypeOf(referenceEClass)) { while (list.size() <= index) { list.addUnique(referencedObject); } } else { throw new DeserializeException(-1, referenceEClass.getName() + " cannot be stored in " + eStructuralFeature.getName()); } }
public void appendShortFragment(EObject obj, StringBuilder target) { EReference containmentFeature = obj.eContainmentFeature(); if (containmentFeature == null) { target.append(obj.eResource().getContents().indexOf(obj)); } else { EObject container = obj.eContainer(); appendShortFragment(container, target); target.append('.').append(container.eClass().getFeatureID(containmentFeature)); if (containmentFeature.isMany()) { List<?> list = (List<?>) container.eGet(containmentFeature); target.append('.').append(list.indexOf(obj)); } } }
/** * This yields an encoding of the owner-child relation. */ public Collection<String> getParameters() { Collection<String> parameters = new ArrayList<String>(); EObject eObject = getEOwner(); EStructuralFeature eStructuralFeature = getEStructuralFeature(); if (eObject != null && eStructuralFeature != null) { parameters.add(eObject.eClass().getName()); parameters.add(eStructuralFeature.getEType().getName()); } return parameters; }
/** * Method which looks up a structure feature of an eobject, first doing an exact name match, * then a case insensitive one. * * @param eobject The eobject. * @param property The property * @return The structure feature, or <code>null</code> if not found. */ public static EStructuralFeature feature(EObject eobject, String property) { EStructuralFeature feature = eobject.eClass().getEStructuralFeature(property); if (feature != null) { return feature; } // do a case insentive check, need to do the walk up the type hierarchy for (Iterator itr = eobject.eClass().getEAllStructuralFeatures().iterator(); itr.hasNext(); ) { feature = (EStructuralFeature) itr.next(); if (feature.getName().equalsIgnoreCase(property)) { return feature; } } return null; }