src/org/nutz/castor/Castors.java
@SuppressWarnings({"unchecked", "rawtypes"})
    public <F, T> T cast(Object src, Class<F> fromType, Class<T> toType, String... args)
            throws FailToCastObjectException {
        if (null == src) {
            // 原生数据的默认值
            if (toType.isPrimitive()) {
                if (toType == int.class)
                    return (T) Integer.valueOf(0);
                else if (toType == long.class)
                    return (T) Long.valueOf(0L);
                else if (toType == byte.class)
                    return (T) Byte.valueOf((byte) 0);
                else if (toType == short.class)
                    return (T) Short.valueOf((short) 0);
                else if (toType == float.class)
                    return (T) Float.valueOf(.0f);
                else if (toType == double.class)
                    return (T) Double.valueOf(.0);
                else if (toType == boolean.class)
                    return (T) Boolean.FALSE;
                else if (toType == char.class)
                    return (T) Character.valueOf(' ');
                throw Lang.impossible();
            }
            // 是对象，直接返回 null
            return null;
        }

        if (fromType == toType || toType == null || fromType == null)
            return (T) src;

        Class<?> componentType = toType.getComponentType();
        if (null != componentType
            && fromType != String.class
            && componentType.isAssignableFrom(fromType)) {
            Object array = Array.newInstance(componentType, 1);
            Array.set(array, 0, src);
            return (T) array;
        }

        if (fromType.getName().equals(toType.getName()))
            return (T) src;
        if (toType.isAssignableFrom(fromType))
            return (T) src;
        Mirror<?> from = Mirror.me(fromType, extractor);
        Castor c = find(from, toType);
        if (null == c)
            throw new FailToCastObjectException(String.format("Can not find castor for '%s'=>'%s' in (%d) because:\n%s",
                                                              fromType.getName(),
                                                              toType.getName(),
                                                              map.size(),
                                                              "Fail to find matched castor"));
        if (Object2Object.class.getName().equals(c.getClass().getName())
            && from.canCastToDirectly(toType)) { // Use language built-in cases
            return (T) src;
        }
        try {
            return (T) c.cast(src, toType, args);
        }
        catch (FailToCastObjectException e) {
            throw e;
        }
        catch (Exception e) {
            throw new FailToCastObjectException(String.format("Fail to cast from <%s> to <%s> for {%s}",
                                                              fromType.getName(),
                                                              toType.getName(),
                                                              src),
                                                Lang.unwrapThrow(e));
        }
    }
@SuppressWarnings({"unchecked", "rawtypes"})
    public <F, T> T cast(Object src, Class<F> fromType, Class<T> toType, String... args)
            throws FailToCastObjectException {
        if (null == src) {
            // 原生数据的默认值
            if (toType.isPrimitive()) {
                if (toType == int.class)
                    return (T) Integer.valueOf(0);
                else if (toType == long.class)
                    return (T) Long.valueOf(0L);
                else if (toType == byte.class)
                    return (T) Byte.valueOf((byte) 0);
                else if (toType == short.class)
                    return (T) Short.valueOf((short) 0);
                else if (toType == float.class)
                    return (T) Float.valueOf(.0f);
                else if (toType == double.class)
                    return (T) Double.valueOf(.0);
                else if (toType == boolean.class)
                    return (T) Boolean.FALSE;
                else if (toType == char.class)
                    return (T) Character.valueOf(' ');
                throw Lang.impossible();
            }
            // 是对象，直接返回 null
            return null;
        }

        if (fromType == toType || toType == null || fromType == null)
            return (T) src;

        Class<?> componentType = toType.getComponentType();
        if (null != componentType
            && fromType != String.class
            && componentType.isAssignableFrom(fromType)) {
            Object array = Array.newInstance(componentType, 1);
            Array.set(array, 0, src);
            return (T) array;
        }

        if (fromType.getName().equals(toType.getName()))
            return (T) src;
        if (toType.isAssignableFrom(fromType))
            return (T) src;
        Mirror<?> from = Mirror.me(fromType, extractor);
        Castor c = find(from, toType);
        if (null == c)
            throw new FailToCastObjectException(String.format("Can not find castor for '%s'=>'%s' in (%d) because:\n%s",
                                                              fromType.getName(),
                                                              toType.getName(),
                                                              map.size(),
                                                              "Fail to find matched castor"));
        if (Object2Object.class.getName().equals(c.getClass().getName())
            && from.canCastToDirectly(toType)) { // Use language built-in cases
            return (T) src;
        }
        try {
            return (T) c.cast(src, toType, args);
        }
        catch (FailToCastObjectException e) {
            throw e;
        }
        catch (Exception e) {
            throw new FailToCastObjectException(String.format("Fail to cast from <%s> to <%s> for {%s}",
                                                              fromType.getName(),
                                                              toType.getName(),
                                                              src),
                                                Lang.unwrapThrow(e));
        }
    }
转换一个 POJO 从一个指定的类型到另外的类型

@param src
源对象
@param fromType
源对象类型
@param toType
目标类型
@param args
转换时参数。有些 Castor 可能需要这个参数，比如 Array2Map
@return 目标对象
@throws FailToCastObjectException
如果没有找到转换器，或者转换失败