ClassUtils { public static Object convertPrimitive(Class<?> type, String value) { if (value == null) { return null; } else if (type == char.class || type == Character.class) { return value.length() > 0 ? value.charAt(0) : '\0'; } else if (type == boolean.class || type == Boolean.class) { return Boolean.valueOf(value); } try { if (type == byte.class || type == Byte.class) { return Byte.valueOf(value); } else if (type == short.class || type == Short.class) { return Short.valueOf(value); } else if (type == int.class || type == Integer.class) { return Integer.valueOf(value); } else if (type == long.class || type == Long.class) { return Long.valueOf(value); } else if (type == float.class || type == Float.class) { return Float.valueOf(value); } else if (type == double.class || type == Double.class) { return Double.valueOf(value); } } catch (NumberFormatException e) { return null; } return value; }  static Class<?> forNameWithThreadContextClassLoader(String name); static Class<?> forNameWithCallerClassLoader(String name, Class<?> caller); static ClassLoader getCallerClassLoader(Class<?> caller); static ClassLoader getClassLoader(Class<?> clazz); static ClassLoader getClassLoader(); static Class<?> forName(String name); static Class<?> forName(String name, ClassLoader classLoader); static Class<?> resolvePrimitiveClassName(String name); static String toShortString(Object obj); static String simpleClassName(Class<?> clazz); static boolean isPrimitive(Class<?> type); static boolean isSimpleType(Class<?> type); static Object convertPrimitive(Class<?> type, String value); static boolean isTypeMatch(Class<?> type, String value); static Set<Class<?>> getAllSuperClasses(Class<?> type, Predicate<Class<?>>... classFilters); static Set<Class<?>> getAllInterfaces(Class<?> type, Predicate<Class<?>>... interfaceFilters); static Set<Class<?>> getAllInheritedTypes(Class<?> type, Predicate<Class<?>>... typeFilters); static boolean isAssignableFrom(Class<?> superType, Class<?> targetType); static boolean isPresent(String className, ClassLoader classLoader); static Class<?> resolveClass(String className, ClassLoader classLoader); static boolean isGenericClass(Class<?> type); static final String ARRAY_SUFFIX; static final Set<Class<?>> SIMPLE_TYPES; }
@Test public void testConvertPrimitive() throws Exception { assertThat(ClassUtils.convertPrimitive(char.class, ""), equalTo('\0')); assertThat(ClassUtils.convertPrimitive(char.class, null), equalTo(null)); assertThat(ClassUtils.convertPrimitive(char.class, "6"), equalTo('6')); assertThat(ClassUtils.convertPrimitive(boolean.class, ""), equalTo(Boolean.FALSE)); assertThat(ClassUtils.convertPrimitive(boolean.class, null), equalTo(null)); assertThat(ClassUtils.convertPrimitive(boolean.class, "true"), equalTo(Boolean.TRUE)); assertThat(ClassUtils.convertPrimitive(byte.class, ""), equalTo(null)); assertThat(ClassUtils.convertPrimitive(byte.class, null), equalTo(null)); assertThat(ClassUtils.convertPrimitive(byte.class, "127"), equalTo(Byte.MAX_VALUE)); assertThat(ClassUtils.convertPrimitive(short.class, ""), equalTo(null)); assertThat(ClassUtils.convertPrimitive(short.class, null), equalTo(null)); assertThat(ClassUtils.convertPrimitive(short.class, "32767"), equalTo(Short.MAX_VALUE)); assertThat(ClassUtils.convertPrimitive(int.class, ""), equalTo(null)); assertThat(ClassUtils.convertPrimitive(int.class, null), equalTo(null)); assertThat(ClassUtils.convertPrimitive(int.class, "6"), equalTo(6)); assertThat(ClassUtils.convertPrimitive(long.class, ""), equalTo(null)); assertThat(ClassUtils.convertPrimitive(long.class, null), equalTo(null)); assertThat(ClassUtils.convertPrimitive(long.class, "6"), equalTo(new Long(6))); assertThat(ClassUtils.convertPrimitive(float.class, ""), equalTo(null)); assertThat(ClassUtils.convertPrimitive(float.class, null), equalTo(null)); assertThat(ClassUtils.convertPrimitive(float.class, "1.1"), equalTo(new Float(1.1))); assertThat(ClassUtils.convertPrimitive(double.class, ""), equalTo(null)); assertThat(ClassUtils.convertPrimitive(double.class, null), equalTo(null)); assertThat(ClassUtils.convertPrimitive(double.class, "10.1"), equalTo(new Double(10.1))); }