Created
January 6, 2019 11:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.