0

I have a project which uses an enum to be used as a parameter for a class which signature is like this:

 public class MyClass<E extends Enum<E>> extends ExtendedClass

The thing is, I don't want to define an enum and do the same programming again for each new enum.

I was thinking of ByteBuddy to generate enums at runtime. but I'm not finding a neat way to do this and the resources are not so much .

edit:

To be more specific, what I have in my code:

private enum MyEnum{
    FOO, BAR;
}

MyClass<MyEnum> obs = new MyClass<MyEnum>() 

It does not work when I use the Enum generated by ByteBuddy; It's not considered as a type in this case (compilation error).

Is there some trick to use or that's it what can be done in run-time?

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
CiCi
  • 47
  • 1
  • 5
  • 1
    I didn't know about ByteBuddy, but looking at it looks like a lot of trouble and I don't really see the benefit. Do you really have to create so many enums? – Ray O'Kalahjan Aug 17 '16 at 09:36
  • Byte Buddy creates types at runtime. You cannot compile against such types. You can still use Byte Buddy to create types before compilation and compile against them. You would need to extend your build for that. – Rafael Winterhalter Aug 17 '16 at 21:37
  • Welcome to Stack Overflow! Can you please have a better title and more detailed information in the content with your effort to solve the problem? – Enamul Hassan Aug 20 '16 at 03:27

3 Answers3

2

Enum types are compiled to class with static fields, so

enum MyEnum {
  FOO
}

is compiled to

class MyEnum extends java.lang.Enum<MyEnum> {
  public static final Sample FOO;
  // synthetic
  private static final Sample[] $VALUES;

  static {
    FOO = new MyEnum("FOO", 0);
    $VALUES = new MyEnum[] {FOO};
  }

  private MyEnum(String name, int ordinal) {
    super(name, ordinal);
  }

  // and other helpers
  public static MyEnum[] values() { /* $VALUES */ }
  public static MyEnum valueOf(String name) { /* $VALUES lookup */ }
}

(that's why enum cannot extend another class only implement interfaces). So you need to generate this kind of class to emulate enum.

Simeon Leyzerzon
  • 18,658
  • 9
  • 54
  • 82
vsminkov
  • 10,912
  • 2
  • 38
  • 50
2

You can create enums with Byte Buddy easily:

Class<? extends Enum<?>> type = new ByteBuddy()
  .makeEnumeration("FOO", "BAR")
  // define methods/fields if required.
  .make()
  .load(MyClass.class.getClassLoader())
  .getLoaded();

This gives you an enum type. You can read the constants by:

Enum<?> foo = Enum.valueOf(type, "FOO");
Enum<?> bar = Enum.valueOf(type, "BAR");

Disclaimer: your question does however not picture enough of your use case to allow for an assertion if this is the best approach for solving your problem.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • Thank you.. that's what I did actually.. I edited the question to be more specific, maybe you can understand me better.. – CiCi Aug 17 '16 at 21:01
-2

You can retrieve an Enumeration at runtime from any given Collection:

java.util.Collections.enumeration(set)

See this SO answer.

Community
  • 1
  • 1
Markus Mitterauer
  • 1,560
  • 1
  • 14
  • 28