RuoYi
2020-07-19 cee572f237cb293e4a9070ef458356bc6daa2105
ruoyi-common/src/main/java/com/ruoyi/common/utils/bean/BeanUtils.java
ÎļþÃû´Ó ruoyi/src/main/java/com/ruoyi/common/utils/bean/BeanUtils.java ÐÞ¸Ä
@@ -1,110 +1,110 @@
package com.ruoyi.common.utils.bean;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * Bean å·¥å…·ç±»
 *
 * @author ruoyi
 */
public class BeanUtils extends org.springframework.beans.BeanUtils
{
    /** Bean方法名中属性名开始的下标 */
    private static final int BEAN_METHOD_PROP_INDEX = 3;
    /** * åŒ¹é…getter方法的正则表达式 */
    private static final Pattern GET_PATTERN = Pattern.compile("get(\\p{javaUpperCase}\\w*)");
    /** * åŒ¹é…setter方法的正则表达式 */
    private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)");
    /**
     * Bean属性复制工具方法。
     *
     * @param dest ç›®æ ‡å¯¹è±¡
     * @param src æºå¯¹è±¡
     */
    public static void copyBeanProp(Object dest, Object src)
    {
        try
        {
            copyProperties(src, dest);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    /**
     * èŽ·å–å¯¹è±¡çš„setter方法。
     *
     * @param obj å¯¹è±¡
     * @return å¯¹è±¡çš„setter方法列表
     */
    public static List<Method> getSetterMethods(Object obj)
    {
        // setter方法列表
        List<Method> setterMethods = new ArrayList<Method>();
        // èŽ·å–æ‰€æœ‰æ–¹æ³•
        Method[] methods = obj.getClass().getMethods();
        // æŸ¥æ‰¾setter方法
        for (Method method : methods)
        {
            Matcher m = SET_PATTERN.matcher(method.getName());
            if (m.matches() && (method.getParameterTypes().length == 1))
            {
                setterMethods.add(method);
            }
        }
        // è¿”回setter方法列表
        return setterMethods;
    }
    /**
     * èŽ·å–å¯¹è±¡çš„getter方法。
     *
     * @param obj å¯¹è±¡
     * @return å¯¹è±¡çš„getter方法列表
     */
    public static List<Method> getGetterMethods(Object obj)
    {
        // getter方法列表
        List<Method> getterMethods = new ArrayList<Method>();
        // èŽ·å–æ‰€æœ‰æ–¹æ³•
        Method[] methods = obj.getClass().getMethods();
        // æŸ¥æ‰¾getter方法
        for (Method method : methods)
        {
            Matcher m = GET_PATTERN.matcher(method.getName());
            if (m.matches() && (method.getParameterTypes().length == 0))
            {
                getterMethods.add(method);
            }
        }
        // è¿”回getter方法列表
        return getterMethods;
    }
    /**
     * æ£€æŸ¥Bean方法名中的属性名是否相等。<br>
     * å¦‚getName()和setName()属性名一样,getName()和setAge()属性名不一样。
     *
     * @param m1 æ–¹æ³•名1
     * @param m2 æ–¹æ³•名2
     * @return å±žæ€§åä¸€æ ·è¿”回true,否则返回false
     */
    public static boolean isMethodPropEquals(String m1, String m2)
    {
        return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
    }
}
package com.ruoyi.common.utils.bean;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * Bean å·¥å…·ç±»
 *
 * @author ruoyi
 */
public class BeanUtils extends org.springframework.beans.BeanUtils
{
    /** Bean方法名中属性名开始的下标 */
    private static final int BEAN_METHOD_PROP_INDEX = 3;
    /** * åŒ¹é…getter方法的正则表达式 */
    private static final Pattern GET_PATTERN = Pattern.compile("get(\\p{javaUpperCase}\\w*)");
    /** * åŒ¹é…setter方法的正则表达式 */
    private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)");
    /**
     * Bean属性复制工具方法。
     *
     * @param dest ç›®æ ‡å¯¹è±¡
     * @param src æºå¯¹è±¡
     */
    public static void copyBeanProp(Object dest, Object src)
    {
        try
        {
            copyProperties(src, dest);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    /**
     * èŽ·å–å¯¹è±¡çš„setter方法。
     *
     * @param obj å¯¹è±¡
     * @return å¯¹è±¡çš„setter方法列表
     */
    public static List<Method> getSetterMethods(Object obj)
    {
        // setter方法列表
        List<Method> setterMethods = new ArrayList<Method>();
        // èŽ·å–æ‰€æœ‰æ–¹æ³•
        Method[] methods = obj.getClass().getMethods();
        // æŸ¥æ‰¾setter方法
        for (Method method : methods)
        {
            Matcher m = SET_PATTERN.matcher(method.getName());
            if (m.matches() && (method.getParameterTypes().length == 1))
            {
                setterMethods.add(method);
            }
        }
        // è¿”回setter方法列表
        return setterMethods;
    }
    /**
     * èŽ·å–å¯¹è±¡çš„getter方法。
     *
     * @param obj å¯¹è±¡
     * @return å¯¹è±¡çš„getter方法列表
     */
    public static List<Method> getGetterMethods(Object obj)
    {
        // getter方法列表
        List<Method> getterMethods = new ArrayList<Method>();
        // èŽ·å–æ‰€æœ‰æ–¹æ³•
        Method[] methods = obj.getClass().getMethods();
        // æŸ¥æ‰¾getter方法
        for (Method method : methods)
        {
            Matcher m = GET_PATTERN.matcher(method.getName());
            if (m.matches() && (method.getParameterTypes().length == 0))
            {
                getterMethods.add(method);
            }
        }
        // è¿”回getter方法列表
        return getterMethods;
    }
    /**
     * æ£€æŸ¥Bean方法名中的属性名是否相等。<br>
     * å¦‚getName()和setName()属性名一样,getName()和setAge()属性名不一样。
     *
     * @param m1 æ–¹æ³•名1
     * @param m2 æ–¹æ³•名2
     * @return å±žæ€§åä¸€æ ·è¿”回true,否则返回false
     */
    public static boolean isMethodPropEquals(String m1, String m2)
    {
        return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
    }
}