一段值得回味的代码 博客分类: 开源jar包学习 经典
程序员文章站
2024-03-14 08:18:04
...
这个代码应该是每一个开源框架的ClassUtils都有的吧!本人觉得这段代码和经典,但又说不出哪里好,只可意会,不可言谈。
public class ClassUtils{ /** * Map with primitive wrapper type as key and corresponding primitive * type as value, for example: Integer.class -> int.class. */ private static final Map primitiveWrapperTypeMap = new HashMap(8); /** * Map with primitive type name as key and corresponding primitive * type as value, for example: "int" -> "int.class". */ private static final Map primitiveTypeNameMap = new HashMap(16); static { primitiveWrapperTypeMap.put(Boolean.class, boolean.class); primitiveWrapperTypeMap.put(Byte.class, byte.class); primitiveWrapperTypeMap.put(Character.class, char.class); primitiveWrapperTypeMap.put(Double.class, double.class); primitiveWrapperTypeMap.put(Float.class, float.class); primitiveWrapperTypeMap.put(Integer.class, int.class); primitiveWrapperTypeMap.put(Long.class, long.class); primitiveWrapperTypeMap.put(Short.class, short.class); Set primitiveTypeNames = new HashSet(16); primitiveTypeNames.addAll(primitiveWrapperTypeMap.values()); primitiveTypeNames.addAll(Arrays.asList(new Class[] { boolean[].class, byte[].class, char[].class, double[].class, float[].class, int[].class, long[].class, short[].class})); for (Iterator it = primitiveTypeNames.iterator(); it.hasNext();) { Class primitiveClass = (Class) it.next(); primitiveTypeNameMap.put(primitiveClass.getName(), primitiveClass); } } }