BeanUtils.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.ozs.common.utils.bean;
  2. import java.lang.reflect.Method;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. /**
  8. * Bean 工具类
  9. *
  10. * @author ruoyi
  11. */
  12. public class BeanUtils extends org.springframework.beans.BeanUtils {
  13. /**
  14. * Bean方法名中属性名开始的下标
  15. */
  16. private static final int BEAN_METHOD_PROP_INDEX = 3;
  17. /**
  18. * 匹配getter方法的正则表达式
  19. */
  20. private static final Pattern GET_PATTERN = Pattern.compile("get(\\p{javaUpperCase}\\w*)");
  21. /**
  22. * 匹配setter方法的正则表达式
  23. */
  24. private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)");
  25. /**
  26. * Bean属性复制工具方法。
  27. *
  28. * @param dest 目标对象
  29. * @param src 源对象
  30. */
  31. public static void copyBeanProp(Object dest, Object src) {
  32. try {
  33. copyProperties(src, dest);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. /**
  39. * 获取对象的setter方法。
  40. *
  41. * @param obj 对象
  42. * @return 对象的setter方法列表
  43. */
  44. public static List<Method> getSetterMethods(Object obj) {
  45. // setter方法列表
  46. List<Method> setterMethods = new ArrayList<Method>();
  47. // 获取所有方法
  48. Method[] methods = obj.getClass().getMethods();
  49. // 查找setter方法
  50. for (Method method : methods) {
  51. Matcher m = SET_PATTERN.matcher(method.getName());
  52. if (m.matches() && (method.getParameterTypes().length == 1)) {
  53. setterMethods.add(method);
  54. }
  55. }
  56. // 返回setter方法列表
  57. return setterMethods;
  58. }
  59. /**
  60. * 获取对象的getter方法。
  61. *
  62. * @param obj 对象
  63. * @return 对象的getter方法列表
  64. */
  65. public static List<Method> getGetterMethods(Object obj) {
  66. // getter方法列表
  67. List<Method> getterMethods = new ArrayList<Method>();
  68. // 获取所有方法
  69. Method[] methods = obj.getClass().getMethods();
  70. // 查找getter方法
  71. for (Method method : methods) {
  72. Matcher m = GET_PATTERN.matcher(method.getName());
  73. if (m.matches() && (method.getParameterTypes().length == 0)) {
  74. getterMethods.add(method);
  75. }
  76. }
  77. // 返回getter方法列表
  78. return getterMethods;
  79. }
  80. /**
  81. * 检查Bean方法名中的属性名是否相等。<br>
  82. * 如getName()和setName()属性名一样,getName()和setAge()属性名不一样。
  83. *
  84. * @param m1 方法名1
  85. * @param m2 方法名2
  86. * @return 属性名一样返回true,否则返回false
  87. */
  88. public static boolean isMethodPropEquals(String m1, String m2) {
  89. return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
  90. }
  91. /**
  92. * 将实体类转换为Vo类
  93. * @param list
  94. * @param clazz
  95. * @param <T>
  96. * @return
  97. */
  98. public static <T> List<T> entityListToVOList(List<?> list, Class<T> clazz) {
  99. List<T> result = new ArrayList<>(list.size());
  100. for (Object source : list) {
  101. T target;
  102. try {
  103. target = clazz.getDeclaredConstructor().newInstance();
  104. } catch (Exception e) {
  105. throw new RuntimeException();
  106. }
  107. BeanUtils.copyProperties(source, target);
  108. result.add(target);
  109. }
  110. return result;
  111. }
  112. }