@Test(expected=ConversionException.class) public void testNullConvertNoClass() { ConversionUtils.convert(new TestJavaBean(), null); }
public static Object convert(Object source, Class<?> target) throws ConversionException { if (target == null) { throw new ConversionException("Unable to perform conversion, target was null"); } if (source == null) { if (target.isPrimitive()) { throw new ConversionException(String.format("Unable to convert null to primitive value of %s", target)); } return source; } else if ((source instanceof Float && ((Float) source).isNaN()) || (source instanceof Double && ((Double) source).isNaN())) { return source; } if (IConnection.class.isAssignableFrom(source.getClass()) && !target.equals(IConnection.class)) { throw new ConversionException("IConnection must match exactly"); } if (target.isInstance(source)) { return source; } if (target.isAssignableFrom(source.getClass())) { return source; } if (target.isArray()) { return convertToArray(source, target); } if (target.equals(String.class)) { return source.toString(); } if (target.isPrimitive()) { return convertToWrappedPrimitive(source, primitiveMap.get(target)); } if (wrapperMap.containsKey(target)) { return convertToWrappedPrimitive(source, target); } if (target.equals(Map.class)) { return convertBeanToMap(source); } if (target.equals(List.class) || target.equals(Collection.class)) { if (source.getClass().equals(LinkedHashMap.class)) { return convertMapToList((LinkedHashMap<?, ?>) source); } else if (source.getClass().isArray()) { return convertArrayToList((Object[]) source); } } if (target.equals(Set.class) && source.getClass().isArray()) { return convertArrayToSet((Object[]) source); } throw new ConversionException(String.format("Unable to preform conversion from %s to %s", source, target)); }
ConversionUtils { public static Object convert(Object source, Class<?> target) throws ConversionException { if (target == null) { throw new ConversionException("Unable to perform conversion, target was null"); } if (source == null) { if (target.isPrimitive()) { throw new ConversionException(String.format("Unable to convert null to primitive value of %s", target)); } return source; } else if ((source instanceof Float && ((Float) source).isNaN()) || (source instanceof Double && ((Double) source).isNaN())) { return source; } if (IConnection.class.isAssignableFrom(source.getClass()) && !target.equals(IConnection.class)) { throw new ConversionException("IConnection must match exactly"); } if (target.isInstance(source)) { return source; } if (target.isAssignableFrom(source.getClass())) { return source; } if (target.isArray()) { return convertToArray(source, target); } if (target.equals(String.class)) { return source.toString(); } if (target.isPrimitive()) { return convertToWrappedPrimitive(source, primitiveMap.get(target)); } if (wrapperMap.containsKey(target)) { return convertToWrappedPrimitive(source, target); } if (target.equals(Map.class)) { return convertBeanToMap(source); } if (target.equals(List.class) || target.equals(Collection.class)) { if (source.getClass().equals(LinkedHashMap.class)) { return convertMapToList((LinkedHashMap<?, ?>) source); } else if (source.getClass().isArray()) { return convertArrayToList((Object[]) source); } } if (target.equals(Set.class) && source.getClass().isArray()) { return convertArrayToSet((Object[]) source); } throw new ConversionException(String.format("Unable to preform conversion from %s to %s", source, target)); } }
ConversionUtils { public static Object convert(Object source, Class<?> target) throws ConversionException { if (target == null) { throw new ConversionException("Unable to perform conversion, target was null"); } if (source == null) { if (target.isPrimitive()) { throw new ConversionException(String.format("Unable to convert null to primitive value of %s", target)); } return source; } else if ((source instanceof Float && ((Float) source).isNaN()) || (source instanceof Double && ((Double) source).isNaN())) { return source; } if (IConnection.class.isAssignableFrom(source.getClass()) && !target.equals(IConnection.class)) { throw new ConversionException("IConnection must match exactly"); } if (target.isInstance(source)) { return source; } if (target.isAssignableFrom(source.getClass())) { return source; } if (target.isArray()) { return convertToArray(source, target); } if (target.equals(String.class)) { return source.toString(); } if (target.isPrimitive()) { return convertToWrappedPrimitive(source, primitiveMap.get(target)); } if (wrapperMap.containsKey(target)) { return convertToWrappedPrimitive(source, target); } if (target.equals(Map.class)) { return convertBeanToMap(source); } if (target.equals(List.class) || target.equals(Collection.class)) { if (source.getClass().equals(LinkedHashMap.class)) { return convertMapToList((LinkedHashMap<?, ?>) source); } else if (source.getClass().isArray()) { return convertArrayToList((Object[]) source); } } if (target.equals(Set.class) && source.getClass().isArray()) { return convertArrayToSet((Object[]) source); } throw new ConversionException(String.format("Unable to preform conversion from %s to %s", source, target)); }  }
ConversionUtils { public static Object convert(Object source, Class<?> target) throws ConversionException { if (target == null) { throw new ConversionException("Unable to perform conversion, target was null"); } if (source == null) { if (target.isPrimitive()) { throw new ConversionException(String.format("Unable to convert null to primitive value of %s", target)); } return source; } else if ((source instanceof Float && ((Float) source).isNaN()) || (source instanceof Double && ((Double) source).isNaN())) { return source; } if (IConnection.class.isAssignableFrom(source.getClass()) && !target.equals(IConnection.class)) { throw new ConversionException("IConnection must match exactly"); } if (target.isInstance(source)) { return source; } if (target.isAssignableFrom(source.getClass())) { return source; } if (target.isArray()) { return convertToArray(source, target); } if (target.equals(String.class)) { return source.toString(); } if (target.isPrimitive()) { return convertToWrappedPrimitive(source, primitiveMap.get(target)); } if (wrapperMap.containsKey(target)) { return convertToWrappedPrimitive(source, target); } if (target.equals(Map.class)) { return convertBeanToMap(source); } if (target.equals(List.class) || target.equals(Collection.class)) { if (source.getClass().equals(LinkedHashMap.class)) { return convertMapToList((LinkedHashMap<?, ?>) source); } else if (source.getClass().isArray()) { return convertArrayToList((Object[]) source); } } if (target.equals(Set.class) && source.getClass().isArray()) { return convertArrayToSet((Object[]) source); } throw new ConversionException(String.format("Unable to preform conversion from %s to %s", source, target)); }  static Object convert(Object source, Class<?> target); static Object convertToArray(Object source, Class<?> target); static List<Object> convertMapToList(Map<?, ?> map); static Object convertToWrappedPrimitive(Object source, Class<?> wrapper); static Object convertStringToWrapper(String str, Class<?> wrapper); static Object convertNumberToWrapper(Number num, Class<?> wrapper); static List<Method> findMethodsByNameAndNumParams(Object object,
			String method, int numParam); static Object[] convertParams(Object[] source, Class<?>[] target); static Class<?>[] convertParams(Object[] source); static List<?> convertArrayToList(Object[] source); static Object convertMapToBean(Map<?, ?> source, Class<?> target); static Map<?, ?> convertBeanToMap(Object source); static Set<?> convertArrayToSet(Object[] source); }
ConversionUtils { public static Object convert(Object source, Class<?> target) throws ConversionException { if (target == null) { throw new ConversionException("Unable to perform conversion, target was null"); } if (source == null) { if (target.isPrimitive()) { throw new ConversionException(String.format("Unable to convert null to primitive value of %s", target)); } return source; } else if ((source instanceof Float && ((Float) source).isNaN()) || (source instanceof Double && ((Double) source).isNaN())) { return source; } if (IConnection.class.isAssignableFrom(source.getClass()) && !target.equals(IConnection.class)) { throw new ConversionException("IConnection must match exactly"); } if (target.isInstance(source)) { return source; } if (target.isAssignableFrom(source.getClass())) { return source; } if (target.isArray()) { return convertToArray(source, target); } if (target.equals(String.class)) { return source.toString(); } if (target.isPrimitive()) { return convertToWrappedPrimitive(source, primitiveMap.get(target)); } if (wrapperMap.containsKey(target)) { return convertToWrappedPrimitive(source, target); } if (target.equals(Map.class)) { return convertBeanToMap(source); } if (target.equals(List.class) || target.equals(Collection.class)) { if (source.getClass().equals(LinkedHashMap.class)) { return convertMapToList((LinkedHashMap<?, ?>) source); } else if (source.getClass().isArray()) { return convertArrayToList((Object[]) source); } } if (target.equals(Set.class) && source.getClass().isArray()) { return convertArrayToSet((Object[]) source); } throw new ConversionException(String.format("Unable to preform conversion from %s to %s", source, target)); }  static Object convert(Object source, Class<?> target); static Object convertToArray(Object source, Class<?> target); static List<Object> convertMapToList(Map<?, ?> map); static Object convertToWrappedPrimitive(Object source, Class<?> wrapper); static Object convertStringToWrapper(String str, Class<?> wrapper); static Object convertNumberToWrapper(Number num, Class<?> wrapper); static List<Method> findMethodsByNameAndNumParams(Object object,
			String method, int numParam); static Object[] convertParams(Object[] source, Class<?>[] target); static Class<?>[] convertParams(Object[] source); static List<?> convertArrayToList(Object[] source); static Object convertMapToBean(Map<?, ?> source, Class<?> target); static Map<?, ?> convertBeanToMap(Object source); static Set<?> convertArrayToSet(Object[] source);  }