Kaynağa Gözat

优化,增加include功能

zhouhao 7 yıl önce
ebeveyn
işleme
9403fcd1b8

+ 32 - 14
hsweb-core/src/main/java/org/hswebframework/web/bean/FastBeanCopier.java

@@ -34,6 +34,7 @@ public final class FastBeanCopier {
 
     private static final Map<Class, Class> wrapperClassMapping = new HashMap<>();
 
+    @SuppressWarnings("all")
     public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
 
     private static BeanFactory BEAN_FACTORY = new BeanFactory() {
@@ -75,6 +76,16 @@ public final class FastBeanCopier {
         DEFAULT_CONVERT.setBeanFactory(BEAN_FACTORY);
     }
 
+    @SuppressWarnings("all")
+    public static Set<String> include(String... inculdeProperties) {
+        return new HashSet<String>(Arrays.asList(inculdeProperties)) {
+            @Override
+            public boolean contains(Object o) {
+                return !super.contains(o);
+            }
+        };
+    }
+
     public static <T, S> T copy(S source, T target, String... ignore) {
         return copy(source, target, DEFAULT_CONVERT, ignore);
     }
@@ -83,12 +94,29 @@ public final class FastBeanCopier {
         return copy(source, target.get(), DEFAULT_CONVERT, ignore);
     }
 
+    @SneakyThrows
     public static <T, S> T copy(S source, Class<T> target, String... ignore) {
-        try {
-            return copy(source, target.newInstance(), DEFAULT_CONVERT, ignore);
-        } catch (Exception e) {
-            throw new RuntimeException(e.getMessage(), e);
+        return copy(source, target.newInstance(), DEFAULT_CONVERT, ignore);
+    }
+
+    public static <T, S> T copy(S source, T target, Converter converter, String... ignore) {
+        return copy(source, target, converter, (ignore == null || ignore.length == 0) ? Collections.emptySet() : new HashSet<>(Arrays.asList(ignore)));
+    }
+
+    public static <T, S> T copy(S source, T target, Set<String> ignore) {
+        return copy(source, target, DEFAULT_CONVERT, ignore);
+    }
+
+    @SuppressWarnings("all")
+    public static <T, S> T copy(S source, T target, Converter converter, Set<String> ignore) {
+        if (source instanceof Map && target instanceof Map) {
+            ((Map) target).putAll(((Map) source));
+            return target;
         }
+
+        getCopier(source, target, true)
+                .copy(source, target, ignore, converter);
+        return target;
     }
 
     public static Copier getCopier(Object source, Object target, boolean autoCreate) {
@@ -103,16 +131,6 @@ public final class FastBeanCopier {
 
     }
 
-    public static <T, S> T copy(S source, T target, Converter converter, String... ignore) {
-        if (source instanceof Map && target instanceof Map) {
-            ((Map) target).putAll(((Map) source));
-            return target;
-        }
-        getCopier(source, target, true)
-                .copy(source, target, (ignore == null || ignore.length == 0) ? Collections.emptySet() : new HashSet<>(Arrays.asList(ignore)), converter);
-        return target;
-    }
-
     private static CacheKey createCacheKey(Class source, Class target) {
         return new CacheKey(source, target);
     }

+ 7 - 2
hsweb-core/src/test/java/org/hswebframework/web/bean/FastBeanCopierTest.java

@@ -23,7 +23,7 @@ public class FastBeanCopierTest {
         source.setAge2(2);
         source.setBoy2(true);
         source.setColor(Color.RED);
-        source.setNestObject2(Collections.singletonMap("name","mapTest"));
+        source.setNestObject2(Collections.singletonMap("name", "mapTest"));
         NestObject nestObject = new NestObject();
         nestObject.setAge(10);
         nestObject.setPassword("1234567");
@@ -67,6 +67,8 @@ public class FastBeanCopierTest {
 
     @Test
     public void testCopyMap() {
+
+
         Source source = new Source();
         source.setAge(100);
         source.setName("测试");
@@ -75,9 +77,12 @@ public class FastBeanCopierTest {
         nestObject.setAge(10);
         nestObject.setName("测试2");
 //        source.setNestObject(nestObject);
+
+
         Map<String, Object> target = new HashMap<>();
 
-        FastBeanCopier.copy(source, target);
+
+        System.out.println(FastBeanCopier.copy(source, target, FastBeanCopier.include("age")));
 
         System.out.println(target);
         System.out.println(FastBeanCopier.copy(target, new Source()));