/** * MethodName: GetReflection <BR> * Descripción : 解析 Respxml 在通过反射设置对象属性值 * Usuario: liqijing * Fecha: 2015-7-19 下午 12:42:55 * @param CLZZNAME * @param respxml * @return * @throws classnotFoundException * @throws documentException * @throws ilegalargumentception * @throws ilegalAsCess * InstanciationException * @throws SecurityException * @throws nosuchfieldException */public static <t> Object getReflection (string clzzName, string respxml) lanza classnotFoundException, documentException, ilegalArgumentException, ilegalAccessException, 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 (); para (Field f: clz.getDeclaredfields () {) it = el.ElementIterator (); while (it.hasNext ()) {element elt = (element) it.next (); if (f.getName (). Equals (elt.getName ())) {f = clz.getDeClaredField (elt.getName ()); f.setAccessible (verdadero); f.set (o,, elgetfield Elt.gettext ());}}} return o;}Estimados:
在开发过程中有类似的需求通过反射动态设置属性值 , 希望有帮助。也很高兴与大家分享 谢谢。 谢谢。
以上这篇 java 通过反射机制动态设置对象属性值的方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 也希望大家多多支持武林网。