4

I want to create an Enum editor, which takes an Enum type as its generic. E is a generic type, but restricted to be an Enum type. How can I get the values of the Enum class from the instance e?

public class ComboBoxEnumEditor<E extends Enum<E>>{
    public ComboBoxEnumEditor(E e) {
        // how to get values of E from e?
        // attemp1:
        List values = e.getClass().values();
        // attemp2:
        List values = ((Enum.class)e.getClass()).values();
        // attemp3:
        List values = ((Enum.class)e.getClass()).values();
        // none of the above works... 
    }
}

Say I have an Enum

public enum Location {
    Default( false, EAttributeLocation.MAIN_TABLE_IF_AVAILABLE ),
    Main( false, EAttributeLocation.MAIN_TABLE ),
    Overflow( false, EAttributeLocation.OVERFLOW_TABLE ),
    Separate( false, EAttributeLocation.SEPARATE_TABLE );
    ......
}

I want my ComboBoxEnumEditor be able to do

{
   ComboBoxEnumEditor(new Location());
}

Please help, thanks.

0

3 Answers 3

14

It looks like you are looking for (but I may be mistaken)

Enum[] values = e.getClass().getEnumConstants();

or as mentioned by @pbabcdefp in this answer (big +1 for him) if you would like to have E[] instead of Enum[]

E[] values = e.getDeclaringClass().getEnumConstants();

Also based on

...which takes an Enum Class as its generic

your argument should probably be Class<E> clazz not E e itself so you could use it with ComboBoxEnumEditor(Location.class);. In that case you could simply use

E[] values = clazz.getEnumConstants();
Sign up to request clarification or add additional context in comments.

1 Comment

This is how I would do it. EnumSet.allOf(enumClass) can also be useful if you need to perform efficient set operations.
5
E[] arr = e.getDeclaringClass().getEnumConstants();

5 Comments

Thanks a lot, this is exactly what I was looking for. I wish I could make both of you and Pshemo as answers. but he was a little bit faster.
e.getDeclaringClass() would be nice if OP really intend to use E e. But with argument Class<E> clazz instead E[] values = clazz.getEnumConstants(); compiles also fine.
@Alex No worries, Pshemo's answer is more detailed than mine. See this: stackoverflow.com/questions/27892675/…
@Pshemo I agree that using clazz is better. I should have mentioned it in my answer, so +1 to you. IMHO Java enums are excellent apart from the confusion caused by 3 ways to get constants (values(), getEnumConstants() and EnumSet.allOf()).
Yes that is true, we are screwed by fact that Enum doesn't have (maybe even abstract) values() method since it is dynamically added by compiler to each specific enum. It would make our life easier.
0

Short answer... You can't. Doing this is not valid syntax since you can't instantiate an enum:

ComboBoxEnumEditor(new Location());

Instead, you need to pass the class of your enum and change your method signature for that, e.g.

ComboBoxEnumEditor(Location.class);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.