posts - 3, comments - 15, trackbacks - 0, articles - 26
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          JAVA 轉(zhuǎn)碼

          Posted on 2009-07-02 09:24 morcble的blog 閱讀(1163) 評論(0)  編輯  收藏 所屬分類: Java
          public static synchronized void paseCode(Object obj,String fromEncode,String toEncode){
            HashMap<String,Method> setMethodMap = new HashMap<String,Method>();
            HashMap<String,Method> getMethodMap = new HashMap<String,Method>();
            List<String> propertyList= new ArrayList<String>();
            Method[] methods = obj.getClass().getDeclaredMethods();
            for(Method method:methods){
             String methodName = method.getName();
             if( methodName.indexOf("get")!=-1){
              if(method.getReturnType().equals(String.class)){
               getMethodMap.put(methodName.substring(3),method);
               propertyList.add(methodName.substring(3));
              }
             }
             else if( methodName.indexOf("set")!=-1){
              if(method.getParameterTypes()[0].equals(String.class)){
               setMethodMap.put(methodName.substring(3),method);
              }
             }
            }
            String tempvalue = null;
            String propertyName = null;
            for(int i = 0;i <propertyList.size();i++){
             try {
              propertyName = propertyList.get(i);    
              tempvalue = (String) getMethodMap.get(propertyName).invoke(obj, new Object[0]);
              if(tempvalue!=null&&!tempvalue.equals(""))
               tempvalue = new String(tempvalue.getBytes(fromEncode),toEncode);
              setMethodMap.get(propertyName).invoke(obj, tempvalue);
             } catch (IllegalArgumentException e) {
              e.printStackTrace();
             } catch (IllegalAccessException e) {
              e.printStackTrace();
             } catch (InvocationTargetException e) {
              e.printStackTrace();
             } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
             }
            }
            
           }



          test eg  paseCode(new Object(),"GB2312","latin1")
          主站蜘蛛池模板: 洛浦县| 垣曲县| 宝鸡市| 武乡县| 靖边县| 枞阳县| 勐海县| 宿迁市| 台南县| 南昌市| 原阳县| 信宜市| 淳化县| 阿图什市| 南京市| 武乡县| 买车| 子洲县| 城口县| 怀集县| 漳浦县| 台安县| 永登县| 监利县| 万年县| 承德市| 林周县| 鹰潭市| 忻州市| 衡东县| 彩票| 新邵县| 岐山县| 辽宁省| 鹿邑县| 黑水县| 柞水县| 塔城市| 油尖旺区| 扶余县| 丽水市|