samedi 28 mai 2016

reflection - Get enum that is inside a class

I have a class with a enum in it and a constructor, that is using it as parameter.

public class Example {
  public enum ExampleEnum {WORK,IDLE};
  public Example(ExampleEnum e, String s){...}
  // Class body
}

But if I get enum value from it and pass it as argument to contstructor, I get java.lang.NoSuchMethodException

This is the code that I am using:

Object enumVal = Class.forName("Example$ExampleEnum").getField("WORK").get(null);
Object instance = Class.forName("Example").getConstructor(
        enumVal.getClass(),
        String.class).newInstance(
            enumVal,
            "test"
);

(The class exists)





Aucun commentaire:

Enregistrer un commentaire