当前位置: 移动技术网 > IT编程>开发语言>Java > Java通过反射机制动态设置对象属性值的方法

Java通过反射机制动态设置对象属性值的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论
/**
	 * 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通过反射机制动态设置对象属性值的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网