Skip to main content



      Home
Home » Modeling » EMF » Is there a "CanAdd" API?(An API that tells whether an EObject would accept another EObject as child)
Is there a "CanAdd" API? [message #1805075] Fri, 05 April 2019 08:26 Go to next message
Eclipse UserFriend
Hi all!

I am currently searching some EMF-Reflection API like "canAdd", "accepts" or something like that which - for two EObjects A and B - tells me whether I can add B as a child to A.

i.e. something like this

EcoreUtil.canAdd(EObject, EObject) :: bool

... that accesses the structuralFeatures, references incl. types and bounds etc. to answer accordingly.

Is there such an API? (I didn't find something like that, yet. Maybe, I missed something).


Thanks in advance,
Ingo
Re: Is there a "CanAdd" API? [message #1805080 is a reply to message #1805075] Fri, 05 April 2019 09:14 Go to previous messageGo to next message
Eclipse UserFriend
No, there isn't something like that. You can iterate over A.eClass().getEAllReferences() and for each such eReference you can test eReference.getEReferenceType().isInstance(B) to find one (of possibly many) of A's EReferences that can reference a B. You might only be interested in A.eClass().getEAllContainments() if you want to find a containment reference.
Re: Is there a "CanAdd" API? [message #1805085 is a reply to message #1805080] Fri, 05 April 2019 09:30 Go to previous messageGo to next message
Eclipse UserFriend
Something like that should do the trick:

public static boolean canAdd(EObject container, EObject potentialChild)
{
	return getContainmentReference(container, potentialChild) != null;
}

public static EReference getContainmentReference(EObject container, EObject potentialChild)
{
	EReference res = null;

	for (final EReference eReference : container.eClass().getEAllContainments())
	{
		final EClass refEClass = eReference.getEReferenceType();
		if (refEClass.isInstance(potentialChild))
		{
			res = eReference;
			break;
		}
	}

	return res;
}

@SuppressWarnings("unchecked")
public static boolean add(EObject container, EObject potentialChild)
{
	boolean res = false;
	final EReference eReference = getContainmentReference(container, potentialChild);

	if (eReference != null)
	{
		if (eReference.isMany())
		{
			final var values = (List<EObject>) container.eGet(eReference);
			values.add(potentialChild);
		}
		else
		{
			container.eSet(eReference, potentialChild);
		}

		res = true;
	}

	return res;
}

[Updated on: Mon, 08 April 2019 05:40] by Moderator

Re: Is there a "CanAdd" API? [message #1805087 is a reply to message #1805085] Fri, 05 April 2019 09:40 Go to previous messageGo to next message
Eclipse UserFriend
Hi

For an arbitrary metamodel a "canAdd" seems dangerously uncontrolled since there may be many possible children. Which to use? I suggest revisiting your requirement.

For a specific metamodel, you are presumably in control and so you might add your own canAdd API to your model elements or implement a derived XXXSwitch to give the correct answer.

Regards

Ed Willink
Re: Is there a "CanAdd" API? [message #1805091 is a reply to message #1805087] Fri, 05 April 2019 10:55 Go to previous messageGo to next message
Eclipse UserFriend
Note that this only works for generated models:
		final Class<?> instanceClass = eReference.getEReferenceType().getInstanceClass();
		if (instanceClass.isAssignableFrom(childClass))
Better just to use EClass.isInstance which for a generated model will use Class.isInstance.
Re: Is there a "CanAdd" API? [message #1805134 is a reply to message #1805091] Mon, 08 April 2019 03:21 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for your quick help!
That should do the job.
Re: Is there a "CanAdd" API? [message #1805140 is a reply to message #1805091] Mon, 08 April 2019 05:42 Go to previous message
Eclipse UserFriend
Ed Merks wrote on Fri, 05 April 2019 14:55
Note that this only works for generated models:
		final Class<?> instanceClass = eReference.getEReferenceType().getInstanceClass();
		if (instanceClass.isAssignableFrom(childClass))
Better just to use EClass.isInstance which for a generated model will use Class.isInstance.


Ok, I edited my previous post.
Previous Topic:Save Emf probleme with many reference to save
Next Topic:How can I contribute
Goto Forum:
  


Current Time: Sun Jul 13 17:20:39 EDT 2025

Powered by FUDForum. Page generated in 0.03279 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top