vendredi 25 mars 2016

Android enum reflection

I have been trying for the past few hours to get the following functionality working.

public class OMG {

    String name;
    HashMap<SETTINGS, String> settings;

    public enum SETTINGS {
        Setting1("OMG setting 1"), Setting2("OMG setting 2");
        private String key;

        @Override
        public String toString() {
            return "SETTINGS: " + key;
        }

        SETTINGS(String key){
            this.key = key;
        }
    }

    public OMG(String name, HashMap<SETTINGS, String> settings) {
        this.name = name;
        this.settings = settings;
    }
}

and

public class Test {

    public static void main(String[] args) {
        try {
            Class<?> c = Class.forName("path.to.OMG" + "$" + "SETTINGS");

            System.out.println(Arrays.toString(c.getEnumConstants()));
            HashMap<c,String > values = new HashMap<>();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

I have to create an OMG instance and am been given the path to OMG and the enum NAME. So far I have the code in the Test class. I am able to get all enumValues however I have no idea how to create a HashMap and how to call the constructor with this HashMap to get an instance of the OMG class.

Please help.

Thank yuo





Aucun commentaire:

Enregistrer un commentaire