[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.tools.emf] Re: Is there a way to extend EFactories?
|
Ed Merks wrote:
For writing you could specialize XMLSaveImpl.shouldSaveFeature to omit
things. Although I'm not sure I understand why you'd ever need to read
a v2 thing and write out a v1 thing... Perhaps the problem is simpler
than you imagine?
No, of course no one would want to do that. No, what we do is "hide"
the implementation (v1, v2, etc.) from the client code, so it's not that
we'd want to read v1 and write v2, it's just that at that level, we
don't know exactly what version we're dealing with. We also have to
deal with extensibility, so that adds a layer of obfuscation as well,
which is unfortunate, but probably as simple as we can imagine.
In order to put this thread to bed and to provide an answer to my
original question (which seems to rarely ever happen when you're
searching for answers :) ) here's the code snippet I came up with to
solve my problem:
public static <T extends EObject> T create(
EFactory eFactory, EClass eClass, Class<T> javaClass) {
EPackage ePackage = eFactory.getEPackage();
for (EClassifier each : ePackage.getEClassifiers()) {
if (each instanceof EClass) {
EClass eachEClass = (EClass) each;
if (eClass.isSuperTypeOf(eachEClass)) {
return (T) eFactory.create(eachEClass);
}
}
}
throw new IllegalArgumentException(
"Factory does not support objects of type \'" +
eClass.getName() + "\'");
}
Of course, this only works if there is one and only one extension for
each type in your resource, but that's our case, so it works fine for us.