Dynamically instantiating enums independent of enum type [message #1095520] |
Tue, 27 August 2013 06:58 |
Caspar D. 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 |
Ed Willink 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 |
Caspar D. 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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02710 seconds