Java reflection tool and java Tool

Source: Internet
Author: User

Java reflection tool and java Tool

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 attribute of an object ** @ param owner, fieldName * @ return this property Object * @ throws Exception **/public Object getProperty (Object owner, String fieldName) throws Exception {Class ownerClass = owner. ge TClass (); Field field = ownerClass. getField (fieldName); Object property = field. get (owner); return property ;} /*** get a static public property ** @ param className class name * @ param fieldName property name * @ return this property Object * @ throws Exception */public Object getStaticProperty (String className, string 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 methodName * method name * @ param args * parameter * @ return method return value * @ throws Exception */public object invokeMethod (Object owner, string methodName, Object [] args) throws Exception {Class ownerClass = owner. getClass (); Class [] argsClass = new Class [args. length]; for (int I = 0, j = args. length; I <j; I ++) {argsC Lass [I] = args [I]. getClass ();} Method method = ownerClass. getMethod (methodName, argsClass); return method. invoke (owner, args );} /*** execute a static method ** @ param className * class name * @ param methodName * method name * @ param args * parameter array * @ return result returned by the execution Method *@ throws Exception */public Object invokeStaticMethod (String className, string methodName, Object [] args) throws Exception {Class ownerClass = Class. forName (c LassName); Class [] argsClass = new Class [args. length]; for (int I = 0, j = args. length; I <j; I ++) {argsClass [I] = args [I]. getClass ();} Method method = ownerClass. getMethod (methodName, argsClass); return method. invoke (null, args);}/*** create instance * @ param className class name * @ param args constructor parameter * if no constructor parameter exists, args is null * @ return the newly created instance * @ throws Exception */public Object newInstance (String className, Ob Ject [] args, Class [] argsType) throws NoSuchMethodException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, expiration, InvocationTargetException {Class newoneClass = Class. forName (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 it an instance of a certain 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);}/*** get an element in the array * @ param arr Ay array * @ param 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);} public Class <?> GetClassListByPackage (String pPackage) {Package _ Package = Package. getPackage (pPackage); Class <?> _ List = _ Package. getClass (); return _ List ;}}

Related Article

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.