1. 程式人生 > >java反射泛型異常:argument type mismatch解決方案

java反射泛型異常:argument type mismatch解決方案

最近使用java反射泛型的時候出現異常:argument type mismatch,經過debug排查終於解決,廢話不說,直接上程式碼:

javabean及相應的執行函式如下所示:

public class RequestUploadXmlData<T> {

	private Root<T> root;

	/**
	 * @return the root
	 */
	public Root<T> getRoot() {
		return root;
	}

	/**
	 * @param root
	 *            the root to set
	 */
	public void setRoot(Root<T> root) {
		this.root = root;
	}

}

public class Root<T> {

	private T vehispara;
	
	/**
	 * @return the vehispara
	 */
	public T getVehispara() {
		return vehispara;
	}

	/**
	 * @param vehispara the vehispara to set
	 */
	public void setVehispara(T vehispara) {
		this.vehispara = vehispara;
	}
	
	
}

public <T> String upload(String jkid, String WriteXmlDoc, Class<T> obj, Object objDao) throws Exception {

		JSONObject xmlJSONObj = XML.toJSONObject(WriteXmlDoc);
		RequestUploadXmlData<T> requestUploadXmlData = JsonUtil.fromRequest(xmlJSONObj.toString(),
				new TypeToken<RequestUploadXmlData<T>>() {
				}.getType(), "yyyy-MM-dd HH:mm:ss"); 

		String webResult = WebServiceUtil.upload("", jkid, WriteXmlDoc);
		xmlJSONObj = XML.toJSONObject(webResult);
		UploadResult uploadResult = JsonUtil.fromRequest(xmlJSONObj.toString(), UploadResult.class);

		/** 如果上傳成功則插入資料 */
		if (CustomData.CODE_SUCCESS.equals(uploadResult.getRoot().getHead().getCode())) {
			
			Class<? extends Object> objClass = objDao.getClass();
			Method declaredMethod = objClass.getMethod("insert", obj);			
			T vehispara = requestUploadXmlData.getRoot().getVehispara();
			declaredMethod.invoke(objDao, vehispara);
			/*tdlxxDao.insert(requestUploadXmlData.getRoot().getVehispara());*/
			
		}

		return webResult;
	}

執行後報異常:java.lang.IllegalArgumentException: argument type mismatch

解決方案

public <T> String upload(String jkid, String WriteXmlDoc, Class<T> obj, Object objDao) throws Exception {

		JSONObject xmlJSONObj = XML.toJSONObject(WriteXmlDoc);
		RequestUploadXmlData<T> requestUploadXmlData = JsonUtil.fromRequest(xmlJSONObj.toString(),
				new TypeToken<RequestUploadXmlData<T>>() {
				}.getType(), "yyyy-MM-dd HH:mm:ss"); 

		String webResult = WebServiceUtil.upload("", jkid, WriteXmlDoc);
		xmlJSONObj = XML.toJSONObject(webResult);
		UploadResult uploadResult = JsonUtil.fromRequest(xmlJSONObj.toString(), UploadResult.class);

		/** 如果上傳成功則插入資料 */
		if (CustomData.CODE_SUCCESS.equals(uploadResult.getRoot().getHead().getCode())) {
			
			Class<? extends Object> objClass = objDao.getClass();
			Method declaredMethod = objClass.getMethod("insert", obj);
			/** 使用json進行型別轉換,避免getVehispara返回map<String>導致異常:argument type mismatch */
			String json = JsonUtil.toJson(requestUploadXmlData.getRoot().getVehispara());
			T vehispara = JsonUtil.fromRequest(json, obj);
			declaredMethod.invoke(objDao, vehispara);
/*tdlxxDao.insert(requestUploadXmlData.getRoot().getVehispara());*/ } return webResult; }

原因是requestUploadXmlData.getRoot().getVehispara()返回的是map<String>型別,導致型別不匹配,至於為什麼是這種型別,抱歉,目前沒時間研究,我也不知道。