Java學習

          java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

           

          LOVO《Oracle》反射的工具包

          把反射的一些相關東西弄到一起變成現在這個工具包

          package com.lovo.util;

          import java.lang.reflect.Constructor;
          import java.lang.reflect.Field;
          import java.lang.reflect.InvocationTargetException;
          import java.lang.reflect.Method;
          import java.text.ParseException;
          import java.text.SimpleDateFormat;
          import java.util.ArrayList;
          import java.util.List;

          public class ReflectUtil {

           
           private Class myClass = null;
           
           
           Object obj = null;

           private String className;

           
           public ReflectUtil() {
           }

           
           public ReflectUtil(String className) {
            try {
             this.myClass = Class.forName(className);
            } catch (ClassNotFoundException e) {
             e.printStackTrace();
            }
           }

           
           public Object createObject() {
            try {
             obj = myClass.newInstance();
            } catch (InstantiationException e) {
             e.printStackTrace();
            } catch (IllegalAccessException e) {
             e.printStackTrace();
            }
            return obj;
           }
           
           
           public Object createObject(Class[] paramTypes,Object[] paramValues) {
            //如果參數類型為空或個數為0,則調用無參構造方法
            if(paramTypes==null ||paramTypes.length==0){
             obj = this.createObject();  
            }else{
             Constructor myConstructor = null;
             try {
              myConstructor = this.myClass.getConstructor(paramTypes);
              obj = (Object) myConstructor.newInstance(paramValues);
             } catch (Exception e) {
              e.printStackTrace();
             }
            }
            return obj;
           }

           
           private Method getMethod(String methodname) {
            Method method = null;
            Method[] methods = myClass.getMethods();
            for (int i = 0; i < methods.length; i++) {
             if (methods[i].getName().equals(methodname)) {
              method = methods[i];
              break;
             }
            }
            return method;
           }

           
           public Object methodRun(String methodName, Object[] args) {
            Object result = methodInvoke(getMethod(methodName), args);
            ;
            return result;
           }

           
           private Object methodInvoke(Method method, Object[] args) {
            Object result = null;
            try {
             // 反射底層方法(調用方法對象的invoke方法)
             // invoke說明:調用obj實例對象的方法(相當于:obj.xXX();
             if(args==null || args.length==0){
              result = method.invoke(obj, new Object[]{null});
             }else{
              result = method.invoke(obj, args);
             }
             
            } catch (IllegalArgumentException e) {
             e.printStackTrace();
            } catch (IllegalAccessException e) {
             e.printStackTrace();
            } catch (InvocationTargetException e) {
             e.printStackTrace();
            }
            return result;
           }

           
           public List<String> getMethodNameList() {
            List<String> list = new ArrayList<String>();
            if (obj != null) {
             Class cls = obj.getClass();
             Method[] method = cls.getMethods();
             for (int i = 0; i < method.length; i++) {
              list.add(method[i].getName());
             }
            }
            return list;
           }

           public String[] getObjFieldsName() {
            Field[] fields = obj.getClass().getDeclaredFields();
            String[] names = new String[fields.length];
            for (int i = 0; i < fields.length; i++) {
             names[i] = fields[i].getName();
            }
            return names;
           }

           
           public Object[] getObjAttValues(String[] attList) {
            // 獲取類的屬性列表
            Object[] data = new Object[attList.length];

            for (int k = 0; k < attList.length; k++) {
             // 指定位置屬性
             String fieldName = attList[k];
             // 根據屬性計算得到get方法名
             String methodName = getMethodName("get", fieldName);
             try {
              // 定義參數對象
              Class[] types = new Class[] {};
              // 根據方法和參數對象,得到指定方法對象
              Method method = obj.getClass().getMethod(methodName, types);
              // 執行方法對象對應的方法(注意也要有參數)
              Object result = method.invoke(obj, new Object[0]);
              data[k] = result;

             } catch (Exception e) {
              e.printStackTrace();
             }
            }
            return data;
           }

           
           public Object[] getObjAttValues(int[] fieldOrder) {

            // 獲取類的屬性列表
            Field[] fields = obj.getClass().getDeclaredFields();
            Object[] data = new Object[fieldOrder.length];

            for (int k = 0; k < fieldOrder.length; k++) {
             // 指定位置屬性
             String fieldName = fields[fieldOrder[k]].getName();
             // 根據屬性計算得到get方法名
             String methodName = getMethodName("get", fieldName);
             try {
              // 定義參數對象
              Class[] types = new Class[] {};
              // 根據方法和參數對象,得到指定方法對象
              Method method = obj.getClass().getMethod(methodName, types);
              // 執行方法對象對應的方法(注意也要有參數)
              Object result = method.invoke(obj, new Object[0]);
              data[k] = result;

             } catch (Exception e) {
              e.printStackTrace();
             }
            }
            return data;
           }

           
           public void copyValuesFromArray(Object[] values) {

            // 獲取類的屬性列表
            Field[] fields = obj.getClass().getDeclaredFields();

            try {
             for (int i = 0; i < fields.length; i++) {
              // 根據屬性計算得到get方法名
              String methodName = getMethodName("set", fields[i].getName());
              Method method = obj.getClass().getDeclaredMethod(methodName,
                new Class[] { fields[i].getType() });
              String name = fields[i].getType().getName();

              if (values[i] != null) {
               if(name.equals("java.lang.Integer")){
                Integer tmpInteger = null;
                try{
                 tmpInteger = Integer.valueOf(values[i].toString());
                }catch(Exception e){
                 tmpInteger = null;
                }
                method.invoke(obj,new Object[]{tmpInteger});
               }else if(name.equals("java.lang.Double")){
                method.invoke(obj,new Object[]{Double.valueOf(values[i].toString())});
               }else if (name.equals("java.sql.Date")) {
                java.sql.Date dt = StringToDate(values[i].toString());
                method.invoke(obj, new Object[] { dt });
               } else {
                method.invoke(obj, new Object[] { values[i] });
               }

              }
             }
            } catch (Exception e) {
             e.printStackTrace();
            }
           }

           
           public String objToString() {
            StringBuffer sb = new StringBuffer();

            // 獲取類的屬性列表
            Field[] fields = obj.getClass().getDeclaredFields();

            for (int i = 0; i < fields.length; i++) {
             // 根據屬性計算得到get方法名
             String methodName = getMethodName("get", fields[i].getName());
             try {
              // 定義參數對象
              Class[] types = new Class[] {};
              // 根據方法和參數對象,得到指定方法對象
              Method method = obj.getClass().getMethod(methodName, types);
              // 執行方法對象對應的方法(注意也要有參數)
              Object result = method.invoke(obj, new Object[0]);

              sb.append(fields[i].getName() + "=" + result + ""t");

             } catch (Exception e) {
              e.printStackTrace();
             }
            }

            return sb.toString();
           }

           public Object getObj() {
            return obj;
           }

           public void setObj(Object obj) {
            this.obj = obj;
           }

           public String getClassName() {
            return className;
           }

           public void setClassName(String className) {
            this.className = className;
           }


           
           private static String getMethodName(String prefix, String fieldName) {
            return new StringBuffer(prefix).append(
              fieldName.substring(0, 1).toUpperCase()).append(
              fieldName.substring(1)).toString();
           }

           
           private static java.sql.Date StringToDate(String dtStr) {
            if (dtStr == null || dtStr.trim().length() == 0) {
             return null;
            } else {
             java.util.Date date = null;
             try {
              // 注意是大寫的MM
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
              date = sdf.parse(dtStr);
              return new java.sql.Date(date.getTime());
             } catch (ParseException ex) {
              ex.printStackTrace();
              return null;
             }
            }
           }

          }

          posted on 2009-07-16 17:33 找個美女做老婆 閱讀(320) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          公告

          本blog已經搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 莒南县| 安平县| 固镇县| 曲靖市| 郯城县| 广州市| 德格县| 英山县| 宝兴县| 米泉市| 三门峡市| 虹口区| 沧州市| 临猗县| 永年县| 宁波市| 浏阳市| 五常市| 南乐县| 漳州市| 繁峙县| 萨迦县| 叶城县| 瑞安市| 汶上县| 乌拉特前旗| 华宁县| 犍为县| 诏安县| 高要市| 临颍县| 景东| 滨州市| 通化县| 海原县| 咸阳市| 建湖县| 西吉县| 香河县| 清苑县| 灵宝市|