Java反射工具類,java工具類

來源:互聯網
上載者:User

Java反射工具類,java工具類

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 {    /**         * 得到某個對象的公用屬性         *         * @param owner, fieldName         * @return 該屬性對象         * @throws Exception         *         */         public Object getProperty(Object owner, String fieldName) throws Exception {              Class ownerClass = owner.getClass();                   Field field = ownerClass.getField(fieldName);                   Object property = field.get(owner);                     return property;           }                /**         * 得到某類的靜態公用屬性         *         * @param className   類名         * @param fieldName   屬性名稱         * @return 該屬性對象         * @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;           }                     /**         * 執行某對象方法         *         * @param owner         *            對象         * @param methodName         *            方法名         * @param args         *            參數         * @return 方法傳回值         * @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++) {                  argsClass[i] = args[i].getClass();              }                  Method method = ownerClass.getMethod(methodName, argsClass);                   return method.invoke(owner, args);          }            /**         * 執行某類的靜態方法         *         * @param className         *            類名         * @param methodName         *            方法名         * @param args         *            參數數組         * @return 執行方法返回的結果         * @throws Exception         */         public 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 method = ownerClass.getMethod(methodName, argsClass);              return method.invoke(null, args);          }           /**         * 建立執行個體         * @param className  類名      * @param args    建構函式的參數       * 如果無構造參數,args 填寫為 null      * @return 建立的執行個體         * @throws Exception         */         public Object newInstance(String className, Object[] args,Class[] argsType) throws NoSuchMethodException, SecurityException, ClassNotFoundException,    InstantiationException, IllegalAccessException,    IllegalArgumentException, 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);             }      }           /**         * 是不是某個類的執行個體         * @param obj 執行個體         * @param cls 類         * @return 如果 obj 是此類的執行個體,則返回 true         */         public boolean isInstance(Object obj, Class cls) {              return cls.isInstance(obj);          }           /**         * 得到數組中的某個元素         * @param array 數組         * @param index 索引         * @return 返回指定數組對象中索引組件的值         */         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;   }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.