Java Reflection Tool Class

Source: Internet
Author: User

Mport Java.lang.reflect.Array;       Import Java.lang.reflect.Constructor;       Import Java.lang.reflect.Field;         Import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.method;public class Reflection {/**         * Get the public properties of an object * * @param owner, FieldName * @return The Property object * @throws Exception * */Public Object GetProperty (object owner, String fieldName) throws Exception {Class OWNERCL                   The Owner.getclass ();                   Field field = Ownerclass.getfield (FieldName);                     Object property = Field.get (owner);           return property;         }/** * Get static public properties for a class * * @param className class name * @param fieldName property name * @return The Property object * @throws Exception */public Object Getstaticproperty (String className, Strin G FieldName) throws Exception {Class Ownerclass = CLass.forname (ClassName);                   Field field = Ownerclass.getfield (FieldName);                   Object property = Field.get (Ownerclass);           return property; /** * Execute an object method * * @param owner * Object * @param meth Odname * Method Name * @param args * parameter * @return method return value * @throws Ex Ception */Public Object InvokeMethod (object owner, String methodName, object[] args) thr                   oWS Exception {Class ownerclass = Owner.getclass ();                   class[] Argsclass = new Class[args.length];              for (int i = 0, j = args.length; I < J; i++) {Argsclass[i] = Args[i].getclass ();                   } method = Ownerclass.getmethod (MethodName, Argsclass);          Return Method.invoke (owner, args); }/** * Performs a class ofstatic method * * @param className * Class name * @param methodName * Method Name * @param args * parameter array * @return The results returned by the execution method * @throws Exception */PU              Blic Object Invokestaticmethod (String className, String methodName, object[] args) throws Exception {              Class Ownerclass = Class.forName (className);              class[] Argsclass = new Class[args.length];              for (int i = 0, j = args.length; I < J; i++) {Argsclass[i] = Args[i].getclass ();              } method = Ownerclass.getmethod (MethodName, Argsclass);          return Method.invoke (null, args);      }/** * New instance * @param className class name * @param args constructor parameter * If no construction parameter, args fills in null * @return New instance * @throws Exception */Public Object newinstance (String className, object[] A Rgs,class[] ArgstyPE) throws Nosuchmethodexception, SecurityException, ClassNotFoundException, Instantiationexception, Illegalaccessexception, IllegalArgumentException, invocationtargetexception {Class Newoneclass = Class.f              ORName (ClassName);         if (args = = null) {return newoneclass.newinstance ();       }else{//class[] Argsclass = new Class[args.length];        for (int i = 0, j = args.length; I < J; i++) {//Argsclass[i] = Args[i].getclass ();        }////Constructor cons = Newoneclass.getconstructor (Argsclass);           Constructor cons = Newoneclass.getconstructor (Argstype);             Return cons.newinstance (args); }}/** * is not an instance of a class * @param obj instance * @param CLS class * @return If obj is an instance of this class,          Returns True */public boolean isinstance (Object obj, Class cls) {return cls.isinstance (obj);     }      /** * Gets an element in the array * @param array array * @param index Index * @return Returns the value of the index component in the specified Array object          */Public object Getbyarray (object array, int index) {return array.get (Array,index);    Class<?> getclasslistbypackage (String ppackage) {Package _package = Package.getpackage (ppackage);      Class<?> _list =_package.getclass ();   return _list; }}

Java Reflection Tool Class

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.