@Test public void testObjectToSimpleType() { assertEquals((Integer)100, ClassUtil.objectToSimpleType(100, Integer.class)); assertEquals((Float)(float)100.1, ClassUtil.objectToSimpleType(100.1, float.class)); }
public static Object objectToSimpleType(Object obj, Class<?> cls) { if (cls == String.class && (obj == null || obj instanceof String)) return obj; if (cls == boolean.class || cls == Boolean.class) return obj; if (cls == byte.class || cls == Byte.class) return ((Number)obj).byteValue(); if (cls == int.class || cls == Integer.class) return ((Number)obj).intValue(); if (cls == long.class || cls == Long.class) return ((Number)obj).longValue(); if (cls == float.class || cls == Float.class) return ((Number)obj).floatValue(); if (cls == double.class || cls == Double.class) return ((Number)obj).doubleValue(); if (cls == char.class || cls == Character.class) return obj instanceof Character ? (Character) obj : ((String)obj).charAt(0); if (cls == BigDecimal.class) return new BigDecimal(String.valueOf(obj)); return null; }
ClassUtil { public static Object objectToSimpleType(Object obj, Class<?> cls) { if (cls == String.class && (obj == null || obj instanceof String)) return obj; if (cls == boolean.class || cls == Boolean.class) return obj; if (cls == byte.class || cls == Byte.class) return ((Number)obj).byteValue(); if (cls == int.class || cls == Integer.class) return ((Number)obj).intValue(); if (cls == long.class || cls == Long.class) return ((Number)obj).longValue(); if (cls == float.class || cls == Float.class) return ((Number)obj).floatValue(); if (cls == double.class || cls == Double.class) return ((Number)obj).doubleValue(); if (cls == char.class || cls == Character.class) return obj instanceof Character ? (Character) obj : ((String)obj).charAt(0); if (cls == BigDecimal.class) return new BigDecimal(String.valueOf(obj)); return null; } }
ClassUtil { public static Object objectToSimpleType(Object obj, Class<?> cls) { if (cls == String.class && (obj == null || obj instanceof String)) return obj; if (cls == boolean.class || cls == Boolean.class) return obj; if (cls == byte.class || cls == Byte.class) return ((Number)obj).byteValue(); if (cls == int.class || cls == Integer.class) return ((Number)obj).intValue(); if (cls == long.class || cls == Long.class) return ((Number)obj).longValue(); if (cls == float.class || cls == Float.class) return ((Number)obj).floatValue(); if (cls == double.class || cls == Double.class) return ((Number)obj).doubleValue(); if (cls == char.class || cls == Character.class) return obj instanceof Character ? (Character) obj : ((String)obj).charAt(0); if (cls == BigDecimal.class) return new BigDecimal(String.valueOf(obj)); return null; }  }
ClassUtil { public static Object objectToSimpleType(Object obj, Class<?> cls) { if (cls == String.class && (obj == null || obj instanceof String)) return obj; if (cls == boolean.class || cls == Boolean.class) return obj; if (cls == byte.class || cls == Byte.class) return ((Number)obj).byteValue(); if (cls == int.class || cls == Integer.class) return ((Number)obj).intValue(); if (cls == long.class || cls == Long.class) return ((Number)obj).longValue(); if (cls == float.class || cls == Float.class) return ((Number)obj).floatValue(); if (cls == double.class || cls == Double.class) return ((Number)obj).doubleValue(); if (cls == char.class || cls == Character.class) return obj instanceof Character ? (Character) obj : ((String)obj).charAt(0); if (cls == BigDecimal.class) return new BigDecimal(String.valueOf(obj)); 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 { public static Object objectToSimpleType(Object obj, Class<?> cls) { if (cls == String.class && (obj == null || obj instanceof String)) return obj; if (cls == boolean.class || cls == Boolean.class) return obj; if (cls == byte.class || cls == Byte.class) return ((Number)obj).byteValue(); if (cls == int.class || cls == Integer.class) return ((Number)obj).intValue(); if (cls == long.class || cls == Long.class) return ((Number)obj).longValue(); if (cls == float.class || cls == Float.class) return ((Number)obj).floatValue(); if (cls == double.class || cls == Double.class) return ((Number)obj).doubleValue(); if (cls == char.class || cls == Character.class) return obj instanceof Character ? (Character) obj : ((String)obj).charAt(0); if (cls == BigDecimal.class) return new BigDecimal(String.valueOf(obj)); 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; }