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 ;}}