Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Dynamically instantiating enums independent of enum type
Dynamically instantiating enums independent of enum type [message #1095520] Tue, 27 August 2013 06:58 Go to next message
Caspar D. is currently offline Caspar D.Friend
Messages: 35
Registered: July 2009
Member
A plain Java enum can be instantiated from a String that
corresponds to one of the constants declared in that enum,
without knowing the actual enum type:

String enumValAsString = ...; // Obtained from storage
Class<Enum> enumClass = ... ; // No need to know which enum it is
Enum<?> z = Enum.valueOf(enumClass, enumValAsString);

Works. But EMF enums override toString to return their custom
literal, which isn't the same as the name of the corresponding
const -- the former being lowercase and the latter uppercase.
So when I persist my EMF enums the same way I persist all my
enums including plain-Java enums, i.e. in their "toString" form,
the above code doesn't work.

There is the static "getByName" method which I can invoke
reflectively, but since that takes the name, not the literal,
I would have to call getName on saving them, which means I would
have to treat EMF enums as 'special' on *both* save and load.

So my question is: is there some better way of doing this that I'm
overlooking? I would like to just treat EMF enums as any other enums
both when I save them to persistence and when I load them.

Thanks
--
Caspar
Re: Dynamically instantiating enums independent of enum type [message #1095576 is a reply to message #1095520] Tue, 27 August 2013 08:32 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You are probably trying to be too clever. Let EMF do the work for you.

You should find that XXXFactoryImpl.convertYYYToString and
createYYYFromString do what is required.

Regards

Ed Willink

On 27/08/2013 07:58, Caspar De Groot wrote:
> A plain Java enum can be instantiated from a String that
> corresponds to one of the constants declared in that enum,
> without knowing the actual enum type:
>
> String enumValAsString = ...; // Obtained from storage
> Class<Enum> enumClass = ... ; // No need to know which enum it is
> Enum<?> z = Enum.valueOf(enumClass, enumValAsString);
>
> Works. But EMF enums override toString to return their custom
> literal, which isn't the same as the name of the corresponding
> const -- the former being lowercase and the latter uppercase.
> So when I persist my EMF enums the same way I persist all my
> enums including plain-Java enums, i.e. in their "toString" form,
> the above code doesn't work.
>
> There is the static "getByName" method which I can invoke
> reflectively, but since that takes the name, not the literal,
> I would have to call getName on saving them, which means I would
> have to treat EMF enums as 'special' on *both* save and load.
>
> So my question is: is there some better way of doing this that I'm
> overlooking? I would like to just treat EMF enums as any other enums
> both when I save them to persistence and when I load them.
>
> Thanks
> --
> Caspar
Re: Dynamically instantiating enums independent of enum type [message #1095624 is a reply to message #1095576] Tue, 27 August 2013 09:59 Go to previous messageGo to next message
Caspar D. is currently offline Caspar D.Friend
Messages: 35
Registered: July 2009
Member
The methods you recommend are in the generated factory class. I
can't use those because the point is to perform the conversion
without being tied to specific packages.

However, I took a look at the base implementation in EFactoryImpl,
which delegates to EcoreUtil.convertToString, and that seems to
work fine, as does its converse EcoreUtil.createFromString.

[UPDATE] But I just realized that still doesn't allow me to treat
them as plain Java enums, which in fact they are.

Thanks
--
Caspar

[Updated on: Tue, 27 August 2013 12:01]

Report message to a moderator

Re: Dynamically instantiating enums independent of enum type [message #1095730 is a reply to message #1095624] Tue, 27 August 2013 12:53 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi, Caspar,

AFAIK, the Java Enum API uses the name as provided by the name() method (which, unlike toString(), cannot be overridden) to look up enum values.

So, I think you should be able to convert any Enum<?> generically to string and back using name() and valueOf() methods.

HTH,

Christian
Re: Dynamically instantiating enums independent of enum type [message #1096156 is a reply to message #1095730] Wed, 28 August 2013 02:53 Go to previous message
Caspar D. is currently offline Caspar D.Friend
Messages: 35
Registered: July 2009
Member
Darn, you're right. I never knew about name().

Thanks!

--
Caspar
Previous Topic:How can I create a Map with Xcore?
Next Topic:How to display edges in UML model editor
Goto Forum:
  


Current Time: Thu Apr 25 12:31:31 GMT 2024

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

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

Back to the top