java - How can i make block of code generic? -


public item marshallitem(string xml) {     // todo auto-generated method stub     xstream xstream = new xstream();      xstream.alias("item", item.class);      return (item) xstream.fromxml(xml); } 

line no 3 :

"item" , item.class hardcoded value.

if have marshall order xml, have write new method or if-else condition achieve.

how can make method more generic can use method multiple class..

something thing:

public <t> t marshallitem(string xml, class<t> clazz) {     xstream xstream = new xstream();     xstream.alias(clazz.getsimplename(), clazz);     return (t) xstream.fromxml(xml); } 

calling method:

item info = marshallitem("yourxml", item.class); 

Comments