From 52e74f9e447142e74b319851d73671a7937f1381 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Mon, 4 Dec 2023 21:35:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[Improve]=20=E4=BF=AE=E6=94=B9BeanUtil.copy?= =?UTF-8?q?Properties=E8=BF=94=E5=9B=9E=E5=80=BC=E4=B8=BAtarget?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/hutool/core/bean/BeanUtil.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/bean/BeanUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/bean/BeanUtil.java index 2c499aa20..e29286152 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/bean/BeanUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/bean/BeanUtil.java @@ -680,8 +680,7 @@ public class BeanUtil { return (T) RecordConverter.INSTANCE.convert(tClass, source); } final T target = ConstructorUtil.newInstanceIfPossible(tClass); - copyProperties(source, target, CopyOptions.of().setIgnoreProperties(ignoreProperties)); - return target; + return copyProperties(source, target, CopyOptions.of().setIgnoreProperties(ignoreProperties)); } /** @@ -692,8 +691,8 @@ public class BeanUtil { * @param target 目标Bean对象 * @param ignoreProperties 不拷贝的的属性列表 */ - public static void copyProperties(final Object source, final Object target, final String... ignoreProperties) { - copyProperties(source, target, CopyOptions.of().setIgnoreProperties(ignoreProperties)); + public static T copyProperties(final Object source, final T target, final String... ignoreProperties) { + return copyProperties(source, target, CopyOptions.of().setIgnoreProperties(ignoreProperties)); } /** @@ -703,8 +702,8 @@ public class BeanUtil { * @param target 目标Bean对象 * @param ignoreCase 是否忽略大小写 */ - public static void copyProperties(final Object source, final Object target, final boolean ignoreCase) { - BeanCopier.of(source, target, CopyOptions.of().setIgnoreCase(ignoreCase)).copy(); + public static T copyProperties(final Object source, final T target, final boolean ignoreCase) { + return BeanCopier.of(source, target, CopyOptions.of().setIgnoreCase(ignoreCase)).copy(); } /** @@ -715,11 +714,11 @@ public class BeanUtil { * @param target 目标Bean对象 * @param copyOptions 拷贝选项,见 {@link CopyOptions} */ - public static void copyProperties(final Object source, final Object target, final CopyOptions copyOptions) { + public static T copyProperties(final Object source, final T target, final CopyOptions copyOptions) { if (null == source || null == target) { - return; + return null; } - BeanCopier.of(source, target, ObjUtil.defaultIfNull(copyOptions, CopyOptions::of)).copy(); + return BeanCopier.of(source, target, ObjUtil.defaultIfNull(copyOptions, CopyOptions::of)).copy(); } /** From 945afcc0dfa038a2dd140072409ee576187feb3b Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 4 Dec 2023 23:49:07 +0800 Subject: [PATCH 2/2] fix test --- .../test/java/org/dromara/hutool/db/driver/DriverUtilTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hutool-db/src/test/java/org/dromara/hutool/db/driver/DriverUtilTest.java b/hutool-db/src/test/java/org/dromara/hutool/db/driver/DriverUtilTest.java index 1d38d567f..296b42835 100644 --- a/hutool-db/src/test/java/org/dromara/hutool/db/driver/DriverUtilTest.java +++ b/hutool-db/src/test/java/org/dromara/hutool/db/driver/DriverUtilTest.java @@ -13,7 +13,6 @@ package org.dromara.hutool.db.driver; import org.dromara.hutool.core.util.RandomUtil; -import org.dromara.hutool.db.driver.DriverUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -25,7 +24,7 @@ public class DriverUtilTest { @Test public void identifyH2DriverTest(){ final String url = "jdbc:h2:file:./db/test;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL"; - final String driver = DriverUtil.identifyDriver(url); // driver 返回 mysql 的 driver + final String driver = DriverUtil.identifyDriver(url); // driver 返回 h2 的 driver Assertions.assertEquals("org.h2.Driver", driver); }