MCPcopy Create free account
hub / github.com/Bit0r/java-util / copy

Method copy

src/main/java/util/CommonUtils.java:485–505  ·  view source on GitHub ↗

复制实体类 @param source 被复制的实体类对象 @param to 复制完后的实体类对象 @throws Exception

(Object source, Object to, String... needProperties)

Source from the content-addressed store, hash-verified

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 * 正则表达式验证

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected