复制实体类 @param source 被复制的实体类对象 @param to 复制完后的实体类对象 @throws Exception
(Object source, Object to, String... needProperties)
| 483 | * @throws Exception |
| 484 | */ |
| 485 | public static void copy(Object source, Object to, String... needProperties) throws Exception { |
| 486 | List<String> propertiesList = new ArrayList(Arrays.asList(needProperties)); |
| 487 | // 获取属性 |
| 488 | // BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), java.lang.Object.class); |
| 489 | // PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors(); |
| 490 | |
| 491 | BeanInfo destBean = Introspector.getBeanInfo(to.getClass(), java.lang.Object.class); |
| 492 | PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors(); |
| 493 | |
| 494 | //不复制的字段 |
| 495 | Set<String> notCopyFieldList = new HashSet<>(); |
| 496 | for (int j = 0; j < destProperty.length; j++) { |
| 497 | if (!propertiesList.contains(destProperty[j].getName())) { |
| 498 | notCopyFieldList.add(destProperty[j].getName()); |
| 499 | } |
| 500 | } |
| 501 | if (notCopyFieldList != null && notCopyFieldList.size() > 0) { |
| 502 | String[] strs1 = notCopyFieldList.toArray(new String[notCopyFieldList.size()]); |
| 503 | BeanUtils.copyProperties(source, to, strs1); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * 正则表达式验证 |
nothing calls this directly
no outgoing calls
no test coverage detected