@Test public void testIsAssignmentCompatible() { assertTrue(ObjectUtils.isAssignmentCompatible(Object.class, String.class)); assertTrue(ObjectUtils.isAssignmentCompatible(String.class, String.class)); assertTrue(ObjectUtils.isAssignmentCompatible(Long.class, long.class)); assertTrue(ObjectUtils.isAssignmentCompatible(long.class, Long.class)); assertTrue(ObjectUtils.isAssignmentCompatible(long.class, Integer.class)); assertTrue(ObjectUtils.isAssignmentCompatible(float.class, double.class)); assertTrue(ObjectUtils.isAssignmentCompatible(DateFormat.class, SimpleDateFormat.class)); assertFalse(ObjectUtils.isAssignmentCompatible(SimpleDateFormat.class, DateFormat.class)); assertFalse(ObjectUtils.isAssignmentCompatible(String.class, Object.class)); }
public static boolean isAssignmentCompatible(Class<?> target, Class<?> source) { if (target.isAssignableFrom(source)) return true; if (source.isPrimitive()) { source = MethodUtils.getPrimitiveWrapper(source); if (source == null) return false; if (target.isAssignableFrom(source)) return true; } if (target.isPrimitive()) { target = MethodUtils.getPrimitiveWrapper(target); if (target == null) return false; if (target.isAssignableFrom(source)) return true; } Class<Number> numberClass = Number.class; if (numberClass.isAssignableFrom(target) && numberClass.isAssignableFrom(source)) { return true; } if (source==Character.class && target==String.class) { return true; } return false; }
ObjectUtils { public static boolean isAssignmentCompatible(Class<?> target, Class<?> source) { if (target.isAssignableFrom(source)) return true; if (source.isPrimitive()) { source = MethodUtils.getPrimitiveWrapper(source); if (source == null) return false; if (target.isAssignableFrom(source)) return true; } if (target.isPrimitive()) { target = MethodUtils.getPrimitiveWrapper(target); if (target == null) return false; if (target.isAssignableFrom(source)) return true; } Class<Number> numberClass = Number.class; if (numberClass.isAssignableFrom(target) && numberClass.isAssignableFrom(source)) { return true; } if (source==Character.class && target==String.class) { return true; } return false; } }
ObjectUtils { public static boolean isAssignmentCompatible(Class<?> target, Class<?> source) { if (target.isAssignableFrom(source)) return true; if (source.isPrimitive()) { source = MethodUtils.getPrimitiveWrapper(source); if (source == null) return false; if (target.isAssignableFrom(source)) return true; } if (target.isPrimitive()) { target = MethodUtils.getPrimitiveWrapper(target); if (target == null) return false; if (target.isAssignableFrom(source)) return true; } Class<Number> numberClass = Number.class; if (numberClass.isAssignableFrom(target) && numberClass.isAssignableFrom(source)) { return true; } if (source==Character.class && target==String.class) { return true; } return false; } private  ObjectUtils(); }
ObjectUtils { public static boolean isAssignmentCompatible(Class<?> target, Class<?> source) { if (target.isAssignableFrom(source)) return true; if (source.isPrimitive()) { source = MethodUtils.getPrimitiveWrapper(source); if (source == null) return false; if (target.isAssignableFrom(source)) return true; } if (target.isPrimitive()) { target = MethodUtils.getPrimitiveWrapper(target); if (target == null) return false; if (target.isAssignableFrom(source)) return true; } Class<Number> numberClass = Number.class; if (numberClass.isAssignableFrom(target) && numberClass.isAssignableFrom(source)) { return true; } if (source==Character.class && target==String.class) { return true; } return false; } private  ObjectUtils(); static boolean isEmpty(Object o); static int lengthOf(Object o); @SuppressWarnings("unchecked") static boolean compareEqual(Object o1, Object o2); static boolean compareEqual(Object[] array1, Object[] array2); static T coalesce(T preferred, T alternative); static int toInteger(Object v); static int getInteger(Object v, int defValue); static int getInteger(Object v); static long toLong(Object v); static long getLong(Object v, long defValue); static long getLong(Object v); static double toDouble(Object v); static double getDouble(Object v, double defValue); static double getDouble(Object v); static BigDecimal toDecimal(Object v); static BigDecimal getDecimal(Object v, BigDecimal defValue); static BigDecimal getDecimal(Object v); static boolean getBoolean(Object v); @SuppressWarnings("unchecked") static T getEnum(Class<T> enumType, Object value); static Date getDate(Object v, Locale locale); static Date getDate(Object v); static String formatDate(Date date, boolean withTime); @SuppressWarnings("unchecked") static T convert(Class<T> c, Object v); static boolean isAssignmentCompatible(Class<?> target, Class<?> source); static List<T> convert(Class<T> t, Collection<? extends T> source); static String[] toStringArray(Object[] objArray, String defValue); static boolean contains(T[] array, T item); static synchronized Object getFieldValue(Class<?> clazz, Object object, String property, boolean includePrivateFields); static Object getFieldValue(Object object, String property); static Object getPrivateFieldValue(Object object, String property); static synchronized Object invokeSimpleMethod(Class<?> clazz, Object object, String methodName, boolean includePrivateMethods); static Object invokeSimpleMethod(Object object, String methodName); static Object invokeSimplePrivateMethod(Object object, String methodName); }
ObjectUtils { public static boolean isAssignmentCompatible(Class<?> target, Class<?> source) { if (target.isAssignableFrom(source)) return true; if (source.isPrimitive()) { source = MethodUtils.getPrimitiveWrapper(source); if (source == null) return false; if (target.isAssignableFrom(source)) return true; } if (target.isPrimitive()) { target = MethodUtils.getPrimitiveWrapper(target); if (target == null) return false; if (target.isAssignableFrom(source)) return true; } Class<Number> numberClass = Number.class; if (numberClass.isAssignableFrom(target) && numberClass.isAssignableFrom(source)) { return true; } if (source==Character.class && target==String.class) { return true; } return false; } private  ObjectUtils(); static boolean isEmpty(Object o); static int lengthOf(Object o); @SuppressWarnings("unchecked") static boolean compareEqual(Object o1, Object o2); static boolean compareEqual(Object[] array1, Object[] array2); static T coalesce(T preferred, T alternative); static int toInteger(Object v); static int getInteger(Object v, int defValue); static int getInteger(Object v); static long toLong(Object v); static long getLong(Object v, long defValue); static long getLong(Object v); static double toDouble(Object v); static double getDouble(Object v, double defValue); static double getDouble(Object v); static BigDecimal toDecimal(Object v); static BigDecimal getDecimal(Object v, BigDecimal defValue); static BigDecimal getDecimal(Object v); static boolean getBoolean(Object v); @SuppressWarnings("unchecked") static T getEnum(Class<T> enumType, Object value); static Date getDate(Object v, Locale locale); static Date getDate(Object v); static String formatDate(Date date, boolean withTime); @SuppressWarnings("unchecked") static T convert(Class<T> c, Object v); static boolean isAssignmentCompatible(Class<?> target, Class<?> source); static List<T> convert(Class<T> t, Collection<? extends T> source); static String[] toStringArray(Object[] objArray, String defValue); static boolean contains(T[] array, T item); static synchronized Object getFieldValue(Class<?> clazz, Object object, String property, boolean includePrivateFields); static Object getFieldValue(Object object, String property); static Object getPrivateFieldValue(Object object, String property); static synchronized Object invokeSimpleMethod(Class<?> clazz, Object object, String methodName, boolean includePrivateMethods); static Object invokeSimpleMethod(Object object, String methodName); static Object invokeSimplePrivateMethod(Object object, String methodName); static final NoValue NO_VALUE; }