fix bug#I3AXIJ

This commit is contained in:
Looly 2021-03-11 17:10:02 +08:00
parent 5cdbd16c0b
commit 511f688d1e
4 changed files with 65 additions and 26 deletions

View File

@ -3,7 +3,7 @@
-------------------------------------------------------------------------------------------------------------
# 5.6.0 (2021-03-10)
# 5.6.0 (2021-03-11)
### 新特性
* 【poi 】 重要不再兼容POI-3.x增加兼容POI-5.xissue#I35J6B@Gitee
@ -15,10 +15,12 @@
* 【core 】 NumberUtil增加factorial针对BigInterger方法issue#1379@Github
* 【core 】 TreeNode增加equals方法issue#1467@Github
* 【core 】 增加汉字转阿拉伯数字Convert.chineseToNumberpr#1469@Github
* 【json 】 JSONUtil增加getByPath方法支持默认值issue#1470@Github
### Bug修复
* 【socket 】 修复Client创建失败资源未释放问题。
* 【core 】 修复DataSizeUtil中EB单位错误问题issue#I39O7I@Gitee
* 【core 】 修复BeanDesc.isMatchSetter的ignoreCase未使用问题issue#I3AXIJ@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -249,12 +249,14 @@ public class BeanDesc implements Serializable {
* @return 是否匹配
*/
private boolean isMatchGetter(String methodName, String fieldName, boolean isBooleanField, boolean ignoreCase) {
// 全部转为小写忽略大小写比较
final String handledFieldName;
if (ignoreCase) {
// 全部转为小写忽略大小写比较
methodName = methodName.toLowerCase();
fieldName = fieldName.toLowerCase();
handledFieldName = fieldName.toLowerCase();
fieldName = handledFieldName;
} else {
fieldName = StrUtil.upperFirst(fieldName);
handledFieldName = StrUtil.upperFirst(fieldName);
}
if (false == methodName.startsWith("get") && false == methodName.startsWith("is")) {
@ -271,12 +273,12 @@ public class BeanDesc implements Serializable {
if (fieldName.startsWith("is")) {
// 字段已经是is开头
if (methodName.equals(fieldName) // isName - isName
|| methodName.equals("get" + fieldName)// isName - getIsName
|| methodName.equals("is" + fieldName)// isName - isIsName
|| methodName.equals("get" + handledFieldName)// isName - getIsName
|| methodName.equals("is" + handledFieldName)// isName - isIsName
) {
return true;
}
} else if (methodName.equals("is" + fieldName)) {
} else if (methodName.equals("is" + handledFieldName)) {
// 字段非is开头 name - isName
return true;
}
@ -304,9 +306,15 @@ public class BeanDesc implements Serializable {
* @return 是否匹配
*/
private boolean isMatchSetter(String methodName, String fieldName, boolean isBooleanField, boolean ignoreCase) {
// 全部转为小写忽略大小写比较
methodName = methodName.toLowerCase();
fieldName = fieldName.toLowerCase();
final String handledFieldName;
if (ignoreCase) {
// 全部转为小写忽略大小写比较
methodName = methodName.toLowerCase();
handledFieldName = fieldName.toLowerCase();
fieldName = handledFieldName;
} else {
handledFieldName = StrUtil.upperFirst(fieldName);
}
// 非标准Setter方法跳过
if (false == methodName.startsWith("set")) {
@ -314,7 +322,7 @@ public class BeanDesc implements Serializable {
}
// 针对Boolean类型特殊检查
if (isBooleanField && fieldName.startsWith("is")) {
if (isBooleanField && handledFieldName.startsWith("is")) {
// 字段是is开头
if (methodName.equals("set" + StrUtil.removePrefix(fieldName, "is"))// isName - setName
|| methodName.equals("set" + fieldName)// isName - setIsName

View File

@ -20,7 +20,7 @@ public class BeanDescTest {
Assert.assertEquals("getAge", desc.getGetter("age").getName());
Assert.assertEquals("setAge", desc.getSetter("age").getName());
Assert.assertEquals(1, desc.getSetter("age").getParameterTypes().length);
Assert.assertEquals(int.class, desc.getSetter("age").getParameterTypes()[0]);
Assert.assertSame(int.class, desc.getSetter("age").getParameterTypes()[0]);
}
@ -33,7 +33,7 @@ public class BeanDescTest {
Assert.assertEquals("getName", prop.getGetter().getName());
Assert.assertEquals("setName", prop.getSetter().getName());
Assert.assertEquals(1, prop.getSetter().getParameterTypes().length);
Assert.assertEquals(String.class, prop.getSetter().getParameterTypes()[0]);
Assert.assertSame(String.class, prop.getSetter().getParameterTypes()[0]);
}
@Test

View File

@ -546,6 +546,35 @@ public class JSONUtil {
return (null == json || StrUtil.isBlank(expression)) ? null : json.getByPath(expression);
}
/**
* 通过表达式获取JSON中嵌套的对象<br>
* <ol>
* <li>.表达式可以获取Bean对象中的属性字段值或者Map中key对应的值</li>
* <li>[]表达式可以获取集合等对象中对应index的值</li>
* </ol>
* <p>
* 表达式栗子
*
* <pre>
* persion
* persion.name
* persons[3]
* person.friends[5].name
* </pre>
*
* @param <T> 值类型
* @param json {@link JSON}
* @param expression 表达式
* @param defaultValue 默认值
* @return 对象
* @see JSON#getByPath(String)
* @since 5.6.0
*/
@SuppressWarnings("unchecked")
public static <T> T getByPath(JSON json, String expression, T defaultValue) {
return (T) ObjectUtil.defaultIfNull(getByPath(json, expression), defaultValue);
}
/**
* 设置表达式指定位置或filed对应的值<br>
* 若表达式指向一个JSONArray则设置其坐标对应位置的值若指向JSONObject则put对应key的值<br>