/** * Generates a standard conversion exception with a message indicating that * the passed in value cannot be converted to the desired target type. * * @param type the target type * @param value the value to be converted * @return a {@code ConversionException} with a standard message * @since 1.9 */ protected ConversionException conversionException(final Class<?> type, final Object value) { return new ConversionException("Can't convert value '" + value + "' to type " + type); } }
value = convertToType(type, defaultValue); } catch (final Throwable t) { throw new ConversionException("Default conversion to " + toString(type) + " failed.", t); final ConversionException cex = new ConversionException("No value specified for '" + toString(type) + "'"); if (log().isDebugEnabled()) { log().debug(" Throwing ConversionException: " + cex.getMessage()); log().debug(" " + DEFAULT_CONFIG_MSG);
private static ConfigNewData<?> convertPrimitive(Class<?> type, ConfigNewData<?> v) { if (ClassUtils.isPrimitiveOrWrapper(type)) { try { return new ConfigNewData<>(v.getPrefixContext(), v.getFieldName(), ConvertUtils.convert(v.getNewValue(), type)); } catch (ConversionException e) { throw new IllegalArgumentException(e.getMessage(), e); } } return v; }
private static Object convert(Class klass, Object value) throws JellyTagException { if(null == value) { return null; } else if(!klass.isInstance(value)) { Converter converter = (Converter)(converterMap.get(klass)); if(null == converter) { throw new JellyTagException("Can't convert " + value + " to " + klass); } else { try { return converter.convert(klass,value); } catch(ConversionException e) { throw new JellyTagException("Can't convert " + value + " to " + klass + " (" + e.toString() + ")",e); } } } else { return value; } }
private static Object convert(Class klass, Object value) throws JellyTagException { if(null == value) { return null; } else if(!klass.isInstance(value)) { Converter converter = (Converter)(converterMap.get(klass)); if(null == converter) { throw new JellyTagException("Can't convert " + value + " to " + klass); } else { try { return converter.convert(klass,value); } catch(ConversionException e) { throw new JellyTagException("Can't convert " + value + " to " + klass + " (" + e.toString() + ")",e); } } } else { return value; } }
/** * Generates a standard conversion exception with a message indicating that * the passed in value cannot be converted to the desired target type. * * @param type the target type * @param value the value to be converted * @return a {@code ConversionException} with a standard message * @since 1.9 */ protected ConversionException conversionException(final Class<?> type, final Object value) { return new ConversionException("Can't convert value '" + value + "' to type " + type); } }
value = convertToType(type, defaultValue); } catch (final Throwable t) { throw new ConversionException("Default conversion to " + toString(type) + " failed.", t); final ConversionException cex = new ConversionException("No value specified for '" + toString(type) + "'"); if (log().isDebugEnabled()) { log().debug(" Throwing ConversionException: " + cex.getMessage()); log().debug(" " + DEFAULT_CONFIG_MSG);
private static Object convert(Class klass, Object value) throws JellyTagException { if(null == value) { return null; } else if(!klass.isInstance(value)) { Converter converter = (Converter)(converterMap.get(klass)); if(null == converter) { throw new JellyTagException("Can't convert " + value + " to " + klass); } else { try { return converter.convert(klass,value); } catch(ConversionException e) { throw new JellyTagException("Can't convert " + value + " to " + klass + " (" + e.toString() + ")",e); } } } else { return value; } }
public Object convert(Class type, Object value) { if (value == null) { if (useDefault) { return (defaultValue); } else { throw new ConversionException("No value specified"); } } if (value instanceof byte[]) { return (value); } // BLOB类型,canal直接存储为String("ISO-8859-1") if (value instanceof String) { try { return ((String) value).getBytes("ISO-8859-1"); } catch (Exception e) { throw new ConversionException(e); } } return converter.convert(type, value); // byteConvertor进行转化 } }
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);
private static Object convert(Class klass, Object value) throws JellyTagException { if(null == value) { return null; } else if(!klass.isInstance(value)) { Converter converter = (Converter)(converterMap.get(klass)); if(null == converter) { throw new JellyTagException("Can't convert " + value + " to " + klass); } else { try { return converter.convert(klass,value); } catch(ConversionException e) { throw new JellyTagException("Can't convert " + value + " to " + klass + " (" + e.toString() + ")",e); } } } else { return value; } }
/** * Checks whether the result of a conversion is conform to the specified * target type. If this is the case, the passed in result object is cast to * the correct target type. Otherwise, an exception is thrown. * * @param <T> the desired result type * @param type the target class of the conversion * @param result the conversion result object * @return the result cast to the target class * @throws ConversionException if the result object is not compatible with * the target type */ private static <T> T checkConversionResult(final Class<T> type, final Object result) { if (type == null) { // in this case we cannot do much; the result object is returned @SuppressWarnings("unchecked") final T temp = (T) result; return temp; } if (result == null) { return null; } if (type.isInstance(result)) { return type.cast(result); } throw new ConversionException("Unsupported target type: " + type); } }
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);
/** * Checks whether the result of a conversion is conform to the specified * target type. If this is the case, the passed in result object is cast to * the correct target type. Otherwise, an exception is thrown. * * @param <T> the desired result type * @param type the target class of the conversion * @param result the conversion result object * @return the result cast to the target class * @throws ConversionException if the result object is not compatible with * the target type */ private static <T> T checkConversionResult(final Class<T> type, final Object result) { if (type == null) { // in this case we cannot do much; the result object is returned @SuppressWarnings("unchecked") final T temp = (T) result; return temp; } if (result == null) { return null; } if (type.isInstance(result)) { return type.cast(result); } throw new ConversionException("Unsupported target type: " + type); } }
/** * Convert the specified locale-sensitive input object into an output object of the * specified type. This method will return values of type Byte. * * @param value The input object to be converted * @param pattern The pattern is used for the convertion * @return The converted value * * @throws org.apache.commons.beanutils.ConversionException if conversion cannot be performed * successfully * @throws ParseException if an error occurs parsing a String to a Number */ @Override protected Object parse(final Object value, final String pattern) throws ParseException { final Number parsed = (Number) super.parse(value, pattern); if (parsed.longValue() != parsed.byteValue()) { throw new ConversionException("Supplied number is not of type Byte: " + parsed.longValue()); } // now returns property Byte return new Byte(parsed.byteValue()); } }
/** * Convert the specified locale-sensitive input object into an output object of the * specified type. This method will return Integer type. * * @param value The input object to be converted * @param pattern The pattern is used for the convertion * @return The converted value * * @throws ConversionException if conversion cannot be performed * successfully * @throws ParseException if an error occurs parsing a String to a Number */ @Override protected Object parse(final Object value, final String pattern) throws ParseException { final Number parsed = (Number) super.parse(value, pattern); if (parsed.longValue() != parsed.intValue()) { throw new ConversionException("Suplied number is not of type Integer: " + parsed.longValue()); } return new Integer(parsed.intValue()); // unlike superclass it will return proper Integer } }
/** * Convert the specified locale-sensitive input object into an output object of the * specified type. This method will return Integer type. * * @param value The input object to be converted * @param pattern The pattern is used for the convertion * @return The converted value * * @throws ConversionException if conversion cannot be performed * successfully * @throws ParseException if an error occurs parsing a String to a Number */ @Override protected Object parse(final Object value, final String pattern) throws ParseException { final Number parsed = (Number) super.parse(value, pattern); if (parsed.longValue() != parsed.intValue()) { throw new ConversionException("Suplied number is not of type Integer: " + parsed.longValue()); } return new Integer(parsed.intValue()); // unlike superclass it will return proper Integer } }
/** * Convert the specified locale-sensitive input object into an output object of the * specified type. This method will return values of type Byte. * * @param value The input object to be converted * @param pattern The pattern is used for the convertion * @return The converted value * * @throws org.apache.commons.beanutils.ConversionException if conversion cannot be performed * successfully * @throws ParseException if an error occurs parsing a String to a Number */ @Override protected Object parse(final Object value, final String pattern) throws ParseException { final Number parsed = (Number) super.parse(value, pattern); if (parsed.longValue() != parsed.byteValue()) { throw new ConversionException("Supplied number is not of type Byte: " + parsed.longValue()); } // now returns property Byte return new Byte(parsed.byteValue()); } }
/** * Convert the specified locale-sensitive input object into an output object of the * specified type. This method will return Float value or throw exception if value * can not be stored in the Float. * * @param value The input object to be converted * @param pattern The pattern is used for the convertion * @return The converted value * * @throws ConversionException if conversion cannot be performed * successfully * @throws ParseException if an error occurs parsing a String to a Number */ @Override protected Object parse(final Object value, final String pattern) throws ParseException { final Number parsed = (Number) super.parse(value, pattern); final double doubleValue = parsed.doubleValue(); final double posDouble = (doubleValue >= 0) ? doubleValue : (doubleValue * -1); if (posDouble != 0 && (posDouble < Float.MIN_VALUE || posDouble > Float.MAX_VALUE)) { throw new ConversionException("Supplied number is not of type Float: "+parsed); } return new Float(parsed.floatValue()); // unlike superclass it returns Float type } }
/** * Convert the specified locale-sensitive input object into an output object of * BigDecimal type. * * @param value The input object to be converted * @param pattern The pattern is used for the convertion * @return The converted value * * @throws ConversionException if conversion cannot be performed * successfully * @throws ParseException if an error occurs parsing a String to a Number * @since 1.8.0 */ @Override protected Object parse(final Object value, final String pattern) throws ParseException { final Object result = super.parse(value, pattern); if (result == null || result instanceof BigDecimal) { return result; } try { return new BigDecimal(result.toString()); } catch (final NumberFormatException ex) { throw new ConversionException("Suplied number is not of type BigDecimal: " + result); } }