lundi 12 décembre 2022

How to get an enum of static class members in Java?

I have a Java class (which I do not control and cannot modify) which has a large number of static String members. How can I have these as an enum (for convenience in coding)?

E.g. I have:

public class ClassWithStaticMembers {
  public static String ONE = "one";
  public static String TWO = "dos";
}

...and I want to derive the following from it:

public enum NUMBERS {
  ONE,
  TWO
}

Is this possible, with reflection or otherwise?

(Assume I don't know Java well, and that I have 100+ values, so repeating each is painful and not desirable (in case more values get added later). Also I don't need the actual values from the class ("one" and "dos" above)).





Aucun commentaire:

Enregistrer un commentaire