From 42ca239a5868871d580c062fed98c39290cc8f21 Mon Sep 17 00:00:00 2001 From: yl-yue Date: Thu, 1 Apr 2021 16:30:47 +0800 Subject: [PATCH] =?UTF-8?q?1.=20mapToBean()=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=B8=83=E5=B0=94=E5=80=BCis=E5=89=8D=E7=BC=80=E7=9A=84?= =?UTF-8?q?=E8=AF=86=E5=88=AB=202.=20=E6=B7=BB=E5=8A=A0=E7=9B=B8=E5=BA=94?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../copier/provider/MapValueProvider.java | 46 +++++++++++++++---- .../cn/hutool/core/bean/BeanUtilTest.java | 18 +++++--- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/copier/provider/MapValueProvider.java b/hutool-core/src/main/java/cn/hutool/core/bean/copier/provider/MapValueProvider.java index 16c1f3d74..284858fe2 100644 --- a/hutool-core/src/main/java/cn/hutool/core/bean/copier/provider/MapValueProvider.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/copier/provider/MapValueProvider.java @@ -50,23 +50,49 @@ public class MapValueProvider implements ValueProvider { @Override public Object value(String key, Type valueType) { - Object value = map.get(key); - if (null == value) { - //检查下划线模式 - value = map.get(StrUtil.toUnderlineCase(key)); - } - + Object value = map.get(getKey(key, valueType)); return Convert.convertWithCheck(valueType, value, null, this.ignoreError); } @Override public boolean containsKey(String key) { + return map.containsKey(getKey(key, null)); + } + + /** + * 获得map中可能包含的key + * + * @param key map中可能包含的key + * @param valueType 值类型,用于判断是否为Boolean,可以为null + * @return map中可能包含的key + */ + private String getKey(String key, Type valueType) { if (map.containsKey(key)) { - return true; - } else { - //检查下划线模式 - return map.containsKey(StrUtil.toUnderlineCase(key)); + return key; } + + //检查下划线模式 + String containKey = StrUtil.toUnderlineCase(key); + if (map.containsKey(containKey)) { + return containKey; + } + + //检查boolean类型 + if (null == valueType || Boolean.class == valueType || boolean.class == valueType) { + //boolean类型字段字段名支持两种方式 + containKey = StrUtil.upperFirstAndAddPre(key, "is"); + if (map.containsKey(containKey)) { + return containKey; + } + + //检查下划线模式 + containKey = StrUtil.toUnderlineCase(containKey); + if (map.containsKey(containKey)) { + return containKey; + } + } + + return key; } } diff --git a/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java index 209e4dba5..2c3621871 100644 --- a/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/bean/BeanUtilTest.java @@ -10,6 +10,7 @@ import cn.hutool.core.util.StrUtil; import lombok.Data; import lombok.Getter; import lombok.Setter; +import lombok.ToString; import org.junit.Assert; import org.junit.Test; @@ -18,13 +19,7 @@ import java.io.Serializable; import java.lang.reflect.Type; import java.time.LocalDate; import java.time.LocalDateTime; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.UUID; +import java.util.*; /** * Bean工具单元测试 @@ -201,6 +196,8 @@ public class BeanUtilTest { person.setName("测试A11"); person.setSubName("sub名字"); person.setSlow(true); + person.setBooleana(true); + person.setBooleanb(true); Map map = BeanUtil.beanToMap(person); Assert.assertEquals("sub名字", map.get("aliasSubName")); @@ -211,9 +208,13 @@ public class BeanUtilTest { Map map = MapUtil.newHashMap(); map.put("aliasSubName", "sub名字"); map.put("slow", true); + map.put("is_booleana", "1"); + map.put("is_booleanb", true); final SubPersonWithAlias subPersonWithAlias = BeanUtil.toBean(map, SubPersonWithAlias.class); Assert.assertEquals("sub名字", subPersonWithAlias.getSubName()); + Assert.assertTrue(subPersonWithAlias.isBooleana()); + Assert.assertEquals(true, subPersonWithAlias.getBooleanb()); } @Test @@ -360,11 +361,14 @@ public class BeanUtilTest { @Getter @Setter + @ToString public static class SubPersonWithAlias extends Person { // boolean参数值非isXXX形式 @Alias("aliasSubName") private String subName; private Boolean slow; + private boolean booleana; + private Boolean booleanb; } @Getter