mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
去除ClassloaderUtil的Cache
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user