/** * 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.