From 23182f2c951df5fa55e70e30ff70ddaf91199a2e Mon Sep 17 00:00:00 2001
From: zhang <zc857179121@qq.com>
Date: 星期四, 05 二月 2026 15:57:58 +0800
Subject: [PATCH] 1

---
 zy-acs-hex/src/main/java/com/zy/acs/hex/utils/ReflectionUtils.java |  171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 171 insertions(+), 0 deletions(-)

diff --git a/zy-acs-hex/src/main/java/com/zy/acs/hex/utils/ReflectionUtils.java b/zy-acs-hex/src/main/java/com/zy/acs/hex/utils/ReflectionUtils.java
new file mode 100644
index 0000000..bc699ee
--- /dev/null
+++ b/zy-acs-hex/src/main/java/com/zy/acs/hex/utils/ReflectionUtils.java
@@ -0,0 +1,171 @@
+package com.zy.acs.hex.utils;
+
+import com.zy.acs.framework.common.Cools;
+
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ReflectionUtils {
+    /**
+     * 鑾峰彇涓�涓被鍜屽叾鐖剁被鐨勬墍鏈夊睘鎬�
+     *
+     * @param clazz
+     * @return
+     */
+    public static List<Field> findAllFieldsOfSelfAndSuperClass(Class clazz) {
+        Field[] fields = null;
+        List fieldList = new ArrayList();
+        while (true) {
+            if (clazz == null) {
+                break;
+            } else {
+                fields = clazz.getDeclaredFields();
+                for (int i = 0; i < fields.length; i++) {
+                    fieldList.add(fields[i]);
+                }
+                clazz = clazz.getSuperclass();
+            }
+        }
+        return fieldList;
+    }
+
+    /**
+     * 鎶婁竴涓狟ean瀵硅薄杞崲鎴怣ap瀵硅薄</br>
+     *
+     * @param obj
+     * @param ignores
+     * @return
+     * @throws IllegalAccessException
+     */
+    public static Map convertBean2Map(Object obj, String[] ignores) {
+        Map map = new HashMap();
+        Class clazz = obj.getClass();
+        List<Field> fieldList = findAllFieldsOfSelfAndSuperClass(clazz);
+        Field field = null;
+        try {
+            for (int i = 0; i < fieldList.size(); i++) {
+                field = fieldList.get(i);
+                // 瀹氫箟fieldName鏄惁鍦ㄦ嫹璐濆拷鐣ョ殑鑼冪暣鍐�
+                boolean flag = false;
+                if (ignores != null && ignores.length != 0) {
+                    flag = isExistOfIgnores(field.getName(), ignores);
+                }
+                if (!flag) {
+                    Object value = getProperty(obj, field.getName());
+                    if (null != value
+                            && !Cools.isEmpty(value.toString())) {
+                        map.put(field.getName(),
+                                getProperty(obj, field.getName()));
+                    }
+                }
+            }
+        } catch (SecurityException e) {
+            e.printStackTrace();
+        } catch (IllegalArgumentException e) {
+            e.printStackTrace();
+        }
+        return map;
+    }
+
+    /**
+     * 鎶婁竴涓狟ean瀵硅薄杞崲鎴怣ap瀵硅薄</br>
+     *
+     * @param obj
+     * @return
+     */
+    public static Map convertBean2Map(Object obj) {
+        return convertBean2Map(obj, null);
+    }
+
+    public static Map convertBean2MapForIngoreserialVersionUID(Object obj) {
+        return convertBean2Map(obj, new String[]{"serialVersionUID"});
+    }
+
+    /**
+     * 鍒ゆ柇fieldName鏄惁鏄痠gnores涓帓闄ょ殑
+     *
+     * @param fieldName
+     * @param ignores
+     * @return
+     */
+    private static boolean isExistOfIgnores(String fieldName,
+                                            String[] ignores) {
+        boolean flag = false;
+        for (String str : ignores) {
+            if (str.equals(fieldName)) {
+                flag = true;
+                break;
+            }
+        }
+        return flag;
+    }
+
+    public static PropertyDescriptor getPropertyDescriptor(Class clazz,
+                                                           String propertyName) {
+        StringBuffer sb = new StringBuffer();// 鏋勫缓涓�涓彲鍙樺瓧绗︿覆鐢ㄦ潵鏋勫缓鏂规硶鍚嶇О
+        Method setMethod = null;
+        Method getMethod = null;
+        PropertyDescriptor pd = null;
+        try {
+            Field f = clazz.getDeclaredField(propertyName);// 鏍规嵁瀛楁鍚嶆潵鑾峰彇瀛楁
+            if (f != null) {
+                // 鏋勫缓鏂规硶鐨勫悗缂�
+                String methodEnd = propertyName.substring(0, 1).toUpperCase()
+                        + propertyName.substring(1);
+                sb.append("set" + methodEnd);// 鏋勫缓set鏂规硶
+                setMethod = clazz.getDeclaredMethod(sb.toString(),
+                        new Class[]{f.getType()});
+                sb.delete(0, sb.length());// 娓呯┖鏁翠釜鍙彉瀛楃涓�
+                sb.append("get" + methodEnd);// 鏋勫缓get鏂规硶
+                // 鏋勫缓get 鏂规硶
+                getMethod =
+                        clazz.getDeclaredMethod(sb.toString(), new Class[]{});
+                // 鏋勫缓涓�涓睘鎬ф弿杩板櫒 鎶婂搴斿睘鎬� propertyName 鐨� get 鍜� set 鏂规硶淇濆瓨鍒板睘鎬ф弿杩板櫒涓�
+                pd = new PropertyDescriptor(propertyName, getMethod, setMethod);
+            }
+        } catch (Exception ex) {
+            ex.printStackTrace();
+        }
+
+        return pd;
+    }
+
+    @SuppressWarnings("unchecked")
+    public static void setProperty(Object obj, String propertyName,
+                                   Object value) {
+        Class clazz = obj.getClass();// 鑾峰彇瀵硅薄鐨勭被鍨�
+        PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);// 鑾峰彇 clazz
+        // 绫诲瀷涓殑
+        // propertyName
+        // 鐨勫睘鎬ф弿杩板櫒
+        Method setMethod = pd.getWriteMethod();// 浠庡睘鎬ф弿杩板櫒涓幏鍙� set 鏂规硶
+        try {
+            setMethod.invoke(obj, new Object[]{value});// 璋冪敤 set 鏂规硶灏嗕紶鍏ョ殑value鍊间繚瀛樺睘鎬т腑鍘�
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    public static Object getProperty(Object obj, String propertyName) {
+        Class clazz = obj.getClass();// 鑾峰彇瀵硅薄鐨勭被鍨�
+        PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);// 鑾峰彇 clazz
+        // 绫诲瀷涓殑
+        // propertyName
+        // 鐨勫睘鎬ф弿杩板櫒
+        Method getMethod = pd.getReadMethod();// 浠庡睘鎬ф弿杩板櫒涓幏鍙� get 鏂规硶
+        Object value = null;
+        try {
+            value = getMethod.invoke(obj, new Object[]{});// 璋冪敤鏂规硶鑾峰彇鏂规硶鐨勮繑鍥炲��
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return value;// 杩斿洖鍊�
+    }
+
+}

--
Gitblit v1.9.1