首先,我想展示用于反序列化 xml 字符串的核心代码
public XMLClient<T> xmlToClient(String xmlString,T t) throws Exception{
//according to the generic class's name,insert the name after input tag
//to make the xstream to know which class is going to deserialize to
String insertClassName = t.getGenericTypeRecord();
xmlString.replaceAll("<INPUT>", "<INPUT input."+insertClassName+">");
XMLClient<T> xmlClient = new XMLClient<T>();
XStream xs = new XStream(new DomDriver());
xs.processAnnotations(XMLClient.class);
xmlClient = (XMLClient<T>)xs.fromXML(xmlString);
logger.info(xmlString);
return xmlClient;
}
T 是从超基类扩展而来的一个类。就像
T 扩展 baseInputXml
但是当应用程序运行在
xmlClient = (XMLClient<T>)xs.fromXML(xmlString);
会有错误消息告诉我 XMLClient 中没有匹配的属性。虽然该属性在超 T 类中不存在,但在子 T 类中存在
我希望你能明白我的想法,我真的对这些 genericType 和 xstream 转换感到困惑,谢谢