From fe21f8ef652103de14622b3f46078a466fa2744d Mon Sep 17 00:00:00 2001 From: chenlw <874313221@qq.com> Date: Fri, 21 Oct 2016 09:01:59 +0800 Subject: [PATCH] =?UTF-8?q?java=20bean=E7=9A=84=E6=8B=B7=E8=B4=9D=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/platform/utils/BeanCopy.java | 80 ++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/com/platform/utils/BeanCopy.java diff --git a/src/com/platform/utils/BeanCopy.java b/src/com/platform/utils/BeanCopy.java new file mode 100644 index 00000000..38ce55a6 --- /dev/null +++ b/src/com/platform/utils/BeanCopy.java @@ -0,0 +1,80 @@ +package com.platform.utils; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.fasterxml.jackson.databind.PropertyNamingStrategy.UpperCamelCaseStrategy; + +public class BeanCopy { + + /** 复制属性(子类父类等继承关系不可用) + * @param source 源对象 + * @param dst 目标对象 + * @param params 不复制的属性 + * @throws Exception + * @throws IllegalArgumentException + * @throws IllegalAccessException + * @throws NoSuchFieldException + * @throws SecurityException + */ + public static void copyBean(Object source, Object dst, String... params) throws Exception{ + if (dst.getClass().getSuperclass() == source.getClass()) { + fatherCopyChild(source, dst, params); + } + List filter = new ArrayList(); + filter.addAll(Arrays.asList(params)); + Field[] sourceField = source.getClass().getDeclaredFields(); + List sourceFieldName = new ArrayList(); + for (Field fie : sourceField) { + sourceFieldName.add(fie.getName()); + } + Field[] dstField = dst.getClass().getDeclaredFields(); + for (Field field : dstField) { + String name = field.getName(); + if (filter.contains(name)) { + continue; + } + if (sourceFieldName.contains(name)) { + field.setAccessible(true); + Object value; +// Method m = source.getClass().getMethod("get"+upperHeadChar(name)); +// value = m.invoke(source); + value = source.getClass().getDeclaredField(name).get(source); + if (null != value) { + field.set(dst, value); + } + } + } + + } + + private static void fatherCopyChild(Object father, Object dst, String... params) throws Exception{ + List filter = new ArrayList(); + filter.addAll(Arrays.asList(params)); + if (dst.getClass().getSuperclass() == father.getClass()) { + Class faClass = father.getClass(); + Field[] ff = faClass.getDeclaredFields(); + for (int i = 0; i < ff.length; i++) { + Field f = ff[i]; + String name = f.getName(); + if (filter.contains(name)) { + continue; + } + f.setAccessible(true); + Class type = f.getType(); + Method m = faClass.getMethod("get"+upperHeadChar(f.getName())); + Object obj = m.invoke(father); + f.set(dst, obj); + } + } + } + + private static String upperHeadChar(String in) { + String head = in.substring(0,1); + return head.toUpperCase()+in.substring(1, in.length()); + } + +}