Copy Code code as follows:
Package com.et59.cus.domain.dao.ex;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Modifier;
Import Org.apache.log4j.Logger;
/**
*
* <p>Title:ReflectUtil.java</p>
* <p>description: Reflection </p>
* <p>company: Drip Studio </p>
* @version 2.0
*
*/
public class Reflectutil {
Private static final Logger log = Logger.getlogger (Reflectutil.class);
@SuppressWarnings ({"Rawtypes", "Unchecked"})
public static void SetFieldValue (Object target, String fname, Class ftype,
Object fvalue) {
If target = null
|| FName = null
|| "". Equals (fname)
|| (Fvalue!= null &&!ftype.isassignablefrom (Fvalue.getclass ()))) {
Return
}
Class clazz = Target.getclass ();
try {
Method method = Clazz.getdeclaredmethod ("Set"
+ character.touppercase (Fname.charat (0))
+ fname.substring (1), ftype);
if (! Modifier.ispublic (Method.getmodifiers ()) {
Method.setaccessible (TRUE);
}
Method.invoke (target, fvalue);
catch (Exception me) {
if (log.isdebugenabled ()) {
Log.debug ("Me exception--------:" +me);
}
try {
Field field = Clazz.getdeclaredfield (fname);
if (! Modifier.ispublic (Field.getmodifiers ()) {
Field.setaccessible (TRUE);
}
Field.set (target, fvalue);
catch (Exception Fe) {
if (log.isdebugenabled ()) {
Log.debug ("Fe----------->" +fe);
}
}
}
}
}