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