|
@@ -0,0 +1,121 @@
|
|
|
+package com.diagbot.util;
|
|
|
+
|
|
|
+import com.diagbot.model.entity.Clinical;
|
|
|
+import com.diagbot.model.entity.General;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @description:
|
|
|
+ * @author: zhoutg
|
|
|
+ * @time: 2020/7/30 15:18
|
|
|
+ */
|
|
|
+public class CoreUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将列表中属性对应的值以list形式返回
|
|
|
+ *
|
|
|
+ * @param list
|
|
|
+ * @param propertyName
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> List<String> getPropertyList(List<T> list, String propertyName) {
|
|
|
+ List<String> res = new ArrayList<>();
|
|
|
+ if (ListUtil.isEmpty(list)) {
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ for (T t : list) {
|
|
|
+ try {
|
|
|
+ String val = (String)getFieldValue(t, propertyName);
|
|
|
+ if (StringUtil.isNotBlank(val)) {
|
|
|
+ res.add(val);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将列表中属性(name)对应的值以list形式返回
|
|
|
+ *
|
|
|
+ * @param list
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> List<String> getPropertyList(List<T> list) {
|
|
|
+ return getPropertyList(list, "name");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 循环向上转型, 获取对象的 DeclaredField
|
|
|
+ * @param object : 子类对象
|
|
|
+ * @param fieldName : 父类中的属性名
|
|
|
+ * @return 父类中的属性对象
|
|
|
+ */
|
|
|
+ public static Field getDeclaredField(Object object, String fieldName){
|
|
|
+ Field field = null ;
|
|
|
+
|
|
|
+ Class<?> clazz = object.getClass() ;
|
|
|
+
|
|
|
+ for(; clazz != Object.class ; clazz = clazz.getSuperclass()) {
|
|
|
+ try {
|
|
|
+ field = clazz.getDeclaredField(fieldName) ;
|
|
|
+ return field ;
|
|
|
+ } catch (Exception e) {
|
|
|
+ //这里甚么都不要做!并且这里的异常必须这样写,不能抛出去。
|
|
|
+ //如果这里的异常打印或者往外抛,则就不会执行clazz = clazz.getSuperclass(),最后就不会进入到父类中了
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter
|
|
|
+ * @param object : 子类对象
|
|
|
+ * @param fieldName : 父类中的属性名
|
|
|
+ * @return : 父类中的属性值
|
|
|
+ */
|
|
|
+ public static Object getFieldValue(Object object, String fieldName){
|
|
|
+
|
|
|
+ //根据 对象和属性名通过反射 调用上面的方法获取 Field对象
|
|
|
+ Field field = getDeclaredField(object, fieldName) ;
|
|
|
+
|
|
|
+ //抑制Java对其的检查
|
|
|
+ field.setAccessible(true) ;
|
|
|
+
|
|
|
+ try {
|
|
|
+ //获取 object 中 field 所代表的属性值
|
|
|
+ return field.get(object) ;
|
|
|
+
|
|
|
+ } catch(Exception e) {
|
|
|
+ e.printStackTrace() ;
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ List<Clinical> clinicals = new ArrayList<>();
|
|
|
+ Clinical c1 = new Clinical();
|
|
|
+ c1.setName("ddd");
|
|
|
+ clinicals.add(c1);
|
|
|
+ Clinical c2 = new Clinical();
|
|
|
+ c2.setName("ddd");
|
|
|
+ clinicals.add(c2);
|
|
|
+ Clinical c3 = new Clinical();
|
|
|
+ c3.setName("ddd");
|
|
|
+ clinicals.add(c3);
|
|
|
+
|
|
|
+ List<General> generalLabelList = new ArrayList<>();
|
|
|
+ General g1 = new General();
|
|
|
+ g1.setName("ab");
|
|
|
+ generalLabelList.add(g1);
|
|
|
+ System.out.println(getPropertyList(generalLabelList));
|
|
|
+ }
|
|
|
+}
|