xml - Approaches for populating Java objects from maps -


i'm dealing recurring pattern @ work.

public class sapexample {    public static void main(string[] args)   {     // read sap data..      // table     list<map<string, object>> objects = gettable("et_widget");      (map<string, object> entry : objects)     {       object value = entry.get("wdgt_id");       string id = (value != null) ? string.valueof(value) : ""; // or null        value = entry.get("wdgt_name");       string name = (value != null) ? string.valueof(value) : ""; // or null        value = entry.get("wdgt_desc");       string description = (value != null) ? string.valueof(value) : ""; // or null        // create object data       //widget obj = new widget(id, name, description);     }   }    private static list<map<string, object>> gettable(string string)   {     return collections.emptylist();   } } 

easy example no relations between objects. more complex scenarios, class has member of class b occur , solved use of foreign keys.

tables returned database handled list<map<string, object>>, map<string, object> depicts row in given table maps column names values of type object, internally of type string or date.

the map contains data needed initialize objects of type, depends on current table. can't change that.

programmers directly or indirectly access data in way showed above , create instances of desired objects.


i find verbose , unesthetic way of programming.

is possible utilize jaxb or other technologies solve problem in declarative way (e.g. use of xml)? if possible use jaxb, have do? suppose have override classes.

jaxb seems inappropriate case because meant handle xml , problem doesn't involve xml.

as alternative use annotations , reflection:

create own annotation:

@target(elementtype.field) @retention(retentionpolicy.runtime) @documented public @interface mapped {     string value() default "";     string defaultvalue() default ""; } 

annotate widget/pojo:

public class widget {     @mapped(key = "wdgt_id", defaultvalue = "")     private string id;     @mapped(key = "wdgt_name", defaultvalue = "")     private string name;     @mapped(key = "wdgt_desc", defaultvalue = "")     private string description;      [constructors...]     [getters&setters...] } 

implement "factory/builder" method can create pojo:

public t getobject(map<string, object> source, class<t> clazz) {     [some java introspection code] } 

your code become:

public class sapexample {    public static void main(string[] args)   {     // read sap data..      // table     list<map<string, object>> objects = gettable("et_widget");      (map<string, object> entry : objects) {         widget obj = getobject(entry , widget.class);     }   }    private static list<map<string, object>> gettable(string string)   {     return collections.emptylist();   } } 

the difficult part here "factory" method. not provide code here because requires lot of time produce , implementation can depend on context. hope have understood idea.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -