add null check

This commit is contained in:
Looly 2021-09-01 22:14:15 +08:00
parent 30f541f849
commit 303c77a08d
3 changed files with 37 additions and 23 deletions

View File

@ -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修复

View File

@ -275,7 +275,7 @@ public class BeanUtil {
* @param fieldNameOrIndex 字段名或序号序号支持负数 * @param fieldNameOrIndex 字段名或序号序号支持负数
* @return 字段值 * @return 字段值
*/ */
public static Object getFieldValue(Object bean, String fieldNameOrIndex) { public static Object getFieldValue(Object bean, String fieldNameOrIndex) {
if (null == bean || null == fieldNameOrIndex) { if (null == bean || null == fieldNameOrIndex) {
return null; return null;
} }
@ -283,18 +283,18 @@ public class BeanUtil {
if (bean instanceof Map) { if (bean instanceof Map) {
return ((Map<?, ?>) bean).get(fieldNameOrIndex); return ((Map<?, ?>) bean).get(fieldNameOrIndex);
} else if (bean instanceof Collection) { } else if (bean instanceof Collection) {
try{ try {
return CollUtil.get((Collection<?>) bean, Integer.parseInt(fieldNameOrIndex)); return CollUtil.get((Collection<?>) bean, Integer.parseInt(fieldNameOrIndex));
} catch (NumberFormatException e){ } catch (NumberFormatException e) {
// 非数字see pr#254@Gitee // 非数字see pr#254@Gitee
return CollUtil.map((Collection<?>) bean, (beanEle)-> getFieldValue(beanEle, fieldNameOrIndex), false); return CollUtil.map((Collection<?>) bean, (beanEle) -> getFieldValue(beanEle, fieldNameOrIndex), false);
} }
} else if (ArrayUtil.isArray(bean)) { } else if (ArrayUtil.isArray(bean)) {
try{ try {
return ArrayUtil.get(bean, Integer.parseInt(fieldNameOrIndex)); return ArrayUtil.get(bean, Integer.parseInt(fieldNameOrIndex));
} catch (NumberFormatException e){ } catch (NumberFormatException e) {
// 非数字see pr#254@Gitee // 非数字see pr#254@Gitee
return ArrayUtil.map(bean, Object.class, (beanEle)-> getFieldValue(beanEle, fieldNameOrIndex)); return ArrayUtil.map(bean, Object.class, (beanEle) -> getFieldValue(beanEle, fieldNameOrIndex));
} }
} else {// 普通Bean对象 } else {// 普通Bean对象
return ReflectUtil.getFieldValue(bean, fieldNameOrIndex); return ReflectUtil.getFieldValue(bean, fieldNameOrIndex);
@ -329,12 +329,15 @@ public class BeanUtil {
* @param <T> 属性值类型 * @param <T> 属性值类型
* @param bean Bean对象支持MapListCollectionArray * @param bean Bean对象支持MapListCollectionArray
* @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);
} }
@ -546,7 +549,7 @@ public class BeanUtil {
* @since 5.2.4 * @since 5.2.4
*/ */
public static <T> T toBean(Object source, Class<T> clazz, CopyOptions options) { public static <T> T toBean(Object source, Class<T> clazz, CopyOptions options) {
if(null == source){ if (null == source) {
return null; return null;
} }
final T target = ReflectUtil.newInstanceIfPossible(clazz); final T target = ReflectUtil.newInstanceIfPossible(clazz);
@ -717,21 +720,21 @@ public class BeanUtil {
* 复制集合中的Bean属性<br> * 复制集合中的Bean属性<br>
* 此方法遍历集合中每个Bean复制其属性后加入一个新的{@link List} * 此方法遍历集合中每个Bean复制其属性后加入一个新的{@link List}
* *
* @param collection 原Bean集合 * @param collection 原Bean集合
* @param targetType 目标Bean类型 * @param targetType 目标Bean类型
* @param copyOptions 拷贝选项 * @param copyOptions 拷贝选项
* @param <T> Bean类型 * @param <T> Bean类型
* @return 复制后的List * @return 复制后的List
* @since 5.6.4 * @since 5.6.4
*/ */
public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType, CopyOptions copyOptions){ public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType, CopyOptions copyOptions) {
if(null == collection){ if (null == collection) {
return null; return null;
} }
if(collection.isEmpty()){ if (collection.isEmpty()) {
return new ArrayList<>(0); return new ArrayList<>(0);
} }
return collection.stream().map((source)->{ return collection.stream().map((source) -> {
final T target = ReflectUtil.newInstanceIfPossible(targetType); final T target = ReflectUtil.newInstanceIfPossible(targetType);
copyProperties(source, target, copyOptions); copyProperties(source, target, copyOptions);
return target; return target;
@ -744,11 +747,11 @@ public class BeanUtil {
* *
* @param collection 原Bean集合 * @param collection 原Bean集合
* @param targetType 目标Bean类型 * @param targetType 目标Bean类型
* @param <T> Bean类型 * @param <T> Bean类型
* @return 复制后的List * @return 复制后的List
* @since 5.6.6 * @since 5.6.6
*/ */
public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType){ public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType) {
return copyToList(collection, targetType, CopyOptions.create()); return copyToList(collection, targetType, CopyOptions.create());
} }
@ -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);
} }
@ -771,13 +777,13 @@ public class BeanUtil {
* 编辑Bean的字段static字段不会处理<br> * 编辑Bean的字段static字段不会处理<br>
* 例如需要对指定的字段做判空操作null转""操作等等 * 例如需要对指定的字段做判空操作null转""操作等等
* *
* @param bean bean * @param bean bean
* @param editor 编辑器函数 * @param editor 编辑器函数
* @param <T> 被编辑的Bean类型 * @param <T> 被编辑的Bean类型
* @return bean * @return bean
* @since 5.6.4 * @since 5.6.4
*/ */
public static <T> T edit(T bean, Editor<Field> editor){ public static <T> T edit(T bean, Editor<Field> editor) {
if (bean == null) { if (bean == null) {
return null; return null;
} }
@ -803,7 +809,7 @@ public class BeanUtil {
* @return 处理后的Bean对象 * @return 处理后的Bean对象
*/ */
public static <T> T trimStrFields(T bean, String... ignoreFields) { public static <T> T trimStrFields(T bean, String... ignoreFields) {
return edit(bean, (field)->{ return edit(bean, (field) -> {
if (ignoreFields != null && ArrayUtil.containsIgnoreCase(ignoreFields, field.getName())) { if (ignoreFields != null && ArrayUtil.containsIgnoreCase(ignoreFields, field.getName())) {
// 不处理忽略的Fields // 不处理忽略的Fields
return field; return field;
@ -844,7 +850,7 @@ public class BeanUtil {
* @return 是否为空{@code true} - / {@code false} - 非空 * @return 是否为空{@code true} - / {@code false} - 非空
* @since 4.1.10 * @since 4.1.10
*/ */
public static boolean isEmpty(Object bean, String... ignoreFiledNames) { public static boolean isEmpty(Object bean, String... ignoreFiledNames) {
if (null != bean) { if (null != bean) {
for (Field field : ReflectUtil.getFields(bean.getClass())) { for (Field field : ReflectUtil.getFields(bean.getClass())) {
if (ModifierUtil.isStatic(field)) { if (ModifierUtil.isStatic(field)) {

View File

@ -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();