zoukankan      html  css  js  c++  java
  • 测试一篇文章

    这是我的一篇用于测试用的文章,

    我也不知道怎么去写

      1 public class BeanUtils {
      2     
      3     
      4     /**
      5      * 比较两个对象是否相等
      6      * @param newFiled 封装修改信息
      7      * @param logMap 封装日志
      8      * @param oldObj 旧的对象
      9      * @param newObj 新的对象
     10      */
     11     public static void differBean(Map<String, Object> newFiled, Map<String, LogContent> logMap, String parName, Object oldObj, Object newObj){
     12         //不需要比较的值有 createTime/lastUpdateTime/status/resourceId
     13         try {
     14             if(oldObj.getClass()==newObj.getClass()){//只有两个对象都是同一类型的才有可比性
     15                 Class clazz = oldObj.getClass();
     16                 PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,Object.class).getPropertyDescriptors();
     17                 for(PropertyDescriptor pd : pds){
     18                     
     19                     Field f = clazz.getDeclaredField(pd.getName());
     20                     f.setAccessible(true); 
     21                     if(f.getAnnotation(Transient.class) != null || f.getAnnotation(Id.class) != null || f.getAnnotation(DNU.class)!=null){
     22                         continue;
     23                     }
     24                     
     25                     String name = null;
     26                     if(parName != null){
     27                         name = parName+"."+pd.getName();
     28                     }else{
     29                         name = pd.getName();
     30                     }
     31                     
     32                     Object oldVal = pd.getReadMethod().invoke(oldObj, null);
     33                     Object newVal = pd.getReadMethod().invoke(newObj, null);
     34                     
     35                     if(oldVal == null && newVal == null){
     36                         continue;
     37                     }else if(oldVal != null && oldVal.equals(newVal)){
     38                         continue;
     39                     }else if(newVal != null && newVal.equals(oldVal)){
     40                         continue;
     41                     }
     42                     
     43                     //1. 基本类型
     44                     if(isBaseData(pd.getPropertyType())){
     45                         //不更新
     46                         if("createTime".equals(name) || "status".equals(name)){
     47                             continue;
     48                         }
     49                         //更新
     50                         if(newVal == null){
     51                             newFiled.put(name+"|unset", newVal);
     52                         }else{
     53                             newFiled.put(name, newVal);
     54                         }
     55                         //日志
     56                         if(!"lastUpdateTime".equals(name)){
     57                             logMap.put(name, new LogContent(oldVal+"", newVal+""));
     58                             //logMap.put(name, "<p><strong>" + name + ":</strong><small><del>" + oldVal + "</del><span class='text-danger'>" + newVal+"</span></small></p>");
     59                             //logMap.put(name, "字段名称:" + name + "旧值:" + oldVal + "新值:" + newVal);
     60                         }
     61                             
     62                     //2. 接口类型
     63                     }else if(pd.getPropertyType().isInterface()){
     64                         //List
     65                         if(List.class.equals(pd.getPropertyType())){
     66                             List<Object> oldList = (List<Object>) oldVal;
     67                             List<Object> newList = (List<Object>) newVal;
     68                             //更新
     69                             newFiled.put(name, newList);
     70                             
     71                             if(oldList != null && oldList.size()>0 && newList != null && newList.size()>0){
     72                                 Object t = oldList.get(0);
     73                                 if(isBaseData(t.getClass())){
     74                                     //比较两个List
     75                                     compareList(oldList, newList, name, logMap);
     76                                 }else{
     77                                     int max = oldList.size();
     78                                     max = max < newList.size()? newList.size():oldList.size();
     79                                     for(int i = 0;i<max;i++){
     80                                         Object oldListObj = null;
     81                                         try {
     82                                             oldListObj = oldList.get(i);
     83                                         } catch (Exception e) {
     84                                             oldListObj = "";
     85                                         }
     86                                         Object newListObj = null;
     87                                         try {
     88                                             newListObj = newList.get(i);
     89                                         } catch (Exception e) {
     90                                             newListObj = "";
     91                                         }
     92                                         if(!"".equals(oldListObj) && !"".equals(newListObj)){
     93                                             differObject(logMap, name+"["+i+"]", oldListObj, newListObj);
     94                                         }else{
     95                                             logMap.put(name+"["+i+"]", new LogContent(oldListObj+"", newListObj+""));
     96                                         }
     97                                     }
     98                                 }
     99                                 
    100                             }else{
    101                                 logMap.put(name, new LogContent(oldList+"", newList+""));
    102                                 //logMap.put(name, "<p><strong>" + name + ":</strong><small><del>" + oldList + "</del><span class='text-danger'>" + newList+"</span></small></p>");
    103                                 //logMap.put(name, "字段名称:" + name + "旧值:" + oldList + "新值:" + newList);
    104                             }
    105                         //Map
    106                         }else if(Map.class.equals(pd.getPropertyType())){
    107                             Map<String, Object> oldMap = (Map<String, Object>) oldVal;
    108                             Map<String, Object> newMap = (Map<String, Object>) newVal;
    109                             if(oldMap != null && newMap != null){
    110                                 //比较两个Map
    111                                 Class tempClazz = null;
    112                                 for(Object obj : oldMap.values()){
    113                                     if(obj == null){
    114                                         continue;
    115                                     }
    116                                     tempClazz = obj.getClass();
    117                                     break;
    118                                 }
    119                                 if(tempClazz == null){
    120                                     for(Object obj : newMap.values()){
    121                                         if(obj == null){
    122                                             continue;
    123                                         }
    124                                         tempClazz = obj.getClass();
    125                                         break;
    126                                     }
    127                                 }
    128                                 if(tempClazz == null){
    129                                     continue;
    130                                 }
    131                                 //Map的值是基本数据类型 或者接口类型List/Map
    132                                 if(isBaseData(tempClazz) || tempClazz.isInterface()){
    133                                     compareMap(oldMap, newMap, name, newFiled, logMap);//比较Map
    134                                 //Map的值不是基本数据类型
    135                                 }else{
    136                                     String tempName = null;
    137                                     for(String key : oldMap.keySet()){
    138                                         tempName = name + "." + key;
    139                                         if(newMap.get(key) == null){
    140                                             newFiled.put("unset|"+tempName, oldMap.get(key));//删除
    141                                             logMap.put(tempName, new LogContent(oldMap.get(key)+"", ""));
    142                                             //logMap.put(tempName, "<p><strong>" + tempName + ":</strong><small><del>" + oldMap.get(key) + "</del></small></p>");
    143                                             //logMap.put(tempName, "移除:字段名称:"+tempName + "原值" + oldMap.get(key));
    144                                         }else{
    145                                             Object oldmVal = oldMap.get(key);
    146                                             Object newmVal = newMap.get(key);
    147                                             if(oldmVal != newmVal){
    148                                                 differBean(newFiled, logMap, tempName, oldmVal, newmVal);
    149                                             }
    150                                         }
    151                                     }
    152                                     for(String key : newMap.keySet()){
    153                                         if(oldMap.get(key) == null){
    154                                             tempName = name + "." + key;
    155                                             newFiled.put(tempName, newMap.get(key));
    156                                             logMap.put(tempName, new LogContent("", newMap.get(key)+""));
    157                                             //logMap.put(tempName, "<p><strong>" + tempName + ":</strong><small><span class='text-danger'>" +  newMap.get(key)+"</span></small></p>");
    158                                             //logMap.put(tempName, "添加:字段名称:"+tempName + "新值" + newMap.get(key));
    159                                         }
    160                                     }
    161                                     
    162                                 }
    163                             }else{
    164                                 newFiled.put(name, newMap);
    165                                 logMap.put(name, new LogContent(oldMap+"", newMap+""));
    166                                 //logMap.put(name, "<p><strong>" + name + ":</strong><small><del>" + oldMap + "</del><span class='text-danger'>" + oldMap+"</span></small></p>");
    167                                 //logMap.put(name, "字段名称:" + name + "旧值:" + oldMap + "新值:" + newMap);
    168                             }
    169                         }
    170                     //3. 对象
    171                     }else{
    172                         differBean(newFiled, logMap,name, oldVal, newVal);
    173                     }
    174                 }
    175             }
    176         } catch (Exception e) {
    177             throw new AppException("修改生成Update失败");
    178         }
    179     }
    180     
    181 
    182 
    183     /**
    184      * 比较两个map
    185      * @param oldMap
    186      * @param newMap
    187      * @param name
    188      * @param newFiled
    189      * @param logMap
    190      */
    191     private static void compareMap(Map<String, Object> oldMap,
    192             Map<String, Object> newMap, String name,
    193             Map<String, Object> newFiled, Map<String, LogContent> logMap) {
    194         if(newFiled == null){
    195             newFiled = new LinkedHashMap<String, Object>();
    196         }
    197         if(oldMap == null || newMap == null){
    198             throw new AppException("Map比较时出现错误");
    199         }
    200         
    201         String tempName = name;
    202         //1. 移除和添加
    203         for(String key : oldMap.keySet()){
    204             tempName = name + "." + key;
    205             if(newMap.get(key) == null){
    206                 newFiled.put(tempName+"|unset", oldMap.get(key));//删除
    207                 logMap.put(tempName, new LogContent(oldMap.get(key)+"", ""));
    208                 
    209                 //logMap.put(tempName, "<p><strong>" + tempName + ":</strong><small><del>" + oldMap.get(key) + "</del></small></p>");
    210                 //logMap.put(tempName, "移除:字段名称:"+tempName + "原值" + oldMap.get(key));
    211             }else{
    212                 Object oldVal = oldMap.get(key);
    213                 Object newVal = newMap.get(key);
    214                 if(oldVal != newVal){
    215                     newFiled.put(tempName, newVal);
    216                     logMap.put(tempName, new LogContent(oldVal+"", newVal+""));
    217                     //logMap.put(tempName, "<p><strong>" + tempName + ":</strong><small><del>" + oldVal + "</del><span class='text-danger'>" + newVal+"</span></small></p>");
    218                     //logMap.put(tempName, "字段名称:"+tempName+"旧值:"+oldVal + "新值:" + newVal);
    219                 }
    220             }
    221         }
    222         
    223         //2. 添加
    224         for(String key : newMap.keySet()){
    225             if(oldMap.get(key) == null){
    226                 tempName = name + "." + key;
    227                 newFiled.put(tempName, newMap.get(key));
    228                 logMap.put(tempName, new LogContent("", newMap.get(key)+""));
    229                 //logMap.put(tempName, "<p><strong>" + tempName + ":</strong><small><span class='text-danger'>" + newMap.get(key)+"</span></small></p>");
    230                 //logMap.put(tempName, "添加:字段名称:"+tempName + "新值" + newMap.get(key));
    231             }
    232         }
    233         
    234     }
    235 
    236     public static void main(String[] args) {
    237         List<String> list = new ArrayList<String>();
    238         list.add("aaa");
    239         String a = list.get(0);
    240         String b = list.get(1);
    241         System.out.println();
    242     }
    243     public static void aa(Object obj){
    244         Map<String, String> map = (Map<String, String>) obj;
    245         System.out.println(map);
    246     }
    247     
    248     /**
    249      * 比较两个List
    250      * 用于记录日志
    251      */
    252     private static void compareList(List<Object> oldList, List<Object> newList,
    253             String name, Map<String, LogContent> logMap) {
    254         if(oldList == null || newList == null){
    255             throw new AppException("比较错误");
    256         }
    257         List<Object> removeList = new ArrayList<Object>();
    258         List<Object> addList = new ArrayList<Object>();
    259         for(Object obj : oldList){
    260             if(!newList.contains(obj)){
    261                 removeList.add(obj);
    262             }
    263         }
    264         for(Object obj : newList){
    265             if(!oldList.contains(obj)){
    266                 addList.add(obj);
    267             }
    268         }
    269         
    270         //移除
    271         if(!CollectionUtils.isEmpty(removeList)){
    272             logMap.put(name+"_1", new LogContent(removeList.toString(), ""));
    273             //logMap.put(name+"_1", "<p><strong>" + name + ":</strong><small><del>" + removeList + "</del></small></p>");
    274             //logMap.put(name, "字段名称:"+name +"移除:" + removeList.toString());
    275         }
    276         //添加
    277         if(!CollectionUtils.isEmpty(addList)){
    278             logMap.put(name+"_2", new LogContent("", addList.toString()));
    279             //logMap.put(name+"_2", "<p><strong>" + name + ":</strong><small><span class='text-danger'>" + addList+"</span></small></p>");
    280             //logMap.put(name, "字段名称:"+name+"增加:"+ addList.toString());
    281         }
    282         
    283     }
    284     /**
    285      * 判断是否是基本数据类型
    286      */
    287     public static boolean isBaseData(Class clazz){
    288         /*byte ——Byte
    289         short ——Short
    290         int ——Integer
    291         long-----Long
    292         float-----Float
    293         double----Double 
    294         char----Character
    295         boolean---Boolean*/
    296         if(clazz.isPrimitive() ||
    297                 String.class.equals(clazz)||
    298                 Byte.class.equals(clazz)||
    299                 Short.class.equals(clazz)||
    300                 Integer.class.equals(clazz)||
    301                 Long.class.equals(clazz)||
    302                 Float.class.equals(clazz)||
    303                 Double.class.equals(clazz)||
    304                 Character.class.equals(clazz)||
    305                 Boolean.class.equals(clazz)){
    306             return true;
    307         }
    308         return false;
    309     }
    310     
    311     /**
    312      * 迭代记录日志
    313      */
    314     private static void differObject(Map<String, LogContent> logMap, String parName,
    315             Object oldObj, Object newObj) {
    316         try {
    317             if(oldObj.getClass()==newObj.getClass()){//只有两个对象都是同一类型的才有可比性
    318                 Class clazz = oldObj.getClass();
    319                 PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,Object.class).getPropertyDescriptors();
    320                 for(PropertyDescriptor pd : pds){
    321                     Field f = clazz.getDeclaredField(pd.getName());
    322                     f.setAccessible(true); 
    323                     if(f.getAnnotation(Transient.class) != null || f.getAnnotation(Id.class) != null || f.getAnnotation(DNU.class)!=null){
    324                         continue;
    325                     }
    326                     
    327                     String name = null;
    328                     if(parName != null){
    329                         name = parName+"."+pd.getName();
    330                     }else{
    331                         name = pd.getName();
    332                     }
    333                     
    334                     Object oldVal = pd.getReadMethod().invoke(oldObj, null);
    335                     Object newVal = pd.getReadMethod().invoke(newObj, null);
    336                     
    337                     if(oldVal == null && newVal == null){
    338                         continue;
    339                     }else if(oldVal != null && oldVal.equals(newVal)){
    340                         continue;
    341                     }else if(newVal != null && newVal.equals(oldVal)){
    342                         continue;
    343                     }
    344                     
    345                     //1. 基本类型
    346                     if(isBaseData(pd.getPropertyType())){
    347                         //不记录
    348                         if("createTime".equals(name) || "status".equals(name)){
    349                             continue;
    350                         }
    351                         //日志
    352                         if(!"lastUpdateTime".equals(name)){
    353                             logMap.put(name, new LogContent(oldVal+"", newVal+""));
    354                             //logMap.put(name, "<p><strong>" + name + ":</strong><small><del>" + oldVal + "</del><span class='text-danger'>" + newVal+"</span></small></p>");
    355                         }
    356                             
    357                     //2. 接口类型
    358                     }else if(pd.getPropertyType().isInterface()){
    359                         //List
    360                         if(List.class.equals(pd.getPropertyType())){
    361                             List<Object> oldList = (List<Object>) oldVal;
    362                             List<Object> newList = (List<Object>) newVal;
    363                             
    364                             if(oldList != null && oldList.size()>0&& newList != null &&newList.size()>0){
    365                                 Object t = oldList.get(0);
    366                                 //更新
    367                                 if(isBaseData(t.getClass())){
    368                                     //比较两个List
    369                                     compareList(oldList, newList, name, logMap);
    370                                 }else{
    371                                     int max = oldList.size();
    372                                     max = max > newList.size()? newList.size():oldList.size();
    373                                     for(int i = 0;i<max;i++){
    374                                         Object oldListObj = null;
    375                                         try {
    376                                             oldListObj = oldList.get(i);
    377                                         } catch (Exception e) {
    378                                             oldListObj = "";
    379                                         }
    380                                         Object newListObj = null;
    381                                         try {
    382                                             newListObj = newList.get(i);
    383                                         } catch (Exception e) {
    384                                             newListObj = "";
    385                                         }
    386                                         if(!"".equals(oldListObj) && !"".equals(newListObj)){
    387                                             differObject(logMap, name+"["+i+"]", oldListObj, newListObj);
    388                                         }else{
    389                                             logMap.put(name+"["+i+"]", new LogContent(oldListObj+"", newListObj+""));
    390                                         }
    391                                         
    392                                         
    393                                     }
    394                                     
    395                                 }
    396                             }else{
    397                                 logMap.put(name, new LogContent(oldList+"", newList+""));
    398                                 //logMap.put(name, "<p><strong>" + name + ":</strong><small><del>" + oldList + "</del><span class='text-danger'>" + newList+"</span></small></p>");
    399                             }
    400                         //Map
    401                         }else if(Map.class.equals(pd.getPropertyType())){
    402                             Map<String, Object> oldMap = (Map<String, Object>) oldVal;
    403                             Map<String, Object> newMap = (Map<String, Object>) newVal;
    404                             if(oldMap != null && newMap != null){
    405                                 //比较两个Map
    406                                 Class tempClazz = null;
    407                                 for(Object obj : oldMap.values()){
    408                                     if(obj == null){
    409                                         continue;
    410                                     }
    411                                     tempClazz = obj.getClass();
    412                                     break;
    413                                 }
    414                                 if(tempClazz == null){
    415                                     for(Object obj : newMap.values()){
    416                                         if(obj == null){
    417                                             continue;
    418                                         }
    419                                         tempClazz = obj.getClass();
    420                                         break;
    421                                     }
    422                                 }
    423                                 if(tempClazz == null){
    424                                     continue;
    425                                 }
    426                                 //Map的值是基本数据类型 或者接口类型List/Map
    427                                 if(isBaseData(tempClazz) || tempClazz.isInterface()){
    428                                     compareMap(oldMap, newMap, name, null, logMap);//比较Map
    429                                 //Map的值不是基本数据类型
    430                                 }else{
    431                                     String tempName = null;
    432                                     for(String key : oldMap.keySet()){
    433                                         tempName = name + "." + key;
    434                                         if(newMap.get(key) == null){
    435                                             logMap.put(tempName, new LogContent(oldMap.get(key)+"", ""));
    436                                             //logMap.put(tempName, "<p><strong>" + tempName + ":</strong><small><del>" + oldMap.get(key) + "</del></small></p>");
    437                                             //logMap.put(tempName, "移除:字段名称:"+tempName + "原值" + oldMap.get(key));
    438                                         }else{
    439                                             Object oldmVal = oldMap.get(key);
    440                                             Object newmVal = newMap.get(key);
    441                                             if(oldmVal != newmVal){
    442                                                 differObject(logMap, tempName, oldmVal, newmVal);
    443                                             }
    444                                         }
    445                                     }
    446                                     for(String key : newMap.keySet()){
    447                                         if(oldMap.get(key) == null){
    448                                             tempName = name + "." + key;
    449                                             logMap.put(tempName, new LogContent("", newMap.get(key)+""));
    450                                             //logMap.put(tempName, "<p><strong>" + tempName + ":</strong><small><span class='text-danger'>" +  newMap.get(key)+"</span></small></p>");
    451                                             //logMap.put(tempName, "添加:字段名称:"+tempName + "新值" + newMap.get(key));
    452                                         }
    453                                     }
    454                                     
    455                                 }
    456                             }else{
    457                                 logMap.put(name, new LogContent(oldMap+"", newMap+""));
    458                             }
    459                         }
    460                     //3. 对象
    461                     }else{
    462                         differObject(logMap,name, oldVal, newVal);
    463                     }
    464                     
    465                 }
    466             }
    467         } catch (Exception e) {
    468             System.out.println(e.getMessage());
    469         }
    470         
    471     }
    472 }


    学了



  • 相关阅读:
    Linux Kernel 'b43'无线驱动本地特权提升漏洞
    Linux Kernel “dispatch_discard_io()”RO Disk Manipulation安全绕过漏洞
    Linux kernel ‘fill_event_metadata’函数资源管理错误漏洞
    Linux kernel ‘b43_request_firmware’函数格式化字符串漏洞
    phpcms 2007 onunload.inc.php update SQL注入漏洞
    出差到天津的计划
    Auguries of Innocence
    做一个东西要多久
    Wise Installation 9 的一个容易忽视的选项
    天津行总结路在嘴底下
  • 原文地址:https://www.cnblogs.com/zhoujian43/p/6323014.html
Copyright © 2011-2022 走看看