Converter { public Object convert(Type returnType, Object object) { if (object == null) { Object primitive = NULL_TO_PRIMITIVE.get(returnType); if (isThrowError() && primitive != null) { throw new ConversionException( "Can't convert a null into a primitive!", primitive); } else { return primitive; } } Class<?> returnClass; if (returnType instanceof Class) { returnClass = (Class<?>) returnType; if (returnClass.isInstance(object)) { return object; } } else { returnClass = null; } Type itemType; if (returnClass != null && returnClass.isArray()) { itemType = returnClass.getComponentType(); } else if (returnType instanceof GenericArrayType) { itemType = ((GenericArrayType) returnType).getGenericComponentType(); } else { Class<?> objectClass = object.getClass(); @SuppressWarnings("unchecked") ConversionFunction<Object, Object> function = (ConversionFunction<Object, Object>) getFunction(objectClass, returnType); if (function != null) { try { return function.convert(this, returnType, object); } catch (Exception ex) { if (isThrowError()) { throw new ConversionException(ex); } else if (ex instanceof ConversionException) { return ((ConversionException) ex).getDefaultValue(); } else { return NULL_TO_PRIMITIVE.get(returnType); } } } throw new ConversionException(String.format( "Can't convert an instance of [%s] to an instance of [%s]!", objectClass, returnType), null); } Collection<?> collection = convert(Collection.class, object); Object array = Array.newInstance( itemType instanceof Class ? (Class<?>) itemType : Object.class, collection.size()); int index = 0; for (Object item : collection) { Array.set(array, index, convert(itemType, item)); ++ index; } return array; }  @SuppressWarnings("unchecked") ConversionFunction<F, Object> getFunction(Class<F> fromClass, Type toType); void putDirectFunction(
            Class<F> fromClass,
            Type toType,
            ConversionFunction<F, ?> function); void putDirectFunction(
            Class<F> fromClass,
            Class<T> toClass,
            ConversionFunction<F, T> function); void putDirectFunction(
            Class<F> fromClass,
            TypeReference<T> toType,
            ConversionFunction<F, T> function); void putInheritableFunction(
            Class<F> fromClass,
            Class<T> toClass,
            ConversionFunction<F, ?> function); void putAllStandardFunctions(); boolean isThrowError(); void setThrowError(boolean isThrowError); Map<String, Exception> getErrors(); Object convert(Type returnType, Object object); @SuppressWarnings("unchecked") T convert(Class<T> returnClass, Object object); @SuppressWarnings("unchecked") T convert(TypeReference<T> returnTypeReference, Object object);  }
@Test public void convert_builtin_primitive() throws Exception { Object result = converter.convert(int.class, "15"); assertEquals(Integer.class, result.getClass()); assertEquals(15, result); }
@Test public void convert_builtin_list_members() throws Exception { List<String> inputs = Arrays.asList("1","2","3"); List<Integer> expect = Arrays.asList(1,2,3); TypeReference<List<Integer>> typeref = new TypeReference<List<Integer>>() {}; List<Integer> output = (List<Integer>)converter.convert(typeref.getType(), inputs); assertEquals(expect, output); }
@Test public void convert_error_noexception() { assertEquals(null, converter.convert(Integer.class, "a")); }