@Test public void testStringToSimpleObject() { BeanConvertContext ctx = new BeanConvertContext(); assertEquals((Byte)(byte)1, ClassUtil.stringToSimpleObject("1", Byte.class, ctx)); assertEquals((Integer)1, ClassUtil.stringToSimpleObject("1", Integer.class, ctx)); assertEquals((Long)1L, ClassUtil.stringToSimpleObject("1", Long.class, ctx)); assertEquals((Double) 1.1d, ClassUtil.stringToSimpleObject("1.1", Double.class, ctx)); assertEquals(Boolean.TRUE, ClassUtil.stringToSimpleObject("true", Boolean.class, ctx)); assertEquals((Character)'a', ClassUtil.stringToSimpleObject("a", Character.class, ctx)); assertEquals(TimeUnit.SECONDS, ClassUtil.stringToSimpleObject("SECONDS", TimeUnit.class, ctx)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T stringToSimpleObject(String str, Class<T> cls, BeanConvertContext ctx) { if(str == null) throw new NullPointerException(); if(cls == String.class) return (T)str; str = str.trim(); if(cls == boolean.class || cls == Boolean.class) return (T)Boolean.valueOf(str); if(cls == byte.class || cls == Byte.class) return (T)Byte.valueOf(str); if(cls == int.class || cls == Integer.class) return (T)Integer.valueOf(str); if(cls == long.class || cls == Long.class) return (T)Long.valueOf(str); if(cls == float.class || cls == Float.class) return (T)Float.valueOf(str); if(cls == double.class || cls == Double.class) return (T)Double.valueOf(str); if(cls == char.class || cls == Character.class) return (T)Character.valueOf(str.charAt(0)); if(Enum.class.isAssignableFrom(cls)){ return (T)stringToEnum((Class)cls, str); } if(Date.class.isAssignableFrom(cls)) try{ return (T)new SimpleDateFormat(ctx.dateFormat).parse(str); }catch(ParseException e){ throw new RuntimeException(e); } return null; }
ClassUtil { @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T stringToSimpleObject(String str, Class<T> cls, BeanConvertContext ctx) { if(str == null) throw new NullPointerException(); if(cls == String.class) return (T)str; str = str.trim(); if(cls == boolean.class || cls == Boolean.class) return (T)Boolean.valueOf(str); if(cls == byte.class || cls == Byte.class) return (T)Byte.valueOf(str); if(cls == int.class || cls == Integer.class) return (T)Integer.valueOf(str); if(cls == long.class || cls == Long.class) return (T)Long.valueOf(str); if(cls == float.class || cls == Float.class) return (T)Float.valueOf(str); if(cls == double.class || cls == Double.class) return (T)Double.valueOf(str); if(cls == char.class || cls == Character.class) return (T)Character.valueOf(str.charAt(0)); if(Enum.class.isAssignableFrom(cls)){ return (T)stringToEnum((Class)cls, str); } if(Date.class.isAssignableFrom(cls)) try{ return (T)new SimpleDateFormat(ctx.dateFormat).parse(str); }catch(ParseException e){ throw new RuntimeException(e); } return null; } }
ClassUtil { @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T stringToSimpleObject(String str, Class<T> cls, BeanConvertContext ctx) { if(str == null) throw new NullPointerException(); if(cls == String.class) return (T)str; str = str.trim(); if(cls == boolean.class || cls == Boolean.class) return (T)Boolean.valueOf(str); if(cls == byte.class || cls == Byte.class) return (T)Byte.valueOf(str); if(cls == int.class || cls == Integer.class) return (T)Integer.valueOf(str); if(cls == long.class || cls == Long.class) return (T)Long.valueOf(str); if(cls == float.class || cls == Float.class) return (T)Float.valueOf(str); if(cls == double.class || cls == Double.class) return (T)Double.valueOf(str); if(cls == char.class || cls == Character.class) return (T)Character.valueOf(str.charAt(0)); if(Enum.class.isAssignableFrom(cls)){ return (T)stringToEnum((Class)cls, str); } if(Date.class.isAssignableFrom(cls)) try{ return (T)new SimpleDateFormat(ctx.dateFormat).parse(str); }catch(ParseException e){ throw new RuntimeException(e); } return null; }  }
ClassUtil { @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T stringToSimpleObject(String str, Class<T> cls, BeanConvertContext ctx) { if(str == null) throw new NullPointerException(); if(cls == String.class) return (T)str; str = str.trim(); if(cls == boolean.class || cls == Boolean.class) return (T)Boolean.valueOf(str); if(cls == byte.class || cls == Byte.class) return (T)Byte.valueOf(str); if(cls == int.class || cls == Integer.class) return (T)Integer.valueOf(str); if(cls == long.class || cls == Long.class) return (T)Long.valueOf(str); if(cls == float.class || cls == Float.class) return (T)Float.valueOf(str); if(cls == double.class || cls == Double.class) return (T)Double.valueOf(str); if(cls == char.class || cls == Character.class) return (T)Character.valueOf(str.charAt(0)); if(Enum.class.isAssignableFrom(cls)){ return (T)stringToEnum((Class)cls, str); } if(Date.class.isAssignableFrom(cls)) try{ return (T)new SimpleDateFormat(ctx.dateFormat).parse(str); }catch(ParseException e){ throw new RuntimeException(e); } return null; }  @Deprecated //Used only for testing, don't use in production code @SneakyThrows static void setStaticPrivateField(String className, String fieldName, Object value); @Deprecated //Used only for testing, don't use in production code static void setPrivateField(Object obj, String fieldName, Object value); @Deprecated //Used only for testing, don't use in production code @SneakyThrows static void setPrivateField(Class<?> cls, Object obj, String fieldName, Object value); static Field getDeclaredField(Class<?> cls, String name); static Map<String, BeanProperty> getProperties(Class<?> cls); static Object getObjectByPath(String path); static Object getObjectByPath(Class<?> cls, Object obj, List<String> relativePath); static Object getObjectProperty(Class<?> cls, Object obj, String propertyName); static Class<?> getGenericClass(Type type); static Type[] getGenericTypeActualParams(Type type); static Type[] getGenericTypeActualParamsForInterface(Type type, Class<?> intf); static List<Class<?>> getAllInterface(Class<?> cls); static Class<?> findInterface(Class<?> cls, Class<?> itf); static Field[] getAllFields(Class<?> cls); static Method[] getAllMethods(Class<?> cls); static T stringToEnum(Class<T> cls, String str); @SuppressWarnings({ "unchecked", "rawtypes" }) static T stringToSimpleObject(String str, Class<T> cls, BeanConvertContext ctx); static boolean isSimpleType(Class<?> cls); static Object objectToSimpleType(Object obj, Class<?> cls); static Type getType(String typeName); static StackTraceElement findCallerStackTrace(Class<?> calleeClass); }
ClassUtil { @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T stringToSimpleObject(String str, Class<T> cls, BeanConvertContext ctx) { if(str == null) throw new NullPointerException(); if(cls == String.class) return (T)str; str = str.trim(); if(cls == boolean.class || cls == Boolean.class) return (T)Boolean.valueOf(str); if(cls == byte.class || cls == Byte.class) return (T)Byte.valueOf(str); if(cls == int.class || cls == Integer.class) return (T)Integer.valueOf(str); if(cls == long.class || cls == Long.class) return (T)Long.valueOf(str); if(cls == float.class || cls == Float.class) return (T)Float.valueOf(str); if(cls == double.class || cls == Double.class) return (T)Double.valueOf(str); if(cls == char.class || cls == Character.class) return (T)Character.valueOf(str.charAt(0)); if(Enum.class.isAssignableFrom(cls)){ return (T)stringToEnum((Class)cls, str); } if(Date.class.isAssignableFrom(cls)) try{ return (T)new SimpleDateFormat(ctx.dateFormat).parse(str); }catch(ParseException e){ throw new RuntimeException(e); } return null; }  @Deprecated //Used only for testing, don't use in production code @SneakyThrows static void setStaticPrivateField(String className, String fieldName, Object value); @Deprecated //Used only for testing, don't use in production code static void setPrivateField(Object obj, String fieldName, Object value); @Deprecated //Used only for testing, don't use in production code @SneakyThrows static void setPrivateField(Class<?> cls, Object obj, String fieldName, Object value); static Field getDeclaredField(Class<?> cls, String name); static Map<String, BeanProperty> getProperties(Class<?> cls); static Object getObjectByPath(String path); static Object getObjectByPath(Class<?> cls, Object obj, List<String> relativePath); static Object getObjectProperty(Class<?> cls, Object obj, String propertyName); static Class<?> getGenericClass(Type type); static Type[] getGenericTypeActualParams(Type type); static Type[] getGenericTypeActualParamsForInterface(Type type, Class<?> intf); static List<Class<?>> getAllInterface(Class<?> cls); static Class<?> findInterface(Class<?> cls, Class<?> itf); static Field[] getAllFields(Class<?> cls); static Method[] getAllMethods(Class<?> cls); static T stringToEnum(Class<T> cls, String str); @SuppressWarnings({ "unchecked", "rawtypes" }) static T stringToSimpleObject(String str, Class<T> cls, BeanConvertContext ctx); static boolean isSimpleType(Class<?> cls); static Object objectToSimpleType(Object obj, Class<?> cls); static Type getType(String typeName); static StackTraceElement findCallerStackTrace(Class<?> calleeClass); static final Type[] EMPTY_TYPE_ARRAY; static final Map<String, Class<?>> PRIMITIVE_TYPE_MAP; }