当前位置: 首页>>代码示例>>Java>>正文


Java ConvertUtils.primitiveToWrapper方法代码示例

本文整理汇总了Java中org.apache.commons.beanutils.ConvertUtils.primitiveToWrapper方法的典型用法代码示例。如果您正苦于以下问题:Java ConvertUtils.primitiveToWrapper方法的具体用法?Java ConvertUtils.primitiveToWrapper怎么用?Java ConvertUtils.primitiveToWrapper使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.beanutils.ConvertUtils的用法示例。


在下文中一共展示了ConvertUtils.primitiveToWrapper方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: convert

import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
/**
 * Convert the specified locale-sensitive input object into an output object of the
 * specified type.
 *
 * @param <T> The desired target type of the conversion
 * @param type Data is type to which this value should be converted
 * @param value is the input object to be converted
 * @param pattern is the pattern is used for the conversion; if null is
 * passed then the default pattern associated with the converter object
 * will be used.
 * @return The converted value
 *
 * @throws ConversionException if conversion cannot be performed
 *  successfully
 */
public <T> T convert(final Class<T> type, final Object value, final String pattern) {
    final Class<T> targetType = ConvertUtils.primitiveToWrapper(type);
    if (value == null) {
        if (useDefault) {
            return getDefaultAs(targetType);
        } else {
            // symmetric beanutils function allows null
            // so do not: throw new ConversionException("No value specified");
            log.debug("Null value specified for conversion, returing null");
            return null;
        }
    }

    try {
        if (pattern != null) {
            return checkConversionResult(targetType, parse(value, pattern));
        } else {
            return checkConversionResult(targetType, parse(value, this.pattern));
        }
    } catch (final Exception e) {
        if (useDefault) {
            return getDefaultAs(targetType);
        } else {
            if (e instanceof ConversionException) {
                throw (ConversionException)e;
            }
            throw new ConversionException(e);
        }
    }
}
 
开发者ID:yippeesoft,项目名称:NotifyTools,代码行数:46,代码来源:BaseLocaleConverter.java

示例2: convert

import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
/**
 * Convert the input object into an output object of the
 * specified type.
 *
 * @param <T> the target type of the conversion
 * @param type Data type to which this value should be converted
 * @param value The input value to be converted
 * @return The converted value.
 * @throws ConversionException if conversion cannot be performed
 * successfully and no default is specified.
 */
public <T> T convert(final Class<T> type, Object value) {

    if (type == null) {
        return convertToDefaultType(type, value);
    }

    Class<?> sourceType  = value == null ? null : value.getClass();
    final Class<T> targetType  = ConvertUtils.primitiveToWrapper(type);

    if (log().isDebugEnabled()) {
        log().debug("Converting"
                + (value == null ? "" : " '" + toString(sourceType) + "'")
                + " value '" + value + "' to type '" + toString(targetType) + "'");
    }

    value = convertArray(value);

    // Missing Value
    if (value == null) {
        return handleMissing(targetType);
    }

    sourceType = value.getClass();

    try {
        // Convert --> String
        if (targetType.equals(String.class)) {
            return targetType.cast(convertToString(value));

        // No conversion necessary
        } else if (targetType.equals(sourceType)) {
            if (log().isDebugEnabled()) {
                log().debug("    No conversion required, value is already a "
                                + toString(targetType));
            }
            return targetType.cast(value);

        // Convert --> Type
        } else {
            final Object result = convertToType(targetType, value);
            if (log().isDebugEnabled()) {
                log().debug("    Converted to " + toString(targetType) +
                               " value '" + result + "'");
            }
            return targetType.cast(result);
        }
    } catch (final Throwable t) {
        return handleError(targetType, value, t);
    }

}
 
开发者ID:yippeesoft,项目名称:NotifyTools,代码行数:63,代码来源:AbstractConverter.java


注:本文中的org.apache.commons.beanutils.ConvertUtils.primitiveToWrapper方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。