本文主要是介绍Java 反射小用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//一条记录的 n 个字段 , 都写入对象Document中Class question_class=question.getClass();for (int j = 0; j < newColumn.size(); j++) {net.sf.json.JSONObject obj = newColumn.getJSONObject(j);name=obj.getString("name");val=obj.getString("value");try{PropertyDescriptor pd = new PropertyDescriptor(name_camel, question_class);Method setMethod = pd.getWriteMethod();if(pd.getPropertyType().equals(Integer.class)){if ("".equals(val) || val == null) {setMethod.invoke(question, Integer.valueOf(0));}else{setMethod.invoke(question, Integer.valueOf(val));} }else if(pd.getPropertyType().equals(Date.class)){//处理时间类型if ("".equals(val) || val == null) {setMethod.invoke(question, new Date());}else{setMethod.invoke(question, DateUtil.stringToDate(val));}}else if(pd.getPropertyType().equals(List.class)){//处理List类型}else{//处理String类型setMethod.invoke(question, val);}}catch(IntrospectionException e){logger1.info("PropertyDescriptor error", e);}}
这篇关于Java 反射小用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!