Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Determine EClass from class name string?
Determine EClass from class name string? [message #1011404] Tue, 19 February 2013 14:16 Go to next message
Eclipse UserFriend
Hello,
consider the following situation. I defined an extension point at which
extensions can register. It has only one attribute called 'class' which
is restricted to implement MyDomainClass being a EMF generated class.
When collecting the registered extensions I want to get the EClass of
the collected class. As you can see, here are both concepts mixed EClass
and Class. The extensions I expect are generated, too. My first approach
was the following:

IConfigurationElement[] elements =
Platform.getExtensionRegistry().getConfigurationElementsFor("myextension");
for (IConfigurationElement element : elements) {
IContributor contributor = element.getContributor();
Bundle bundle = Platform.getBundle(contributor.getName());
Enumeration<URL> entries = bundle.findEntries("/", "*.genmodel", true);
while (entries.hasMoreElements()) {
URL url = (URL) entries.nextElement();
InputStream stream = url.openStream();
URI uri = URI.createURI(url.getFile());
ResourceSet rs = new ResourceSetImpl();
Resource resource = rs.createResource(uri);
resource.load(stream, null);
GenModel genmodel = (GenModel) resource.getContents().get(0);
GenClass genClass = getGenClassForName(genmodel,
element.getAttribute("class"));
EClass eClass = genClass.getEcoreClass();
}
}

Then in getGenClassForName(..) I tried this to determine the correct class:

String qualifiedClassName = genClass.getQualifiedClassName();
String qualifiedInterfaceName = genClass.getQualifiedInterfaceName();
if(qualifiedClassName.equals(classNameFromExtension) ||
qualifiedInterfaceName.equals(classNameFromExtension)){
return genClass;
}

My problem is that the genClasses are proxies here and the name
comparison fails because of null values inside the qualifiedNames.

Another possibility might be to use the normal
element.createExecutableExtension("class") on the configuration element
but then I'm forced to assume that the extensions have public
constructors. I hope you can understand my problem. What is best
practice for such a scenario? How do you do this?

best regards,
Gilbert
Re: Determine EClass from class name string? [message #1011439 is a reply to message #1011404] Tue, 19 February 2013 15:04 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 854
Registered: July 2009
Senior Member
In the past, I have come up with the following brute force method:

First, resolve the Class by its string representation, i.e. Class.forName(), or better bundle.loadClass(), you know which bundle contributes the extension, so you can get the bundle, then...

- Traverse all registered EPackages (EPackage.Registry.INSTANCE)
--> Traverse all EClassifiers of each EPackage
---> Check if EClassifier.getInstanceClass() == YourClass.class
---> Save your result in a lookup table for better performance if you need it more than once (Map<Class<?>, EClass>)
Re: Determine EClass from class name string? [message #1011445 is a reply to message #1011439] Tue, 19 February 2013 15:10 Go to previous messageGo to next message
Eclipse UserFriend
Hi Erdal,

Erdal Karaca wrote:
> First, resolve the Class by its string representation, i.e.
> Class.forName(), or better bundle.loadClass(), you know which bundle
> contributes the extension, so you can get the bundle, then...
exactly, the bundle can load it

> - Traverse all registered EPackages (EPackage.Registry.INSTANCE)
> --> Traverse all EClassifiers of each EPackage
> ---> Check if EClassifier.getInstanceClass() == YourClass.class
> ---> Save your result in a lookup table for better performance if you
> need it more than once (Map<Class<?>, EClass>)
It's really a brute force approach but it might work. I didn't know the
EClassifier.getInstanceClass() method. If nobody knows a better solution
I'll do it this way.

Thanks a lot, Erdal and best regards,
Gilbert
Re: Determine EClass from class name string? [message #1011466 is a reply to message #1011445] Tue, 19 February 2013 15:43 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Gilbert,

There is no way beyond brute force and the brute force method is quite
bad because it will cause all bundles with registered models to be
activated.


On 19/02/2013 4:10 PM, Gilbert Mirenque wrote:
> Hi Erdal,
>
> Erdal Karaca wrote:
>> First, resolve the Class by its string representation, i.e.
>> Class.forName(), or better bundle.loadClass(), you know which bundle
>> contributes the extension, so you can get the bundle, then...
> exactly, the bundle can load it
>
>> - Traverse all registered EPackages (EPackage.Registry.INSTANCE)
>> --> Traverse all EClassifiers of each EPackage
>> ---> Check if EClassifier.getInstanceClass() == YourClass.class
>> ---> Save your result in a lookup table for better performance if you
>> need it more than once (Map<Class<?>, EClass>)
> It's really a brute force approach but it might work. I didn't know the
> EClassifier.getInstanceClass() method. If nobody knows a better solution
> I'll do it this way.
>
> Thanks a lot, Erdal and best regards,
> Gilbert


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[CDO] Setting Access Rights on nodes
Next Topic: org.eclipse.emf.mwe.utils.Reader is not working
Goto Forum:
  


Current Time: Sat Apr 20 00:00:36 GMT 2024

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

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

Back to the top