2

My enum types-

public enum Foo {A, B, C}

And

public enum Bar {A("aaa"), B("bbb"), C("ccc")}

All I have at runtime is the enum class name i.e. "Foo"

I do this -

Class<?> c = Class.forName(getClassName()) // local function 

Using Arrays.asList(c.getEnumConstants()) gets me -

Foo -

[A, B, C]

Bar -

[aaa, bbb, ccc]

I also want [A, B, C] when evaluating Bar.

.values() is what I want but how do I get to it dynamically without any explicit casting?

Many thanks for any replies.

Have found the solution -

List<? extends Enum<?>> enums = (List<? extends Enum<?>>) Arrays.asList(c.getEnumConstants());
for (Enum<?> e: enums) {
    System.err.println("e.name: " + e.name());
}
2
  • Have you looked at my Answer? Commented Aug 15, 2013 at 13:36
  • 1
    You should post your own solution as an answer, not as a part of the question. Commented Aug 15, 2013 at 14:20

2 Answers 2

3

I suspect your Arrays.asList(c.getEnumConstants()) is actually working correctly. What you are seeing when you print the array out is a list of the toString() results of Bar which become [aaa, bbb, ccc].

Try something like:

for (Bar b : Bar.class.getEnumConstants()) {
  System.out.println(b.name() + "(\"" + b.toString() + "\")");
}

You should see what I mean.

If you have an enum that defines its own toString() you could try wrapping it:

static class EnumNamer<T extends Enum<T>> {
  final T he;

  public EnumNamer(T he) {
    this.he = he;
  }

  @Override
  public String toString() {
    return he.name();
  }
}

public void test() {
  System.out.println("Hello");
  for (Bar b : Bar.class.getEnumConstants()) {
    System.out.println(b.name() + "(\"" + b.toString() + "\")");
    EnumNamer<Bar> en = new EnumNamer<>(b);
    System.out.println(en + "(\"" + en.toString() + "\")");
  }
}

Now you've clarified a few points - this works for me:

// This cast should be OK so long as we KNOW its an enum.
Class<Enum> c = (Class<Enum>)Class.forName(Bar.class.getName());
for (Enum e : c.getEnumConstants()) {
  EnumNamer en = new EnumNamer(e);
  System.out.println(en + "(\"" + en.toString() + "\")");
}
Sign up to request clarification or add additional context in comments.

4 Comments

Ah, yes it does override toString. The question is, how do I get round this without altering the enum?
Get around what? Perhaps don't call toString(). Perhaps call .name() instead.
'.name()' is not available off of 'getEnumConstants()' and as I stated, I cannot do any explicit casting...
.name() is available from getEnumConstants. See my sample code System.out.println(b.name() ....
0

For Bar You can use the following code while retrieving the constants:

List<Bar> list = Arrays.asList(c.getEnumConstants());
List<String> list1 = new ArrayList<String>();
for (Bar b : list)
{
   list1.add(b.name());
}
System.out.println(list1);

1 Comment

@OldCurmudgeon: In that case OP can use .name() method of Enum as I have done in my updated code..

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.