去除ClassloaderUtil的Cache

This commit is contained in:
Looly 2022-11-08 19:09:46 +08:00
parent 4b2ec7d7a5
commit ecc4cd64eb
3 changed files with 7 additions and 11 deletions

View File

@ -3,12 +3,13 @@
-------------------------------------------------------------------------------------------------------------
# 5.8.10.M1 (2022-11-03)
# 5.8.10.M1 (2022-11-08)
### 🐣新特性
* 【http 】 HttpResponse增加getFileNameFromDisposition方法pr#2676@Github
* 【core 】 FileUtil.copy当来源为文件时返回文件而非目录issue#I5YCVL@Gitee
* 【db 】 DialectFactory增加identifyDriver重载issue#I5YWI6@Gitee
* 【core 】 去除ClassloaderUtil的Cacheissue#I5YWI6@Gitee
### 🐞Bug修复
* 【db 】 修复分页时order by截断问题issue#I5X6FM@Gitee

View File

@ -4,9 +4,7 @@ import cn.hutool.core.convert.BasicType;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.JarClassLoader;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.map.SafeConcurrentHashMap;
import cn.hutool.core.map.WeakConcurrentMap;
import cn.hutool.core.text.CharPool;
import java.io.File;
@ -50,10 +48,9 @@ public class ClassLoaderUtil {
* 原始类型名和其class对应表例如int = int.class
*/
private static final Map<String, Class<?>> PRIMITIVE_TYPE_NAME_MAP = new SafeConcurrentHashMap<>(32);
private static final Map<Pair<String, ClassLoader>, Class<?>> CLASS_CACHE = new WeakConcurrentMap<>();
static {
List<Class<?>> primitiveTypes = new ArrayList<>(32);
final List<Class<?>> primitiveTypes = new ArrayList<>(32);
// 加入原始类型
primitiveTypes.addAll(BasicType.PRIMITIVE_WRAPPER_MAP.keySet());
// 加入原始类型数组类型
@ -66,7 +63,7 @@ public class ClassLoaderUtil {
primitiveTypes.add(long[].class);
primitiveTypes.add(short[].class);
primitiveTypes.add(void.class);
for (Class<?> primitiveType : primitiveTypes) {
for (final Class<?> primitiveType : primitiveTypes) {
PRIMITIVE_TYPE_NAME_MAP.put(primitiveType.getName(), primitiveType);
}
}
@ -197,9 +194,7 @@ public class ClassLoaderUtil {
// 加载原始类型和缓存中的类
Class<?> clazz = loadPrimitiveClass(name);
if (clazz == null) {
final String finalName = name;
final ClassLoader finalClassLoader = classLoader;
clazz = CLASS_CACHE.computeIfAbsent(Pair.of(name, classLoader), (key)-> doLoadClass(finalName, finalClassLoader, isInitialized));
clazz = doLoadClass(name, classLoader, isInitialized);
}
return clazz;
}

View File

@ -4,12 +4,12 @@ import org.junit.Assert;
import org.junit.Test;
public class ClassLoaderUtilTest {
@Test
public void loadClassTest() {
String name = ClassLoaderUtil.loadClass("java.lang.Thread.State").getName();
Assert.assertEquals("java.lang.Thread$State", name);
name = ClassLoaderUtil.loadClass("java.lang.Thread$State").getName();
Assert.assertEquals("java.lang.Thread$State", name);
}