Skip to content

Commit

Permalink
replace cache map with thread-safe implemention. Fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
qdaxb committed Apr 25, 2016
1 parent f1608ca commit e494965
Showing 1 changed file with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ReflectUtil {
public static final String EMPTY_PARAM = "void";
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];

private static final Map<String, Class<?>> name2ClassCache = new HashMap<String, Class<?>>();
private static final ConcurrentMap<String, Class<?>> name2ClassCache = new ConcurrentHashMap<String, Class<?>>();
private static final ConcurrentMap<Class<?>, String> class2NameCache = new ConcurrentHashMap<Class<?>, String>();

private static final String[] PRIMITIVE_NAMES = new String[] {"boolean", "byte", "char", "double", "float", "int", "long", "short",
Expand Down Expand Up @@ -129,10 +129,8 @@ public static Class<?> forName(String className) throws ClassNotFoundException {

clz = forNameWithoutCache(className);

if (clz != null) {
// 应该没有内存消耗过多的可能,除非有些代码很恶心,创建特别多的类
name2ClassCache.put(className, clz);
}
// 应该没有内存消耗过多的可能,除非有些代码很恶心,创建特别多的类
name2ClassCache.putIfAbsent(className, clz);

return clz;
}
Expand Down Expand Up @@ -308,4 +306,5 @@ private static Object getEmptyObject(Class<?> returnType, Map<Class<?>, Object>
return null;
}
}

}

0 comments on commit e494965

Please sign in to comment.