/** * methodname: getreflection<br> * description:解析respxml 在通过反射设置对象属性值 * user: liqijing * date:2015-7-19下午12:42:55 * @param clzzname * @param respxml * @return * @throws classnotfoundexception * @throws documentexception * @throws illegalargumentexception * @throws illegalaccessexception * @throws instantiationexception * @throws securityexception * @throws nosuchfieldexception */ public static <t> object getreflection(string clzzname , string respxml) throws classnotfoundexception, documentexception, illegalargumentexception, illegalaccessexception, instantiationexception, securityexception, nosuchfieldexception{ object o = class.forname(clzzname).newinstance(); class clz = class.forname(clzzname).newinstance().getclass(); document doc = null ; doc = documenthelper.parsetext(respxml); element el = doc.getrootelement(); for (field f : clz.getdeclaredfields()){ iterator it=el.elementiterator(); while(it.hasnext()){ element elt = (element) it.next(); if(f.getname().equals(elt.getname())){ f = clz.getdeclaredfield(elt.getname()); f.setaccessible(true); f.set(o, elt.gettext()); } } } return o; }
dear all:
在开发过程中有类似的需求通过反射动态设置属性值,希望有帮助。也很高兴与大家分享,谢谢。
以上这篇java通过反射机制动态设置对象属性值的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
SpringBoot引用阿里easyexcel,Excel导出返回浏览器下载
HashMap、Hashtable、ConcurrentHashMap三者间的异同
解决RecycleView 中Item包含Edittext时,滑动view复用导致数据错乱的问题
多线程、同步工作原理、死锁案例、Lock接口、线程的生命周期的讲解及实现
网友评论