In primo luogo, ho un oggetto del genere:Impostare il valore per il campo oggetto di riflessione in java
public class Entity {
public int data1;
public String data2;
public float data3;
public SubEntity data4;
}
public class SubEntity{
public int id;
public SubEntity(int id){
tis.id = id;
}
}
E un HashMap:
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("data1", 1);
map.put("data2", "name");
map.put("data3", 1.7);
map.put("data4", new SubEntity(11));
Ho bisogno il modo giusto per impostare il valore per tutti i campi di entità dinamica da utilizzare riflette da hashmap. Qualcosa di simile a questo:
for (Field f : entity.getClass().getDeclaredFields()) {
String name = f.getName();
Object obj = map.get("name");
// Need set value field base on its name and type.
}
Come posso raggiungere questo obiettivo? Supponiamo che abbia molte sottoclassi nell'entità.