public Object convert()

in src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean.java [247:287]


    public <T> Object convert(final Object value, final Class<T> targetType) {
        final boolean nullValue = value == null;
        final Class<?> sourceType = nullValue ? null : value.getClass();

        if (LOG.isDebugEnabled()) {
            if (nullValue) {
                LOG.debug("Convert null value to type '" + targetType.getName() + "'");
            } else {
                LOG.debug("Convert type '" + sourceType.getName() + "' value '" + value + "' to type '" + targetType.getName() + "'");
            }
        }

        Object converted = value;
        final Converter<T> converter = lookup(sourceType, targetType);
        if (converter != null) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("  Using converter " + converter);
            }
            converted = converter.convert(targetType, value);
        }
        if (String.class.equals(targetType) && converted != null && !(converted instanceof String)) {

            // NOTE: For backwards compatibility, if the Converter
            // doesn't handle conversion-->String then
            // use the registered String Converter
            final Converter<String> strConverter = lookup(String.class);
            if (strConverter != null) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("  Using converter " + converter);
                }
                converted = strConverter.convert(String.class, converted);
            }

            // If the object still isn't a String, use toString() method
            if (converted != null && !(converted instanceof String)) {
                converted = converted.toString();
            }

        }
        return converted;
    }