mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add null check
This commit is contained in:
parent
30f541f849
commit
303c77a08d
@ -8,6 +8,7 @@
|
|||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【system 】 OshiUtil增加getCurrentProcess方法
|
* 【system 】 OshiUtil增加getCurrentProcess方法
|
||||||
* 【extra 】 SpringUtil增加getApplicationName、publishEvent方法(issue#I485NZ@Gitee)
|
* 【extra 】 SpringUtil增加getApplicationName、publishEvent方法(issue#I485NZ@Gitee)
|
||||||
|
* 【core 】 BeanUtil.getProperty增加判空(issue#I488HA@Gitee)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
|
|
||||||
|
@ -329,12 +329,15 @@ public class BeanUtil {
|
|||||||
* @param <T> 属性值类型
|
* @param <T> 属性值类型
|
||||||
* @param bean Bean对象,支持Map、List、Collection、Array
|
* @param bean Bean对象,支持Map、List、Collection、Array
|
||||||
* @param expression 表达式,例如:person.friend[5].name
|
* @param expression 表达式,例如:person.friend[5].name
|
||||||
* @return Bean属性值
|
* @return Bean属性值,bean为{@code null}或者express为空,返回{@code null}
|
||||||
* @see BeanPath#get(Object)
|
* @see BeanPath#get(Object)
|
||||||
* @since 3.0.7
|
* @since 3.0.7
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> T getProperty(Object bean, String expression) {
|
public static <T> T getProperty(Object bean, String expression) {
|
||||||
|
if (null == bean || StrUtil.isBlank(expression)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return (T) BeanPath.create(expression).get(bean);
|
return (T) BeanPath.create(expression).get(bean);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -764,6 +767,9 @@ public class BeanUtil {
|
|||||||
* @since 4.0.6
|
* @since 4.0.6
|
||||||
*/
|
*/
|
||||||
public static boolean isMatchName(Object bean, String beanClassName, boolean isSimple) {
|
public static boolean isMatchName(Object bean, String beanClassName, boolean isSimple) {
|
||||||
|
if (null == bean || StrUtil.isBlank(beanClassName)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return ClassUtil.getClassName(bean, isSimple).equals(isSimple ? StrUtil.upperFirst(beanClassName) : beanClassName);
|
return ClassUtil.getClassName(bean, isSimple).equals(isSimple ? StrUtil.upperFirst(beanClassName) : beanClassName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,6 +257,13 @@ public class BeanUtilTest {
|
|||||||
Assert.assertEquals("sub名字", subName);
|
Assert.assertEquals("sub名字", subName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
|
public void getNullPropertyTest() {
|
||||||
|
final Object property = BeanUtil.getProperty(null, "name");
|
||||||
|
Assert.assertNull(property);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getPropertyDescriptorsTest() {
|
public void getPropertyDescriptorsTest() {
|
||||||
HashSet<Object> set = CollUtil.newHashSet();
|
HashSet<Object> set = CollUtil.newHashSet();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user