R Pervasive wrote: Is it possible to get that bit value?
Yes, it is possible using reflection.
Here is an example for enums up to 64 elements (bits):
This code accesses a private field of Enum object and is highly implementation dependent,
could run on some versions of JVM/operating systems, could break on others or on future versions of JVM,
so don't use this method unless you really must.
I am facing a similar problem to that exposed in this thread..
I want to use an EnumSet instead of bit fields, but my problem is that I need a constructor which has to accept an integer representing the members of the enum which have to be present in the EnumSet..
Do I have to make an explicit switch (s) in the constructor? Or is there a better solution?
The reverse of the above works almost as well. Only trick is the EnumSet must be created, such as with the noneOf() call below. Again, this assumes the "RegularEnumSet", with 64 or less bits, but you wanted a bitmask passed in as a 32-bit int, so that seems a safe assumption. Standard disclaimers about subject to changes in implementation details apply.
Joined: Feb 23, 2011
On reflection (pun intended), this isn't that difficult to do without using reflection: