Java學(xué)習(xí)

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

           

          LOVO《Oracle》反射的工具包

          把反射的一些相關(guān)東西弄到一起變成現(xiàn)在這個工具包

          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) {
            //如果參數(shù)類型為空或個數(shù)為0,則調(diào)用無參構(gòu)造方法
            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 {
             // 反射底層方法(調(diào)用方法對象的invoke方法)
             // invoke說明:調(diào)用obj實例對象的方法(相當(dāng)于: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];
             // 根據(jù)屬性計算得到get方法名
             String methodName = getMethodName("get", fieldName);
             try {
              // 定義參數(shù)對象
              Class[] types = new Class[] {};
              // 根據(jù)方法和參數(shù)對象,得到指定方法對象
              Method method = obj.getClass().getMethod(methodName, types);
              // 執(zhí)行方法對象對應(yīng)的方法(注意也要有參數(shù))
              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();
             // 根據(jù)屬性計算得到get方法名
             String methodName = getMethodName("get", fieldName);
             try {
              // 定義參數(shù)對象
              Class[] types = new Class[] {};
              // 根據(jù)方法和參數(shù)對象,得到指定方法對象
              Method method = obj.getClass().getMethod(methodName, types);
              // 執(zhí)行方法對象對應(yīng)的方法(注意也要有參數(shù))
              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++) {
              // 根據(jù)屬性計算得到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++) {
             // 根據(jù)屬性計算得到get方法名
             String methodName = getMethodName("get", fields[i].getName());
             try {
              // 定義參數(shù)對象
              Class[] types = new Class[] {};
              // 根據(jù)方法和參數(shù)對象,得到指定方法對象
              Method method = obj.getClass().getMethod(methodName, types);
              // 執(zhí)行方法對象對應(yīng)的方法(注意也要有參數(shù))
              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)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計

          公告

          本blog已經(jīng)搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新化县| 府谷县| 环江| 东丽区| 鸡西市| 儋州市| 小金县| 陇西县| 巨野县| 渝中区| 泾阳县| 边坝县| 普洱| 高清| 武强县| 泰安市| 鸡西市| 社会| 天祝| 乌恰县| 阿拉尔市| 察雅县| 招远市| 东乌珠穆沁旗| 青浦区| 汶川县| 大田县| 正宁县| 鄄城县| 谢通门县| 上栗县| 区。| 灯塔市| 郸城县| 读书| 营口市| 南乐县| 江川县| 措勤县| 嘉峪关市| 青铜峡市|