首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

透过反射生成RequestParams

2013-06-26 
通过反射生成RequestParamspublic static RequestParams getRequestParamsFromObject(Object obj) {Reques

通过反射生成RequestParams

public static RequestParams getRequestParamsFromObject(Object obj) {RequestParams params = new RequestParams();Class classType = obj.getClass();Field[] fields = classType.getDeclaredFields();if (fields != null) {int length = fields.length;for (int i = 0; i < length; i++) {Field field = fields[i];String fieldName = field.getName();String getMethodName = "get"+ fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1);try {Method getMethod = classType.getMethod(getMethodName,new Class[] {});Object value = getMethod.invoke(obj, new Object[] {});if (value instanceof File) {try {params.put(fieldName, (File) value);} catch (FileNotFoundException e) {e.printStackTrace();}} else {params.put(fieldName,value != null ? String.valueOf(value): (String) null);}} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}}return params;}

热点排行