### Question:
ObjectUtils { @SuppressWarnings("unchecked") public static <T> T convert(Class<T> c, Object v) throws ClassCastException { if (v==null || c.isInstance(v)) return (T)v; if (c.isPrimitive()) { c = (Class<T>) MethodUtils.getPrimitiveWrapper(c); } if (c.isAssignableFrom(Boolean.class)) return c.cast(getBoolean(v)); if (c.isAssignableFrom(Integer.class)) return c.cast(getInteger(v)); if (c.isAssignableFrom(Long.class)) return c.cast(getLong(v)); if(c.isAssignableFrom(Double.class)) return c.cast(getDouble(v)); if (c.isAssignableFrom(String.class)) return c.cast(v.toString()); return c.cast(v); } 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; }### Answer:
@Test public void testConvertClassOfTObject() { Object object = new Object(); assertNull(ObjectUtils.convert(Void.class, (Object)null)); assertEquals("JUnit", ObjectUtils.convert(String.class, "JUnit")); assertEquals(Integer.valueOf(123), ObjectUtils.convert(Integer.class, "123")); assertEquals(Long.valueOf(123), ObjectUtils.convert(Long.class, "123")); assertEquals(object.toString(), ObjectUtils.convert(String.class, object)); assertEquals(Boolean.TRUE, ObjectUtils.convert(Boolean.class, 123)); assertEquals(Long.valueOf(Long.MAX_VALUE), ObjectUtils.convert(Long.class, Long.MAX_VALUE)); assertEquals(Float.valueOf(123.123f), ObjectUtils.convert(Float.class, 123.123f)); assertEquals(Integer.valueOf(123), ObjectUtils.convert(Integer.class, 123.123)); assertEquals("JUnit", ObjectUtils.convert(Object.class, "JUnit")); }
@Test public void testConvertClassOfTCollectionOfQextendsT() { List<String> objectList = new ArrayList<String>(); objectList.add("JUnit"); objectList.add("JUnit2"); Collection<Object> results = ObjectUtils.convert(Object.class, objectList); assertEquals(2, objectList.size()); Iterator<Object> it = results.iterator(); assertEquals("JUnit", it.next()); assertEquals("JUnit2", it.next()); assertFalse(it.hasNext()); }