Encapsulates common reflection-related methods
public class Reflectutil {/** * gets class through Classpath * * @author lhy <br> * Description <br> * LASTMO Dified 2014-5-9 Content <br> */public static class<?> getclass (String PackagePath) throws ClassNotFoundException {return class.forname (PackagePath);} /** * Get Activity class * * @author lhy <br> * Description packageroot: module with path <br> * Activitypa Th:activity full path or module activity path lastmodified 2014-5-9 * Content <br> */public static class<?> GETMODELC Lass (String packageroot, String activitypath) throws ClassNotFoundException {if (Activitypath.indexof (".") = = 0) { Activitypath = Packageroot + Activitypath;} Return GetClass (Activitypath);} /** * * @author lhy <br> * Description analog Get method <br> * lastmodified 2013-12-9 Content <br> ; * * @param obj Action Object <br> * * @param att action Properties <br> * @param <T> */public static <T> T Getter (Object obj, String att) {T t =Null Field field;try {field = Obj.getclass (). Getdeclaredfield (ATT); field.setaccessible (true); t = (t) field.get (obj);} catch (Nosuchfieldexception e) {E.printstacktrace ();} catch (IllegalArgumentException e) {e.printstacktrace ();} catch (Illegalaccessexception e) {e.printstacktrace ();} return t;} /** * * @author lhy <br> * Description by property name, add properties <br> * lastmodified 2014-2-13 Content <br > */public static void Setter (object root, String name, object value) {field field;try {field = Root.getclass (). getdecl Aredfield (name); Field.setaccessible (true); Field.set (root, value);} catch (Nosuchfieldexception e) {e.printstacktrace ();} catch (IllegalArgumentException e) {e.printstacktrace ();} catch ( Illegalaccessexception e) {e.printstacktrace ();}} /** * * @author lhy <br> * Description get entity attribute Name list <br> * lastmodified 2013-12-9 Content <br > */public static list<string> Getfieldlist (Object object) {list<string> fieldhashlist =New Arraylist<string> (); Class<?> cls = Object.getclass (); field[] FieldList = Cls.getdeclaredfields (); for (int i = 0; i < fieldlist.length; i++) {Field fld = FIELDLIST[I];FIELDH Ashlist.add (Fld.getname ());} return fieldhashlist;} /** * * @author lhy <br> * Description get entity property name and category key value pairs <br> * lastmodified 2013-12-9 Content &L t;br> */public static hashmap<string, class<?>> Getfieldmap (Object object) {hashmap<string, Class <?>> Fieldhashmap = new hashmap<string, class<?>> (); Class<?> cls = Object.getclass (); field[] FieldList = Cls.getdeclaredfields (); for (int i = 0; i < fieldlist.length; i++) {Field fld = FIELDLIST[I];FIELDH Ashmap.put (Fld.getname (), Fld.gettype ());} return fieldhashmap;} /** * * @author lhy <br> * Description get a new instance <br> * lastmodified 2013-12-10 Content <br& Gt */public static Object Getnewbean (Object object) {try {object = Object.getclass (). newinstance ();} CAtch (instantiationexception e) {e.printstacktrace ();} catch (Illegalaccessexception e) {e.printstacktrace ();} return object;}}
Java Reflection Tool Class encapsulation