Skip to main content



      Home
Home » Modeling » EMF » Get a list of all subclasses of a given class
Get a list of all subclasses of a given class [message #429694] Wed, 29 April 2009 13:50 Go to next message
Eclipse UserFriend
Hi

I'm new to EMF and have a question, that I couldn't find an answer for,
using google and the archive of this news group. Maybe the answer is
realy simple, but somehow I'm unable to figure it out myself.




The Problem:

I have designed an EMF model (using annotated interfaces) with an
abstract type "MyClass" and some subtypes "MySubClassOne",
"MySubClassTwo", "MySubClassThree", ..., that extend "MyClass"

All of this classes live in the same package. Now I want a list of all
types, that are subtypes of MyClass. How to do that?



My current approach:

1st: Get the contents of the package
2nd: and filter out all objects, that aren't a subtype of MyClass.
But what I get after the first step isn't a list like "MyClass",
"MySubclassOne", ... "MyOtherClass", ... but a list of multiple
instances of org.eclipse.emf.ecore.impl.EClassImpl

I think it's possible to get the "real" interfaces from these
EClassImpl, but couldn't figure it out.

Bellow you can find the code of my model and the routine that should
create the list.


Thanks for your help
Damian



Model:

/**

* @model abstract="true"

*/

public interface MyClass extends EObject {

/**

* @model kind="operation"

*/

public Object getValue();



/**

* @model kind="operation"

*/

public void setValue(Object value);

}


/**
* @model
*/
public interface MySubClassOne extends BeginnEndeValue {

/**
* @model
*/
public Object getValue();

/**
* -- snipp --
* @generated
*/
void setValue(Object value);
}


--> The other subclasses look the same like MySubclassOne.



Here is my failed try, to get the list of subclasses:

ModelPackage package = ModelFactory.eINSTANCE.getModelPackage();
EList<EObject> list = package.eContents();
for (EObject object : list) {
if (true) { //TODO test, if object is subtype of MyClass
System.out.println("Object in Package: " + object);
}
}
Re: Get a list of all subclasses of a given class [message #429695 is a reply to message #429694] Wed, 29 April 2009 14:39 Go to previous messageGo to next message
Eclipse UserFriend
Damian,

Comments below.

Damian Hofmann wrote:
> Hi
>
> I'm new to EMF and have a question, that I couldn't find an answer
> for, using google and the archive of this news group. Maybe the answer
> is realy simple, but somehow I'm unable to figure it out myself.
>
>
>
>
> The Problem:
>
> I have designed an EMF model (using annotated interfaces) with an
> abstract type "MyClass" and some subtypes "MySubClassOne",
> "MySubClassTwo", "MySubClassThree", ..., that extend "MyClass"
>
> All of this classes live in the same package. Now I want a list of all
> types, that are subtypes of MyClass. How to do that?
>
>
>
> My current approach:
>
> 1st: Get the contents of the package
> 2nd: and filter out all objects, that aren't a subtype of MyClass.
> But what I get after the first step isn't a list like "MyClass",
> "MySubclassOne", ... "MyOtherClass", ... but a list of multiple
> instances of org.eclipse.emf.ecore.impl.EClassImpl
An EClass has a name and and instanceClassName.
>
> I think it's possible to get the "real" interfaces from these
> EClassImpl, but couldn't figure it out.
Yes, instanceClassName should give you that, if it's a generated model.
>
> Bellow you can find the code of my model and the routine that should
> create the list.
>
>
> Thanks for your help
> Damian
>
>
>
> Model:
>
> /**
>
> * @model abstract="true"
>
> */
>
> public interface MyClass extends EObject {
>
> /**
>
> * @model kind="operation"
>
> */
>
> public Object getValue();
>
>
>
> /**
>
> * @model kind="operation"
>
> */
>
> public void setValue(Object value);
>
> }
>
>
> /**
> * @model
> */
> public interface MySubClassOne extends BeginnEndeValue {
>
> /**
> * @model
> */
> public Object getValue();
>
> /**
> * -- snipp --
> * @generated
> */
> void setValue(Object value);
> }
>
>
> --> The other subclasses look the same like MySubclassOne.
>
>
>
> Here is my failed try, to get the list of subclasses:
>
> ModelPackage package = ModelFactory.eINSTANCE.getModelPackage();
> EList<EObject> list = package.eContents();
Use getEClassifiers.
> for (EObject object : list) {
> if (true) { //TODO test, if object is subtype of MyClass
Check if it's an EClass, cast to that, then check if your target EClass
is in the getEAllSuperTypes of that class.
> System.out.println("Object in Package: " + object);
> }
> }
Re: Get a list of all subclasses of a given class [message #429702 is a reply to message #429695] Wed, 29 April 2009 16:12 Go to previous message
Eclipse UserFriend
Hi Ed

Thanks alot for your help. The most important hint was your
recommendation to use getEClassifiers() instead of eContents(). That did
the trick for me!

Greetings
Damian


For those interessted, here is the solution, that seems to work for me:

In the interface ModelPackage I added the method signature
public List<EClass> getSubClasses();



In the class ModelPackageImpl I implemented the method:

public List<EClass> getSubClasses() {
List<EClass> classList = new ArrayList<EClass>();
EList<EClassifier> classifiers = this.getEClassifiers();

EClass parentClass = this.getMyClass();
for (EClassifier classifier : classifiers) {
if (classifier instanceof EClass) {
EClass clazz = (EClass)classifier;
if (parentClass.isSuperTypeOf(clazz) &&
!clazz.isAbstract()) {
classList.add(clazz);
}
}
}
return classList;
}

Any improvments welcome!




Ed Merks wrote:
> Damian,
>
> Comments below.
>
> Damian Hofmann wrote:
>> Hi
>>
>> I'm new to EMF and have a question, that I couldn't find an answer
>> for, using google and the archive of this news group. Maybe the answer
>> is realy simple, but somehow I'm unable to figure it out myself.
>>
>>
>>
>>
>> The Problem:
>>
>> I have designed an EMF model (using annotated interfaces) with an
>> abstract type "MyClass" and some subtypes "MySubClassOne",
>> "MySubClassTwo", "MySubClassThree", ..., that extend "MyClass"
>>
>> All of this classes live in the same package. Now I want a list of all
>> types, that are subtypes of MyClass. How to do that?
>>
>>
>>
>> My current approach:
>>
>> 1st: Get the contents of the package
>> 2nd: and filter out all objects, that aren't a subtype of MyClass.
>> But what I get after the first step isn't a list like "MyClass",
>> "MySubclassOne", ... "MyOtherClass", ... but a list of multiple
>> instances of org.eclipse.emf.ecore.impl.EClassImpl
> An EClass has a name and and instanceClassName.
>>
>> I think it's possible to get the "real" interfaces from these
>> EClassImpl, but couldn't figure it out.
> Yes, instanceClassName should give you that, if it's a generated model.
>>
>> Bellow you can find the code of my model and the routine that should
>> create the list.
>>
>>
>> Thanks for your help
>> Damian
>>
>>
>>
>> Model:
>>
>> /**
>>
>> * @model abstract="true"
>>
>> */
>>
>> public interface MyClass extends EObject {
>>
>> /**
>>
>> * @model kind="operation"
>>
>> */
>>
>> public Object getValue();
>>
>>
>>
>> /**
>>
>> * @model kind="operation"
>>
>> */
>>
>> public void setValue(Object value);
>>
>> }
>>
>>
>> /**
>> * @model
>> */
>> public interface MySubClassOne extends BeginnEndeValue {
>> /**
>> * @model
>> */
>> public Object getValue();
>>
>> /**
>> * -- snipp --
>> * @generated
>> */
>> void setValue(Object value);
>> }
>>
>>
>> --> The other subclasses look the same like MySubclassOne.
>>
>>
>>
>> Here is my failed try, to get the list of subclasses:
>>
>> ModelPackage package = ModelFactory.eINSTANCE.getModelPackage();
>> EList<EObject> list = package.eContents();
> Use getEClassifiers.
>> for (EObject object : list) {
>> if (true) { //TODO test, if object is subtype of MyClass
> Check if it's an EClass, cast to that, then check if your target EClass
> is in the getEAllSuperTypes of that class.
>> System.out.println("Object in Package: " + object);
>> }
>> }
Previous Topic:[CDO] Headless Server & Equinox
Next Topic:EMF with org.eclipse.core.resources but without org.eclipse.ui.ide
Goto Forum:
  


Current Time: Fri Oct 24 13:04:27 EDT 2025

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

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

Back to the top