mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix beanUtil
This commit is contained in:
parent
a98f518c6f
commit
c8f911ad4b
@ -1,15 +1,16 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
import cn.hutool.core.lang.SimpleCache;
|
||||
import cn.hutool.core.lang.func.Func0;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.util.Map;
|
||||
|
||||
import cn.hutool.core.lang.SimpleCache;
|
||||
|
||||
/**
|
||||
* Bean属性缓存<br>
|
||||
* 缓存用于防止多次反射造成的性能问题
|
||||
* @author Looly
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public enum BeanInfoCache {
|
||||
INSTANCE;
|
||||
@ -19,21 +20,50 @@ public enum BeanInfoCache {
|
||||
|
||||
/**
|
||||
* 获得属性名和{@link PropertyDescriptor}Map映射
|
||||
*
|
||||
* @param beanClass Bean的类
|
||||
* @param ignoreCase 是否忽略大小写
|
||||
* @return 属性名和{@link PropertyDescriptor}Map映射
|
||||
*/
|
||||
public Map<String, PropertyDescriptor> getPropertyDescriptorMap(Class<?> beanClass, boolean ignoreCase) {
|
||||
return (ignoreCase ? ignoreCasePdCache : pdCache).get(beanClass);
|
||||
return getCache(ignoreCase).get(beanClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得属性名和{@link PropertyDescriptor}Map映射
|
||||
*
|
||||
* @param beanClass Bean的类
|
||||
* @param ignoreCase 是否忽略大小写
|
||||
* @param supplier 缓存对象产生函数
|
||||
* @return 属性名和{@link PropertyDescriptor}Map映射
|
||||
* @since 5.4.1
|
||||
*/
|
||||
public Map<String, PropertyDescriptor> getPropertyDescriptorMap(
|
||||
Class<?> beanClass,
|
||||
boolean ignoreCase,
|
||||
Func0<Map<String, PropertyDescriptor>> supplier) {
|
||||
return getCache(ignoreCase).get(beanClass, supplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入缓存
|
||||
*
|
||||
* @param beanClass Bean的类
|
||||
* @param fieldNamePropertyDescriptorMap 属性名和{@link PropertyDescriptor}Map映射
|
||||
* @param ignoreCase 是否忽略大小写
|
||||
*/
|
||||
public void putPropertyDescriptorMap(Class<?> beanClass, Map<String, PropertyDescriptor> fieldNamePropertyDescriptorMap, boolean ignoreCase) {
|
||||
(ignoreCase ? ignoreCasePdCache : pdCache).put(beanClass, fieldNamePropertyDescriptorMap);
|
||||
getCache(ignoreCase).put(beanClass, fieldNamePropertyDescriptorMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据是否忽略字段名的大小写,返回不用Cache对象
|
||||
*
|
||||
* @param ignoreCase 是否忽略大小写
|
||||
* @return SimpleCache
|
||||
* @since 5.4.1
|
||||
*/
|
||||
private SimpleCache<Class<?>, Map<String, PropertyDescriptor>> getCache(boolean ignoreCase) {
|
||||
return ignoreCase ? ignoreCasePdCache : pdCache;
|
||||
}
|
||||
}
|
||||
|
@ -205,12 +205,7 @@ public class BeanUtil {
|
||||
* @throws BeanException 获取属性异常
|
||||
*/
|
||||
public static Map<String, PropertyDescriptor> getPropertyDescriptorMap(Class<?> clazz, boolean ignoreCase) throws BeanException {
|
||||
Map<String, PropertyDescriptor> map = BeanInfoCache.INSTANCE.getPropertyDescriptorMap(clazz, ignoreCase);
|
||||
if (null == map) {
|
||||
map = internalGetPropertyDescriptorMap(clazz, ignoreCase);
|
||||
BeanInfoCache.INSTANCE.putPropertyDescriptorMap(clazz, map, ignoreCase);
|
||||
}
|
||||
return map;
|
||||
return BeanInfoCache.INSTANCE.getPropertyDescriptorMap(clazz, ignoreCase, ()-> internalGetPropertyDescriptorMap(clazz, ignoreCase));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user