zhoutg 4 лет назад
Родитель
Сommit
4d6132679d

+ 3 - 3
src/main/java/com/diagbot/facade/CommonFacade.java

@@ -232,8 +232,8 @@ public class CommonFacade {
                             try {
                                 T tNew = (T) tList.get(0).getClass().newInstance(); // 泛型不能直接创建对象
                                 BeanUtil.copyProperties(t, tNew);
-                                ReflectUtil.setFieldValue(tNew, "name", s);
-                                ReflectUtil.setFieldValue(tNew, "standName", s);
+                                ReflectUtil.setProperty(tNew, "name", s);
+                                ReflectUtil.setProperty(tNew, "standName", s);
                                 participleList.add(tNew);
                             } catch (Exception e) {
                                 e.printStackTrace();
@@ -748,7 +748,7 @@ public class CommonFacade {
     public <T> void setListProperty(Object obj, String listProperty, String objProperty, Map<String, Map<String, Long>> configMap) {
         Object tList = CoreUtil.getFieldValue(obj, listProperty);
         List<T> newList = convertStandName((List) tList, configMap, objProperty);
-        ReflectUtil.setFieldValue(obj, listProperty, newList);
+        ReflectUtil.setProperty(obj, listProperty, newList);
     }
 
     /**

+ 1 - 1
src/main/java/com/diagbot/util/CoreUtil.java

@@ -266,7 +266,7 @@ public class CoreUtil {
             String value = (String)getFieldValue(r, "uniqueName");
             if (StringUtil.isBlank(value)) {
                 String detailName = (String) getFieldValue(r, targetProperty);
-                ReflectUtil.setFieldValue(r, "uniqueName", detailName);
+                ReflectUtil.setProperty(r, "uniqueName", detailName);
             }
         }
     }

+ 31 - 3
src/main/java/com/diagbot/util/ReflectUtil.java

@@ -3,6 +3,8 @@ package com.diagbot.util;
 import com.google.common.collect.Lists;
 
 import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 
 /**
@@ -96,11 +98,11 @@ public class ReflectUtil {
      * @param property
      * @param value
      */
-    public static void setFieldValue(Object object, String property, Object value){
+    public static void setProperty(Object object, String property, Object value) {
         //根据 对象和属性名通过反射 调用上面的方法获取 Field对象
-        Field field = getDeclaredField(object, property) ;
+        Field field = getDeclaredField(object, property);
         //抑制Java对其的检查
-        field.setAccessible(true) ;
+        field.setAccessible(true);
         try {
             field.set(object, value);
         } catch (IllegalArgumentException e) {
@@ -110,6 +112,32 @@ public class ReflectUtil {
         }
     }
 
+    /**
+     * 根据指定字段按照字符串排序
+     *
+     * @param tList
+     * @param property
+     * @param <T>
+     */
+    public <T> void sort(List<T> tList, String property) {
+        if (ListUtil.isNotEmpty(tList) && tList.size() > 1) {
+            Collections.sort(tList, new Comparator<T>() {
+                @Override
+                public int compare(T o1, T o2) {
+                    String v1 = getProperty(o1, property);
+                    String v2 = getProperty(o2, property);
+                    if (StringUtil.isBlank(v1)) {
+                        return -1;
+                    }
+                    if (StringUtil.isBlank(v2)) {
+                        return 1;
+                    }
+                    return v1.compareTo(v2);
+                }
+            });
+        }
+    }
+
     public static void main(String[] args) {
 
     }