java - How to Make Object.class Type Generic -
i create utility method takes 2 objects parameters , marshalls xml them. below code works if use actual object type parameter, how make generic? below wont compile since can't resolve object type. ideas?
public static string getxml(object from){ stringwriter xml = new stringwriter(); try { jaxbcontext.newinstance(from.class).createmarshaller().marshal(from, xml); } catch (exception e) { e.printstacktrace(); } return xml.tostring(); }
you can class
instance of object so
jaxbcontext.newinstance(from.getclass()) //...
this explained in ojbect#getclass()
javadoc.
returns runtime class of object.
note there aren't generics directly involved in code snippet.
Comments
Post a Comment