BeanUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package com.persagy.framework.util;
  2. import java.beans.PropertyDescriptor;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.math.BigDecimal;
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Set;
  13. import org.apache.commons.beanutils.ConvertUtils;
  14. import org.apache.commons.beanutils.PropertyUtils;
  15. import org.apache.commons.beanutils.converters.BigDecimalConverter;
  16. import org.apache.commons.beanutils.converters.DoubleConverter;
  17. import org.apache.commons.beanutils.converters.FloatConverter;
  18. import org.apache.commons.beanutils.converters.IntegerConverter;
  19. import org.apache.commons.beanutils.converters.LongConverter;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.springframework.util.Assert;
  23. @SuppressWarnings({"rawtypes", "unchecked"})
  24. public class BeanUtils {
  25. protected static final Log logger = LogFactory.getLog(BeanUtils.class);
  26. /**
  27. * 通过get、set方法复制类属性,不复制为值null的属性
  28. * @param fromObj
  29. * @param toObj
  30. * @throws Exception
  31. */
  32. public static void copyBean(Object fromObj, Object toObj) throws Exception{
  33. Map<String, Object> mname_value = new HashMap<>();
  34. Map<String, Class> mname_Type = new HashMap<>();
  35. for(Method m : fromObj.getClass().getDeclaredMethods()) {
  36. String mname = m.getName();
  37. if(mname.startsWith("get") && m.getParameterTypes().length == 0) {
  38. mname = "set" + mname.substring(3);
  39. Object value = m.invoke(fromObj);
  40. if(value != null) {
  41. mname_value.put(mname, value);
  42. mname_Type.put(mname, value.getClass());
  43. }
  44. }
  45. }
  46. if(!mname_value.isEmpty()) {
  47. Class toClass = toObj.getClass();
  48. for(String setName : mname_value.keySet()) {
  49. Method m = toClass.getDeclaredMethod(setName, mname_Type.get(setName));
  50. if(m != null) {
  51. m.invoke(toObj, mname_value.get(setName));
  52. }
  53. }
  54. }
  55. }
  56. /**
  57. * 按Filed的类型取得Field列表.
  58. */
  59. public static List<Field> getFieldsByType(Object object, Class<?> type) {
  60. List<Field> list = new ArrayList<Field>();
  61. Field[] fields = object.getClass().getDeclaredFields();
  62. for (Field field : fields) {
  63. if (field.getType().isAssignableFrom(type)) {
  64. list.add(field);
  65. }
  66. }
  67. return list;
  68. }
  69. /**
  70. * 获得field的getter函数名称.
  71. */
  72. public static String getGetterName(Class<?> type, String fieldName) {
  73. Assert.notNull(type, "Type required");
  74. Assert.hasText(fieldName, "FieldName required");
  75. if (type.getName().equals("boolean")) {
  76. return "is" + StringUtils.capitalize(fieldName);
  77. } else {
  78. return "get" + StringUtils.capitalize(fieldName);
  79. }
  80. }
  81. /**
  82. * 获得field的getter函数,如果找不到该方法,返回null.
  83. */
  84. public static Method getGetterMethod(Class<?> type, String fieldName) {
  85. try {
  86. return type.getMethod(getGetterName(type, fieldName));
  87. } catch (NoSuchMethodException e) {
  88. logger.error(e.getMessage(), e);
  89. }
  90. return null;
  91. }
  92. /**
  93. * bean属性复制
  94. * @param dest 目的
  95. * @param orig 源
  96. * @return
  97. */
  98. public static final Object copyProperties(Object dest, Object orig) {
  99. ConvertUtils.register(new DoubleConverter(null), Double.class);
  100. ConvertUtils.register(new FloatConverter(null), Float.class);
  101. ConvertUtils.register(new IntegerConverter(null), Integer.class);
  102. ConvertUtils.register(new LongConverter(null), Long.class);
  103. ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
  104. if (orig != null) {
  105. try {
  106. org.apache.commons.beanutils.BeanUtils.copyProperties(dest, orig);
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. } else {
  111. dest = null;
  112. }
  113. return dest;
  114. }
  115. /**
  116. * Bean属性复制,如果源对象的属性值为空,此方法会将空值复制到目标对象的相应属性上。
  117. *
  118. * @param dest 目标对象
  119. * @param origi 源对象
  120. */
  121. public static void copyPropertysWithNull(Object dest,Object origi){
  122. ConvertUtils.register(new DoubleConverter(null), Double.class);
  123. ConvertUtils.register(new FloatConverter(null), Float.class);
  124. ConvertUtils.register(new IntegerConverter(null), Integer.class);
  125. ConvertUtils.register(new LongConverter(null), Long.class);
  126. PropertyDescriptor[] destDescriptors=PropertyUtils.getPropertyDescriptors(dest);
  127. PropertyDescriptor[] origiDescriptors=PropertyUtils.getPropertyDescriptors(origi);
  128. List<String> origiPropertyNames=new ArrayList<String>();
  129. for (int i=0; i<origiDescriptors.length; i++) {
  130. origiPropertyNames.add(origiDescriptors[i].getName());
  131. }
  132. for (int i=0; i<destDescriptors.length; i++) {
  133. PropertyDescriptor destProperty=destDescriptors[i];
  134. String name=destProperty.getName();
  135. try {
  136. if (origiPropertyNames.contains(name)) {
  137. Object value=PropertyUtils.getSimpleProperty(origi,name);
  138. PropertyUtils.setSimpleProperty(dest,name,value);
  139. }
  140. }
  141. catch (Exception ex) {
  142. // ex.printStackTrace();
  143. }
  144. }
  145. }
  146. /**
  147. * Bean属性复制,如果源对象的属性值为" 非空",此方法会将空值复制到目标对象的相应属性上
  148. * @param dest
  149. * @param origi
  150. */
  151. public static void copyPropertysWithoutNull(Object dest,Object origi)
  152. {
  153. ConvertUtils.register(new DoubleConverter(null), Double.class);
  154. ConvertUtils.register(new FloatConverter(null), Float.class);
  155. ConvertUtils.register(new IntegerConverter(null), Integer.class);
  156. ConvertUtils.register(new LongConverter(null), Long.class);
  157. PropertyDescriptor[] destDescriptors=PropertyUtils.getPropertyDescriptors(dest);
  158. PropertyDescriptor[] origiDescriptors=PropertyUtils.getPropertyDescriptors(origi);
  159. List<String> origiPropertyNames=new ArrayList<String>();
  160. for (int i=0; i<origiDescriptors.length; i++) {
  161. origiPropertyNames.add(origiDescriptors[i].getName());
  162. }
  163. for (int i=0; i<destDescriptors.length; i++) {
  164. PropertyDescriptor destProperty=destDescriptors[i];
  165. String name=destProperty.getName();
  166. try {
  167. if (origiPropertyNames.contains(name)) {
  168. Object value=PropertyUtils.getSimpleProperty(origi,name);
  169. if(value!=null)//只复制非空值
  170. PropertyUtils.setSimpleProperty(dest,name,value);
  171. }
  172. }
  173. catch (Exception ex) {
  174. // ex.printStackTrace();
  175. }
  176. }
  177. }
  178. /**
  179. * 根据属性名和值在beanList中查找bean的位置,未找到时返回-1
  180. * @param beanList list of bean
  181. * @param propertyName 属性
  182. * @param value 值
  183. * @return
  184. * @throws NoSuchMethodException
  185. * @throws InvocationTargetException
  186. * @throws IllegalAccessException
  187. */
  188. public static int findInList(List<?> beanList,String propertyName,Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
  189. for(int i=0;i<beanList.size();i++){
  190. Object bean = beanList.get(i);
  191. Object tmpValue = PropertyUtils.getProperty(bean,propertyName);
  192. if (tmpValue == null){
  193. if(value==null){
  194. return i;
  195. }
  196. }else if(tmpValue.equals(value)){
  197. return i;
  198. }
  199. }
  200. return -1;
  201. }
  202. /**
  203. * 根据属性名和值在beanList中查找bean,未找到时返回null
  204. * @param beanList list of bean
  205. * @param propertyName 属性
  206. * @param value 值
  207. * @return
  208. * @throws NoSuchMethodException
  209. * @throws InvocationTargetException
  210. * @throws IllegalAccessException
  211. */
  212. public static Object findBean(List<?> beanList,String propertyName,Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
  213. for(int i=0;i<beanList.size();i++){
  214. Object bean = beanList.get(i);
  215. Object tmpValue = PropertyUtils.getProperty(bean,propertyName);
  216. if (tmpValue == null){
  217. if(value==null){
  218. return bean;
  219. }
  220. }else if(tmpValue.equals(value)){
  221. return bean;
  222. }
  223. }
  224. return null;
  225. }
  226. /**
  227. * 根据属性名和值在Collection中查找bean,未找到时返回null
  228. * @param collection Collection of bean
  229. * @param propertyName 属性
  230. * @param value 值
  231. * @return Object
  232. * @throws NoSuchMethodException
  233. * @throws InvocationTargetException
  234. * @throws IllegalAccessException
  235. */
  236. public static Object findBean(Collection<?> collection,String propertyName,Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
  237. for(Object bean : collection){
  238. Object tmpValue = PropertyUtils.getProperty(bean,propertyName);
  239. if (tmpValue == null){
  240. if(value==null){
  241. return bean;
  242. }
  243. }else if(tmpValue.equals(value)){
  244. return bean;
  245. }
  246. }
  247. return null;
  248. }
  249. // /**
  250. // * 判断string是否在集合中,只对Set<String>有效
  251. // * @return
  252. // */
  253. // public static boolean isStringInSet(Set<String> set, String string) {
  254. // boolean isIn=false;
  255. // for (String tmp : set) {
  256. // if (com.syswin.crm.framework.common.util.StringUtils.equalsWithNull(tmp,string)) {
  257. // isIn=true;
  258. // return isIn;
  259. // }
  260. // }
  261. // return isIn;
  262. // }
  263. /**
  264. * 判断Object是否在集合中,只对实现了equals方法的类有效
  265. * @return
  266. */
  267. public static boolean isObjectInSet(Set<Object> set, Object object) {
  268. boolean isIn=false;
  269. for (Object tmp : set) {
  270. if (object==null) {
  271. if (tmp==null) {
  272. return true;
  273. }
  274. }
  275. else if (tmp.equals(object)) {
  276. isIn=true;
  277. return isIn;
  278. }
  279. }
  280. return isIn;
  281. }
  282. /**
  283. * 根据属性名和值在Collection中查找bean,未找到时返回null
  284. * @param collection Collection of bean
  285. * @param propertyName 属性
  286. * @param value 值
  287. * @return Object
  288. * @throws NoSuchMethodException
  289. * @throws InvocationTargetException
  290. * @throws IllegalAccessException
  291. */
  292. public static Object findBean(Collection<?> collection,String[] propertyNameArray,Object[] valueArray) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
  293. for(Object bean : collection){
  294. boolean isEqual=true;
  295. for(int i=0;i<propertyNameArray.length;i++){
  296. String propertyName = propertyNameArray[i];
  297. Object value = valueArray[i];
  298. Object tmpValue = PropertyUtils.getProperty(bean,propertyName);
  299. if (tmpValue == null){
  300. if(value==null){
  301. continue;
  302. }else{
  303. isEqual=false;
  304. break;
  305. }
  306. }else if(tmpValue.equals(value)){
  307. continue;
  308. }else{
  309. isEqual=false;
  310. break;
  311. }
  312. }
  313. if (isEqual){
  314. return bean;
  315. }
  316. }
  317. return null;
  318. }
  319. }