Skip to content

Instantly share code, notes, and snippets.

@pvlasov
Created January 6, 2019 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvlasov/78d0c212bba54342d52613b725f89e6c to your computer and use it in GitHub Desktop.
Save pvlasov/78d0c212bba54342d52613b725f89e6c to your computer and use it in GitHub Desktop.
Collecting and instantiating concrete subclasses of a specified base class. Can be used to collect children for a reference in emf-based editor
/**
* Iterates over registered ecore packages. Collects and instantiates concrete subclasses.
* @return
*/
public static List<MyType> collectMyTypes() {
List<MyType> ret = new ArrayList<>();
IExtensionRegistry registry = RegistryFactory.getRegistry();
if (registry != null) {
IConfigurationElement[] configElems = registry.getConfigurationElementsFor("org.eclipse.emf.ecore.generated_package");
for (IConfigurationElement elem : configElems) {
String uri = elem.getAttribute("uri");
if (!Util.isBlank(uri)) {
EPackage epkg = EPackage.Registry.INSTANCE.getEPackage(uri);
if (epkg != null) {
for (EClassifier eClassifier: epkg.getEClassifiers()) {
if (eClassifier instanceof EClass
&& !((EClass) eClassifier).isAbstract()
&& MyPackage.Literals.MY_TYPE.isSuperTypeOf((EClass) eClassifier)) {
EFactory eFactory = epkg.getEFactoryInstance();
ret.add((MyType) eFactory.create((EClass) eClassifier));
}
}
}
}
}
}
return ret;
}
@pvlasov
Copy link
Author

pvlasov commented Feb 17, 2019

Using the extension registry to enumerate over all registered packages instead of the package registry because the package registry does not "see" workspace packages when running an Eclipse application from IDE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment