Java Reflection Tool Class encapsulation

Source: Internet
Author: User

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&gt ;  * * @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

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.