mardi 25 août 2015

How i can access inner class variable value using reflection in java?

Suppose i have class like below with multiple inner classes :

public class Constants {

public class Class1 {

public static final String IODATA                         = "IOData";
public static final String HUB_INPUTS                     = "HubInputs";

}

public class Class2 {

public static final String CLASS_INPUTS           = "Class Inputs";
public static final String AUXILIARIES_ON_BUTTON  = "Auxiliaries On Button";

}

public class Class3 {

public static final String CLASS_INPUTS           = "Class Inputs";
public static final String AUXILIARIES_ON_BUTTON  = "Auxiliaries On Button";

}

}

I want to access all inner class variable then i can write code like below and i can get the variable name and value:

public static void getClassNameMap() throws Exception {

Constants cons = new Constants();
Constants.Class1 class1 = cons.new Class1();
for (Field fields : class1 .getClass().getFields()) 
{
  System.out.println(fields.get(projectObjectCons).toString() +"--"+fields.getName());
}

}

But some how i get array of String of all inner classes name like :

String []arr = {"Class1","Class2","Class3"};

how can i get all inner class variable and value using these String class name ??? Because i can not make class instance using its string name. Please help me out.





Aucun commentaire:

Enregistrer un commentaire