BeanUtil.java 850 B

1234567891011121314151617181920212223242526272829303132333435
  1. package com.shkpr.service.alambizplugin.commtools;
  2. import org.apache.commons.lang3.ObjectUtils;
  3. import org.springframework.beans.BeanUtils;
  4. /**
  5. * bean工具类
  6. *
  7. * @author 欧阳劲驰
  8. * @since 1.0.0
  9. */
  10. public class BeanUtil {
  11. /**
  12. * 拷贝
  13. *
  14. * @param source 被拷贝对象
  15. * @param targetType 类型
  16. * @param <T> 类型
  17. * @return 新对象
  18. */
  19. public static <T> T copy(Object source, Class<T> targetType) {
  20. try {
  21. if (!ObjectUtils.allNotNull(source, targetType)) {
  22. return null;
  23. }
  24. //拷贝资源
  25. T t = targetType.getDeclaredConstructor().newInstance();
  26. BeanUtils.copyProperties(source, t);
  27. return t;
  28. } catch (ReflectiveOperationException e) {
  29. return null;
  30. }
  31. }
  32. }