mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add null check
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
### 🐣新特性
|
||||
* 【system 】 OshiUtil增加getCurrentProcess方法
|
||||
* 【extra 】 SpringUtil增加getApplicationName、publishEvent方法(issue#I485NZ@Gitee)
|
||||
* 【core 】 BeanUtil.getProperty增加判空(issue#I488HA@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
|
||||
|
@@ -283,18 +283,18 @@ public class BeanUtil {
|
||||
if (bean instanceof Map) {
|
||||
return ((Map<?, ?>) bean).get(fieldNameOrIndex);
|
||||
} else if (bean instanceof Collection) {
|
||||
try{
|
||||
try {
|
||||
return CollUtil.get((Collection<?>) bean, Integer.parseInt(fieldNameOrIndex));
|
||||
} catch (NumberFormatException e){
|
||||
} catch (NumberFormatException e) {
|
||||
// 非数字,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)) {
|
||||
try{
|
||||
try {
|
||||
return ArrayUtil.get(bean, Integer.parseInt(fieldNameOrIndex));
|
||||
} catch (NumberFormatException e){
|
||||
} catch (NumberFormatException e) {
|
||||
// 非数字,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对象
|
||||
return ReflectUtil.getFieldValue(bean, fieldNameOrIndex);
|
||||
@@ -329,12 +329,15 @@ public class BeanUtil {
|
||||
* @param <T> 属性值类型
|
||||
* @param bean Bean对象,支持Map、List、Collection、Array
|
||||
* @param expression 表达式,例如:person.friend[5].name
|
||||
* @return Bean属性值
|
||||
* @return Bean属性值,bean为{@code null}或者express为空,返回{@code null}
|
||||
* @see BeanPath#get(Object)
|
||||
* @since 3.0.7
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getProperty(Object bean, String expression) {
|
||||
if (null == bean || StrUtil.isBlank(expression)) {
|
||||
return null;
|
||||
}
|
||||
return (T) BeanPath.create(expression).get(bean);
|
||||
}
|
||||
|
||||
@@ -546,7 +549,7 @@ public class BeanUtil {
|
||||
* @since 5.2.4
|
||||
*/
|
||||
public static <T> T toBean(Object source, Class<T> clazz, CopyOptions options) {
|
||||
if(null == source){
|
||||
if (null == source) {
|
||||
return null;
|
||||
}
|
||||
final T target = ReflectUtil.newInstanceIfPossible(clazz);
|
||||
@@ -724,14 +727,14 @@ public class BeanUtil {
|
||||
* @return 复制后的List
|
||||
* @since 5.6.4
|
||||
*/
|
||||
public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType, CopyOptions copyOptions){
|
||||
if(null == collection){
|
||||
public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType, CopyOptions copyOptions) {
|
||||
if (null == collection) {
|
||||
return null;
|
||||
}
|
||||
if(collection.isEmpty()){
|
||||
if (collection.isEmpty()) {
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
return collection.stream().map((source)->{
|
||||
return collection.stream().map((source) -> {
|
||||
final T target = ReflectUtil.newInstanceIfPossible(targetType);
|
||||
copyProperties(source, target, copyOptions);
|
||||
return target;
|
||||
@@ -748,7 +751,7 @@ public class BeanUtil {
|
||||
* @return 复制后的List
|
||||
* @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());
|
||||
}
|
||||
|
||||
@@ -764,6 +767,9 @@ public class BeanUtil {
|
||||
* @since 4.0.6
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -777,7 +783,7 @@ public class BeanUtil {
|
||||
* @return bean
|
||||
* @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) {
|
||||
return null;
|
||||
}
|
||||
@@ -803,7 +809,7 @@ public class BeanUtil {
|
||||
* @return 处理后的Bean对象
|
||||
*/
|
||||
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())) {
|
||||
// 不处理忽略的Fields
|
||||
return field;
|
||||
|
@@ -257,6 +257,13 @@ public class BeanUtilTest {
|
||||
Assert.assertEquals("sub名字", subName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public void getNullPropertyTest() {
|
||||
final Object property = BeanUtil.getProperty(null, "name");
|
||||
Assert.assertNull(property);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPropertyDescriptorsTest() {
|
||||
HashSet<Object> set = CollUtil.newHashSet();
|
||||
|
Reference in New Issue
Block a user