[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.tools.emf] Re: [newbie qns] reflection and class impl

Alex,

Comments below.


Tom Schindl wrote:
Alex schrieb:
Hi,

1. is it possible to create an EClass through reflection? e.g. I have a class name as a string ("MyEClass"). Now I would like to create this class with the factory. The factory has a method "EObject create(EClass arg0)" but I don't know how to get the EClass from the string.
What else do you know about the name? Something like EPackage.Registry.INSTANCE.getEPackage(<nsUR>).getEClassifier(<name>) will find the named classifier in the package. Of course if you have the instance class or instance class name, Tom's advice is good.

2. all our code is in a package seperated from the auto generated packages. If I define an operation in the ecore model (e.g. operation "myOperation" on the class "MyEClass") then I'm supposed to implement the method in the generated class "MyEClassImpl". But I would like to implement the operation in an own class (different package). Therefor I could extend the class "MyEClassImpl" but if I create this class through the factory then an object of type "MyEClassImpl" would be instantiated instead of my own implemention. Is there a good solution for this?
It's best to modify the generate MyEClassImpl directly; EMF supports merging when it regenerates, but you could also change the XyzFactoryImpl.createMyEClass method to return your Impl instead. The problem I see is that anything that extends MyEClass will extend MyEClassImpl and not your class derived from it...

thanks a lot, Alex

I don't know if this the right thing but in my code (used to instantiate classes for iBatis) I do something like this (I think it was Ed's suggestion in a thread):


((ExtendedSqlMapClient) sqlMap).getDelegate()
                    .setResultObjectFactory(new ResultObjectFactory() {

@SuppressWarnings("unchecked")
public Object createInstance(String arg0, Class arg1)
throws InstantiationException,
IllegalAccessException {


for (EClassifier cf : ingeniumPackage.eINSTANCE.getEClassifiers()) {
if (cf.getInstanceClass() == arg1) {
return EcoreUtil.create((EClass) cf);
}
}
return null;
}


public void setProperty(String arg0, String arg1) {
}


});

If performance is important you might generate a map. I suppose you could even make the constructors public (there's a generator flag for that) and just do newInstance...
I have a class but you could easily exchange this with:

cf.getInstanceClass().getName().equals("MyEClass").

Tom