Java method for dynamically setting object property values through reflection mechanism _java

Source: Internet
Author: User
/** * methodname:getreflection<br> * Description: Parsing respxml Setting object property value through Reflection * user:liqijing * date:2015-7-19 Midday 12:42:55 * @param clzzname * @param respxml * @return * @throws classnotfoundexception * @throws documentexce ption * @throws illegalargumentexception * @throws illegalaccessexception * @throws instantiationexception * @thro  WS 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:

There are similar requirements in the development process to dynamically set property values through reflection, which is helpful. Also very happy to share with you, thank you.

The above this Java through the reflection mechanism dynamic Setting object attribute value method is small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.