Beispiel #1
0
  @Test
  public void getAddedAndRemoved() {
    final EObject obj00 = EcoreUtil.create(eClass0);
    final EObject obj1 = EcoreUtil.create(eClass1);
    final EObject obj01 = EcoreUtil.create(eClass0);
    final EObject obj02 = EcoreUtil.create(eClass0);
    final List<Notification> messages = new ArrayList<Notification>();
    obj1.eAdapters()
        .add(
            new AdapterImpl() {
              @Override
              public void notifyChanged(Notification msg) {
                messages.add(msg);
              }
            });

    // Add
    obj1.eSet(multiRef, Arrays.asList(new EObject[] {obj00, obj01}));
    EMFUtils.setOrAdd(obj1, multiRef, obj02);

    // Remove
    final List list = (List) obj1.eGet(multiRef);
    list.remove(obj00);
    list.removeAll(Arrays.asList(new EObject[] {obj01, obj02}));

    final List<EObject> addedObjects = new ArrayList<EObject>();
    final List<EObject> removedObjects = new ArrayList<EObject>();
    for (Notification msg : messages) {
      addedObjects.addAll(EMFUtils.getAddedObjects(msg, obj00.getClass()));
      removedObjects.addAll(EMFUtils.getRemovedObjects(msg, obj00.getClass()));
    }

    assertEquals(Arrays.asList(new EObject[] {obj00, obj01, obj02}), addedObjects);
    assertEquals(Arrays.asList(new EObject[] {obj00, obj01, obj02}), removedObjects);
  }
Beispiel #2
0
 @Test
 public void set() {
   EObject obj0 = EcoreUtil.create(eClass0);
   EObject obj1 = EcoreUtil.create(eClass1);
   EMFUtils.setOrAdd(obj0, singleRef, obj1);
   assertEquals(obj1, obj0.eGet(singleRef));
 }
Beispiel #3
0
 @Test
 public void containingListAndIndex() {
   EObject obj0 = EcoreUtil.create(eClass0);
   assertNull(EMFUtils.getContainingList(obj0));
   assertEquals(-1, EMFUtils.getIndexWithinParent(obj0));
   EObject obj1 = EcoreUtil.create(eClass1);
   assertNull(EMFUtils.getContainingList(obj1));
   assertEquals(-1, EMFUtils.getIndexWithinParent(obj1));
   EMFUtils.setOrAdd(obj1, multiRef, obj0);
   assertTrue(obj1.eGet(multiRef) == EMFUtils.getContainingList(obj0));
   assertEquals(0, EMFUtils.getIndexWithinParent(obj0));
 }
  /**
   * This adds {@link org.eclipse.emf.edit.command.CommandParameter}s describing the children that
   * can be created under this object.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generatedNOT
   */
  @Override
  protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object) {
    super.collectNewChildDescriptors(newChildDescriptors, object);

    // newChildDescriptors.add(createChildParameter(
    // AnnotationsmodelPackage.Literals.ANNOTATED_EENUM_LITERAL__EENUM_LITERAL_ANNOTATIONS,
    // AnnotationsmodelFactory.eINSTANCE.createEEnumLiteralAnnotation()));

    final AnnotatedEEnumLiteral aEnum = (AnnotatedEEnumLiteral) object;
    for (EClass eClass :
        AnnotationsModelRegistry.getInstance()
            .getSubEClasses(AnnotationsmodelPackage.Literals.EENUM_LITERAL_ANNOTATION)) {
      if (eClass.isAbstract()) {
        continue;
      }
      boolean isUsed = false;
      for (EEnumLiteralAnnotation annotation : aEnum.getEEnumLiteralAnnotations()) {
        if (annotation.eClass() == eClass) {
          isUsed = true;
          break;
        }
      }
      if (!isUsed && !eClass.isAbstract()) {
        newChildDescriptors.add(
            createChildParameter(
                AnnotationsmodelPackage.Literals.ANNOTATED_EENUM_LITERAL__EENUM_LITERAL_ANNOTATIONS,
                EcoreUtil.create(eClass)));
      }
    }
  }
  public void testCustomDefaultLiteral() throws CommitException {
    // valid default literal
    att.setDefaultValueLiteral("1;2");

    EObject obj = EcoreUtil.create(cls);
    obj.eUnset(att);

    {
      CDOSession session = openSession();
      session.getPackageRegistry().putEPackage(pkg);
      CDOTransaction tx = session.openTransaction();
      CDOResource res = tx.createResource(getResourcePath("/test"));
      res.getContents().add(obj);
      tx.commit();
      tx.close();
      session.close();
    }

    clearCache(getRepository().getRevisionManager());

    {
      CDOSession session = openSession();
      session.getPackageRegistry().putEPackage(pkg);
      CDOView v = session.openView();
      CDOResource res = v.getResource(getResourcePath("/test"));
      EObject persistent = res.getContents().get(0);

      CustomType pCustom = (CustomType) persistent.eGet(att);
      assertEquals(1, pCustom.getA());
      assertEquals(2, pCustom.getB());

      v.close();
      session.close();
    }
  }
  public void testCustomRegular() throws CommitException {
    EObject obj = EcoreUtil.create(cls);
    obj.eSet(att, new CustomType(23, 42));

    {
      CDOSession session = openSession();
      session.getPackageRegistry().putEPackage(pkg);
      CDOTransaction tx = session.openTransaction();
      CDOResource res = tx.createResource(getResourcePath("/test"));
      res.getContents().add(obj);
      tx.commit();
      tx.close();
      session.close();
    }

    clearCache(getRepository().getRevisionManager());

    {
      CDOSession session = openSession();
      session.getPackageRegistry().putEPackage(pkg);
      CDOView v = session.openView();
      CDOResource res = v.getResource(getResourcePath("/test"));
      EObject persistent = res.getContents().get(0);

      CustomType pCustom = (CustomType) persistent.eGet(att);
      assertEquals(23, pCustom.getA());
      assertEquals(42, pCustom.getB());

      v.close();
      session.close();
    }
  }
  public void testBugzilla_246442() throws Exception {
    CDOID lookupObject = null;

    {
      EPackage topPackage = createDynamicEPackage();

      EPackage subpackage1 = topPackage.getESubpackages().get(0);
      EClass class1Class = (EClass) subpackage1.getEClassifier("class1");

      CDOSession session = openSession();
      session.getPackageRegistry().putEPackage(topPackage);

      CDOTransaction transaction = session.openTransaction();
      CDOObject instance = CDOUtil.getCDOObject(EcoreUtil.create(class1Class));

      CDOResource resource = transaction.createResource(getResourcePath("/test1"));
      resource.getContents().add(instance);
      transaction.commit();

      lookupObject = instance.cdoID();
      session.close();
    }

    CDOSession session = openSession();
    if (session instanceof org.eclipse.emf.cdo.net4j.CDONet4jSession) {
      ((org.eclipse.emf.cdo.net4j.CDONet4jSession) session)
          .options()
          .getNet4jProtocol()
          .setTimeout(2000L);
    }

    CDOTransaction transaction = session.openTransaction();
    transaction.getObject(lookupObject);
  }
 protected EObject createInitialModel() {
   EClass eClass = ExtendedMetaData.INSTANCE.getDocumentRoot(dsPackage);
   EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature("data");
   EObject rootObject = dsFactory.create(eClass);
   rootObject.eSet(eStructuralFeature, EcoreUtil.create((EClass) eStructuralFeature.getEType()));
   return rootObject;
 }
Beispiel #9
0
  @SuppressWarnings("deprecation")
  private EObject createEntry(String key, JsonNode value) {
    EObject eObject = EcoreUtil.create(EcorePackage.Literals.ESTRING_TO_STRING_MAP_ENTRY);
    eObject.eSet(EcorePackage.Literals.ESTRING_TO_STRING_MAP_ENTRY__KEY, key);
    eObject.eSet(EcorePackage.Literals.ESTRING_TO_STRING_MAP_ENTRY__VALUE, value.getValueAsText());

    return eObject;
  }
Beispiel #10
0
    @Override
    protected void preRun() throws Exception {
      SelectClassDialog dialog =
          new SelectClassDialog(
              page, "New Root Object", "Select a package and a class for the new root object.");

      if (dialog.open() == SelectClassDialog.OK) {
        EClass eClass = dialog.getSelectedClass();
        object = EcoreUtil.create(eClass);
      } else {
        cancel();
      }
    }
Beispiel #11
0
  private EObject createEObject(Resource resource, EClass eClass, JsonNode node) {
    if (node.isObject()) {
      final EObject object = EcoreUtil.create(eClass);
      processed.put(object, node);

      fillEAttribute(object, node);
      fillEContainment(object, node, resource);

      return object;
    }

    return null;
  }
  @Override
  public void initialize(IInitializationParticipantContext context, Object facet) {
    if (!(facet instanceof EReference)) return;
    final EReference ref = (EReference) facet;
    if (!ref.isRequired() || ref.isContainer()) return;

    final EObject object = context.getObject();

    if (ref.getUpperBound() != 1) {
      /*
       * A list
       */
      @SuppressWarnings("unchecked")
      final EList<? extends EObject> l =
          (EList<? extends EObject>) context.getStructuralFeature(ref);
      int missing = ref.getLowerBound() - l.size();
      if (missing < 0) return;

      final List<EObject> newChildren = new ArrayList<EObject>(missing);
      for (; missing > 0; missing--) {
        final EObject newChild = EcoreUtil.create(ref.getEReferenceType());
        context.addCommand(
            IManager.Factory.getManager()
                .initializeObject(context.getEditingDomain(), object, ref, newChild, false));
      }
      context.addCommand(AddCommand.create(context.getEditingDomain(), object, ref, newChildren));
    } else {
      /*
       * Simple object
       */
      final Object o = context.getStructuralFeature(ref);
      if (o != null) return;

      final EObject newChild = EcoreUtil.create(ref.getEReferenceType());
      context.addCommand(
          IManager.Factory.getManager()
              .initializeObject(context.getEditingDomain(), object, ref, newChild, true));
    }
  }
Beispiel #13
0
 public static void setSimplifiedNestedField(
     final EObject owner,
     final EReference firstLevelFeature,
     EAttribute simplifiedFeature,
     final Object newSimpleValue) {
   Object complexObject = owner.eGet(firstLevelFeature);
   if (complexObject == null) {
     complexObject = EcoreUtil.create((EClass) firstLevelFeature.getEType());
     ((EObject) complexObject).eSet(simplifiedFeature, newSimpleValue);
     owner.eSet(firstLevelFeature, complexObject);
   } else {
     ((EObject) complexObject).eSet(simplifiedFeature, newSimpleValue);
   }
 }
Beispiel #14
0
  private EObject createProxy(Resource resource, EClass eClass, JsonNode node) {
    EObject proxy = null;

    if (isRefNode(node)) {
      final URI objectURI = getEObjectURI(node.get(EJS_REF_KEYWORD), resource.getURI(), nsMap);
      proxy = EcoreUtil.create(eClass);
      ((InternalEObject) proxy).eSetProxyURI(objectURI);

      if (useProxyAttributes) {
        JsonNode refNode = JSUtil.getNode(resource, objectURI, eClass);
        if (refNode != null) fillEAttribute(proxy, refNode);
      }
    }

    return proxy;
  }
 private <T extends EObject> T copy(final T t) {
   EClass _eClass = t.eClass();
   EObject _create = EcoreUtil.create(_eClass);
   T result = ((T) _create);
   return result;
 }
Beispiel #16
0
  /**
   * Converts the value of an EReference with isMany==true, the values of the collection are
   * converted to EObjects and added to the list in the correct feature in the eObject.
   *
   * @param modelObject the modelObject from which the value is retrieved.
   * @param eObject the eObject in which the value is set (after it has been converted)
   * @param eReference the eReference which is converted
   */
  protected void convertManyEReference(
      final ModelObject<?> modelObject, final EObject eObject, final EReference eReference) {
    // container feature is always set from the other side, the containment
    // side
    if (eReference.isContainer()) {
      return;
    }

    final Object manyValue = modelObject.eGet(eReference);
    final Collection<EObject> newValues = new ArrayList<EObject>();

    boolean isMap = Map.class.isAssignableFrom(manyValue.getClass());
    EStructuralFeature valueFeature = null;
    EStructuralFeature keyFeature = null;
    if (isMap) {
      final EClass mapEClass = eReference.getEReferenceType();
      valueFeature = mapEClass.getEStructuralFeature("value"); // $NON-NLS-1$
      keyFeature = mapEClass.getEStructuralFeature("key"); // $NON-NLS-1$

      Check.isTrue(
          ModelUtils.isEMap(eReference),
          "Expected emap EReference, but th// case for EReference " //$NON-NLS-1$
              + eReference);

      final Map<?, ?> map = (Map<?, ?>) manyValue;

      for (final Object key : map.keySet()) {
        final Object value = map.get(key);
        final EObject mapEntryEObject = EcoreUtil.create(mapEClass);

        // key and value maybe primitive types but can also be
        // references to model objects.
        if (valueFeature instanceof EReference) {
          mapEntryEObject.eSet(valueFeature, createTarget(value));
        } else {
          mapEntryEObject.eSet(valueFeature, value);
        }
        if (keyFeature instanceof EReference) {
          mapEntryEObject.eSet(keyFeature, createTarget(key));
        } else {
          mapEntryEObject.eSet(keyFeature, key);
        }
        newValues.add(mapEntryEObject);
      }
    } else {

      // a many to many
      if (eReference.getEOpposite() != null && eReference.getEOpposite().isMany()) {
        final ManyToMany mtm = new ManyToMany();
        mtm.setOwner(modelObject);
        mtm.setEReference(eReference);
        toRepairManyToMany.add(mtm);
      }

      @SuppressWarnings("unchecked")
      final Collection<Object> values = (Collection<Object>) manyValue;

      for (final Object value : values) {
        if (value == null) {
          newValues.add(null);
        } else {
          final InternalEObject eValue = (InternalEObject) createTarget(value);
          if (!newValues.contains(eValue)) {
            newValues.add(eValue);
          }
        }
      }
    }

    @SuppressWarnings("unchecked")
    final Collection<EObject> eValues = (Collection<EObject>) eObject.eGet(eReference);

    boolean updateList = false;
    if (newValues.size() == eValues.size()) {
      final Iterator<?> it = eValues.iterator();
      for (Object newValue : newValues) {
        final Object oldValue = it.next();

        if (isMap) {
          final EObject oldMapEntry = (EObject) oldValue;
          final EObject newMapEntry = (EObject) newValue;
          final Object oldMapValue = oldMapEntry.eGet(valueFeature);
          final Object oldMapKey = oldMapEntry.eGet(keyFeature);
          final Object newMapValue = newMapEntry.eGet(valueFeature);
          final Object newMapKey = newMapEntry.eGet(keyFeature);
          if (valueFeature instanceof EReference) {
            updateList = oldMapValue == newMapValue;
          } else {
            updateList =
                oldMapValue != null
                    ? !oldMapValue.equals(newMapValue)
                    : newMapValue != null ? !newMapValue.equals(oldMapValue) : false;
          }
          if (keyFeature instanceof EReference) {
            updateList = updateList || oldMapKey == newMapKey;
          } else {
            updateList =
                updateList
                    || (oldMapKey != null
                        ? !oldMapKey.equals(newMapKey)
                        : newMapKey != null ? !newMapKey.equals(oldMapKey) : false);
          }
        } else {
          updateList = oldValue != newValue;
        }

        if (updateList) {
          break;
        }
      }
    } else {
      updateList = true;
    }

    if (updateList) {
      // clear the evalues so that an empty tag is created in the xml
      eValues.clear();
      eValues.addAll(newValues);
    }
  }