lundi 3 février 2020

Create POJO and populate it from Map

At runtime I want to create a POJO with attributes that are same as keys of the map and populate it with the values of the map.
I don't know the content of the map.
Example-

Map<String,String> map = new HashMap<>();
map.add("attr1", obj1);
map.add("attr2", obj2);
...

From this map I want to create a POJO-

class POJO
{
    String attr1;

    public void setAttr1(String attr1) {
        this.attr1 = attr1;
    }

    public String getAttr1() {
        return attr1;
    }

    String attr2;

    public void setAttr2(String attr2) {
        this.attr2 = attr2;
    }

    public String getAttr2() {
        return attr2;
    }

    .....
}

and populate it as well. All of this should happen at runtime. Something like-

Object object = getPopulatedPOJO(map)

or

Class type = getPOJOType(map);
Object object = type.newInstance();
object = getPopulatedPOJO(map)




Aucun commentaire:

Enregistrer un commentaire