Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » java package of an EPackage
java package of an EPackage [message #502803] Wed, 09 December 2009 18:09 Go to next message
gary s thompson is currently offline gary s thompsonFriend
Messages: 83
Registered: July 2009
Member
Dear all
For reasons of my own I needed recently to get the java package of an ecore package at runtime and couldn't seem to find a way to do it easily. Have I missed something Wink In the end I came up with this rather inelegant hack which doesn't seem the best way.

private static String getJavaPackageNameForECorePackage(EPackage rootEPackage) {
//TODO this is a hack there should be a better way
Package rootJavaImplPackage = rootEPackage.getClass().getPackage();
List<String> packagePathParts = Arrays.asList(rootJavaImplPackage.getName().split("\\."));
return EUtils.join(packagePathParts.subList(0, packagePathParts.size()-1),".");
}

as usual any help graefully recieved
regards
gary
Re: java package of an EPackage [message #502817 is a reply to message #502803] Wed, 09 December 2009 19:25 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30653
Registered: July 2009
Senior Member
Gary,

There are no utilities for doing this. Here's a simpler idiom we us in
XMLSaveImpl:

Field field = ePackage.getClass().getField("eINSTANCE");
javaImplementationLocation = "java://" +
field.getDeclaringClass().getName();


gary s thompson wrote:
> Dear all
> For reasons of my own I needed recently to get the java package of an
> ecore package at runtime and couldn't seem to find a way to do it
> easily. Have I missed something ;) In the end I came up with this
> rather inelegant hack which doesn't seem the best way.
>
> private static String getJavaPackageNameForECorePackage(EPackage
> rootEPackage) {
> //TODO this is a hack there should be a better way
> Package rootJavaImplPackage =
> rootEPackage.getClass().getPackage();
> List<String> packagePathParts =
> Arrays.asList(rootJavaImplPackage.getName().split("\\."));
> return EUtils.join(packagePathParts.subList(0,
> packagePathParts.size()-1),".");
> }
>
> as usual any help graefully recieved
> regards
> gary
Re: java package of an EPackage [message #502836 is a reply to message #502817] Wed, 09 December 2009 20:52 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

Hi Ed,

Just out of curiosity, if you have: ePackage.getClass()

why do you need to get the field? Can't you just use the
ePackage.getClass()? Or is it possible that ePackage is not the package
class itself but a subclass that doesn't have the eINSTANCE field in it,
but inherits it?

Ed Merks wrote:
> Gary,
>
> There are no utilities for doing this. Here's a simpler idiom we us in
> XMLSaveImpl:
>
--
Thanks,
Rich Kulp
Re: java package of an EPackage [message #502840 is a reply to message #502836] Wed, 09 December 2009 21:10 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30653
Registered: July 2009
Senior Member
Rich,

Comments below.


Rich Kulp wrote:
> Hi Ed,
>
> Just out of curiosity, if you have: ePackage.getClass()
>
> why do you need to get the field? Can't you just use the
> ePackage.getClass()?
This will get org.example.xyz.impl.XyzPackageImpl, i.e., the internal
implementation of the package, when we want org.example.xyz.XyzPackage,
i.e., the public API for the package.
> Or is it possible that ePackage is not the package class itself but a
> subclass that doesn't have the eINSTANCE field in it, but inherits it?
Yes. :-P
>
> Ed Merks wrote:
>> Gary,
>>
>> There are no utilities for doing this. Here's a simpler idiom we us
>> in XMLSaveImpl:
>>
Previous Topic:[TENEO & SDO] Where is SDO?
Next Topic:Re: [CDO] TimeoutRuntimeException when trying to open session
Goto Forum:
  


Current Time: Fri Nov 22 18:52:32 GMT 2019

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

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

Back to the top