/** * Set the default value, converting as required. * <p> * If the default value is different from the type the * <code>Converter</code> handles, it will be converted * to the handled type. * * @param defaultValue The default value to be returned * if the value to be converted is missing or an error * occurs converting the value. * @throws ConversionException if an error occurs converting * the default value */ protected void setDefaultValue(final Object defaultValue) { useDefault = false; if (log().isDebugEnabled()) { log().debug("Setting default value: " + defaultValue); } if (defaultValue == null) { this.defaultValue = null; } else { this.defaultValue = convert(getDefaultType(), defaultValue); } useDefault = true; }
return convertToDefaultType(type, value); 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); return handleMissing(targetType); return targetType.cast(convertToString(value)); if (log().isDebugEnabled()) { log().debug(" No conversion required, value is already a " + toString(targetType)); final Object result = convertToType(targetType, value); if (log().isDebugEnabled()) { log().debug(" Converted to " + toString(targetType) + " value '" + result + "'"); return handleError(targetType, value, t);
Object value = getDefault(type); if (useDefault && value != null && !(type.equals(value.getClass()))) { try { value = convertToType(type, defaultValue); } catch (final Throwable t) { throw new ConversionException("Default conversion to " + toString(type) + " failed.", t); if (log().isDebugEnabled()) { log().debug(" Using default " + (value == null ? "" : toString(value.getClass()) + " ") + "value '" + defaultValue + "'"); toString(type) + "'"); if (log().isDebugEnabled()) { log().debug(" Throwing ConversionException: " + cex.getMessage()); log().debug(" " + DEFAULT_CONFIG_MSG);
/** * Performs a conversion to the default type. This method is called if we do * not have a target class. In this case, the T parameter is not set. * Therefore, we can cast to it (which is required to fulfill the contract * of the method signature). * * @param <T> the type of the result object * @param targetClass the target class of the conversion * @param value the value to be converted * @return the converted value */ private <T> T convertToDefaultType(final Class<T> targetClass, final Object value) { @SuppressWarnings("unchecked") final T result = (T) convert(getDefaultType(), value); return result; }
Class targetType = primitive(type == null ? getDefaultType() : type); if (log().isDebugEnabled()) { log().debug("Converting" + (value == null ? "" : " '" + toString(sourceType) + "'") + " value '" + value + "' to type '" + toString(targetType) + "'"); value = convertArray(value); return handleMissing(targetType); return convertToString(value); if (log().isDebugEnabled()) { log().debug(" No conversion required, value is already a " + toString(targetType)); Object result = convertToType(targetType, value); if (log().isDebugEnabled()) { log().debug(" Converted to " + toString(targetType) + " value '" + result + "'"); return handleError(targetType, value, t);
if (log().isDebugEnabled()) { if (cause instanceof ConversionException) { log().debug(" Conversion threw ConversionException: " + cause.getMessage()); } else { log().debug(" Conversion threw " + cause); return handleMissing(type); if (cause instanceof ConversionException) { cex = (ConversionException)cause; if (log().isDebugEnabled()) { log().debug(" Re-throwing ConversionException: " + cex.getMessage()); log().debug(" " + DEFAULT_CONFIG_MSG); final String msg = "Error converting from '" + toString(value.getClass()) + "' to '" + toString(type) + "' " + cause.getMessage(); cex = new ConversionException(msg, cause); if (log().isDebugEnabled()) { log().debug(" Throwing ConversionException: " + msg); log().debug(" " + DEFAULT_CONFIG_MSG);
/** * Construct a <i>Converter</i> that returns a default * value if an error occurs. * * @param defaultValue The default value to be returned * if the value to be converted is missing or an error * occurs converting the value. */ public AbstractConverter(final Object defaultValue) { setDefaultValue(defaultValue); }
/** * Return the default value for conversions to the specified * type. * @param type Data type to which this value should be converted. * @return The default value for the specified type. */ @Override protected Object getDefault(final Class<?> type) { if (type.equals(String.class)) { return null; } final Object defaultValue = super.getDefault(type); if (defaultValue == null) { return null; } if (defaultValue.getClass().equals(type)) { return defaultValue; } else { return Array.newInstance(type.getComponentType(), defaultSize); } }
Class targetType = primitive(type == null ? getDefaultType() : type); if (log().isDebugEnabled()) { log().debug("Converting" + (value == null ? "" : " '" + toString(sourceType) + "'") + " value '" + value + "' to type '" + toString(targetType) + "'"); value = convertArray(value); return handleMissing(targetType); return convertToString(value); if (log().isDebugEnabled()) { log().debug(" No conversion required, value is already a " + toString(targetType)); Object result = convertToType(targetType, value); if (log().isDebugEnabled()) { log().debug(" Converted to " + toString(targetType) + " value '" + result + "'"); return handleError(targetType, value, t);
if (log().isDebugEnabled()) { if (cause instanceof ConversionException) { log().debug(" Conversion threw ConversionException: " + cause.getMessage()); } else { log().debug(" Conversion threw " + cause); return handleMissing(type); if (cause instanceof ConversionException) { cex = (ConversionException)cause; if (log().isDebugEnabled()) { log().debug(" Re-throwing ConversionException: " + cex.getMessage()); log().debug(" " + DEFAULT_CONFIG_MSG); final String msg = "Error converting from '" + toString(value.getClass()) + "' to '" + toString(type) + "' " + cause.getMessage(); cex = new ConversionException(msg, cause); if (log().isDebugEnabled()) { log().debug(" Throwing ConversionException: " + msg); log().debug(" " + DEFAULT_CONFIG_MSG);
/** * Performs a conversion to the default type. This method is called if we do * not have a target class. In this case, the T parameter is not set. * Therefore, we can cast to it (which is required to fulfill the contract * of the method signature). * * @param <T> the type of the result object * @param targetClass the target class of the conversion * @param value the value to be converted * @return the converted value */ private <T> T convertToDefaultType(final Class<T> targetClass, final Object value) { @SuppressWarnings("unchecked") final T result = (T) convert(getDefaultType(), value); return result; }
/** * Construct a <i>Converter</i> that returns a default * value if an error occurs. * * @param defaultValue The default value to be returned * if the value to be converted is missing or an error * occurs converting the value. */ public AbstractConverter(final Object defaultValue) { setDefaultValue(defaultValue); }
/** * Return the default value for conversions to the specified * type. * @param type Data type to which this value should be converted. * @return The default value for the specified type. */ @Override protected Object getDefault(final Class<?> type) { if (type.equals(String.class)) { return null; } final Object defaultValue = super.getDefault(type); if (defaultValue == null) { return null; } if (defaultValue.getClass().equals(type)) { return defaultValue; } else { return Array.newInstance(type.getComponentType(), defaultSize); } }
Class targetType = primitive(type == null ? getDefaultType() : type); if (log().isDebugEnabled()) { log().debug("Converting" + (value == null ? "" : " '" + toString(sourceType) + "'") + " value '" + value + "' to type '" + toString(targetType) + "'"); value = convertArray(value); return handleMissing(targetType); return convertToString(value); if (log().isDebugEnabled()) { log().debug(" No conversion required, value is already a " + toString(targetType)); Object result = convertToType(targetType, value); if (log().isDebugEnabled()) { log().debug(" Converted to " + toString(targetType) + " value '" + result + "'"); return handleError(targetType, value, t);
return convertToDefaultType(type, value); 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); return handleMissing(targetType); return targetType.cast(convertToString(value)); if (log().isDebugEnabled()) { log().debug(" No conversion required, value is already a " + toString(targetType)); final Object result = convertToType(targetType, value); if (log().isDebugEnabled()) { log().debug(" Converted to " + toString(targetType) + " value '" + result + "'"); return handleError(targetType, value, t);
Object value = getDefault(type); if (useDefault && value != null && !(type.equals(value.getClass()))) { try { value = convertToType(type, defaultValue); } catch (final Throwable t) { throw new ConversionException("Default conversion to " + toString(type) + " failed.", t); if (log().isDebugEnabled()) { log().debug(" Using default " + (value == null ? "" : toString(value.getClass()) + " ") + "value '" + defaultValue + "'"); toString(type) + "'"); if (log().isDebugEnabled()) { log().debug(" Throwing ConversionException: " + cex.getMessage()); log().debug(" " + DEFAULT_CONFIG_MSG);
/** * Set the default value, converting as required. * <p> * If the default value is different from the type the * <code>Converter</code> handles, it will be converted * to the handled type. * * @param defaultValue The default value to be returned * if the value to be converted is missing or an error * occurs converting the value. * @throws ConversionException if an error occurs converting * the default value */ protected void setDefaultValue(final Object defaultValue) { useDefault = false; if (log().isDebugEnabled()) { log().debug("Setting default value: " + defaultValue); } if (defaultValue == null) { this.defaultValue = null; } else { this.defaultValue = convert(getDefaultType(), defaultValue); } useDefault = true; }
if (log().isDebugEnabled()) { if (cause instanceof ConversionException) { log().debug(" Conversion threw ConversionException: " + cause.getMessage()); } else { log().debug(" Conversion threw " + cause); return handleMissing(type); if (cause instanceof ConversionException) { cex = (ConversionException)cause; if (log().isDebugEnabled()) { log().debug(" Re-throwing ConversionException: " + cex.getMessage()); log().debug(" " + DEFAULT_CONFIG_MSG); String msg = "Error converting from '" + toString(value.getClass()) + "' to '" + toString(type) + "' " + cause.getMessage(); cex = new ConversionException(msg, cause); if (log().isDebugEnabled()) { log().debug(" Throwing ConversionException: " + msg); log().debug(" " + DEFAULT_CONFIG_MSG);
/** * Performs a conversion to the default type. This method is called if we do * not have a target class. In this case, the T parameter is not set. * Therefore, we can cast to it (which is required to fulfill the contract * of the method signature). * * @param <T> the type of the result object * @param targetClass the target class of the conversion * @param value the value to be converted * @return the converted value */ private <T> T convertToDefaultType(final Class<T> targetClass, final Object value) { @SuppressWarnings("unchecked") final T result = (T) convert(getDefaultType(), value); return result; }
/** * Construct a <i>Converter</i> that returns a default * value if an error occurs. * * @param defaultValue The default value to be returned * if the value to be converted is missing or an error * occurs converting the value. */ public AbstractConverter(final Object defaultValue) { setDefaultValue(defaultValue); }