本文整理匯總了Java中org.apache.commons.beanutils.ConversionException類的典型用法代碼示例。如果您正苦於以下問題:Java ConversionException類的具體用法?Java ConversionException怎麽用?Java ConversionException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ConversionException類屬於org.apache.commons.beanutils包,在下文中一共展示了ConversionException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: checkConversionResult
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* 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);
}
示例2: parse
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* Convert the specified locale-sensitive input object into an output object of the
* specified type. This method will return values of type Short.
*
* @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
* @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 Short) {
return result;
}
final Number parsed = (Number)result;
if (parsed.longValue() != parsed.shortValue()) {
throw new ConversionException("Supplied number is not of type Short: " + parsed.longValue());
}
// now returns property Short
return new Short(parsed.shortValue());
}
示例3: parse
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* 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);
}
}
示例4: parse
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* Convert the specified locale-sensitive input object into an output object of
* BigInteger 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 BigInteger) {
return result;
}
if (result instanceof Number) {
return BigInteger.valueOf(((Number)result).longValue());
}
try {
return new BigInteger(result.toString());
}
catch (final NumberFormatException ex) {
throw new ConversionException("Suplied number is not of type BigInteger: " + result);
}
}
示例5: parse
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* Convert a String into a <code>Number</code> object.
* @param sourceType the source type of the conversion
* @param targetType The type to convert the value to
* @param value The String date value.
* @param format The NumberFormat to parse the String value.
*
* @return The converted Number object.
* @throws ConversionException if the String cannot be converted.
*/
private Number parse(final Class<?> sourceType, final Class<?> targetType, final String value, final NumberFormat format) {
final ParsePosition pos = new ParsePosition(0);
final Number parsedNumber = format.parse(value, pos);
if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedNumber == null) {
String msg = "Error converting from '" + toString(sourceType) + "' to '" + toString(targetType) + "'";
if (format instanceof DecimalFormat) {
msg += " using pattern '" + ((DecimalFormat)format).toPattern() + "'";
}
if (locale != null) {
msg += " for locale=[" + locale + "]";
}
if (log().isDebugEnabled()) {
log().debug(" " + msg);
}
throw new ConversionException(msg);
}
return parsedNumber;
}
示例6: parse
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* Parse a String date value using the set of patterns.
*
* @param sourceType The type of the value being converted
* @param targetType The type to convert the value to.
* @param value The String date value.
*
* @return The converted Date object.
* @throws Exception if an error occurs parsing the date.
*/
private Calendar parse(final Class<?> sourceType, final Class<?> targetType, final String value) throws Exception {
Exception firstEx = null;
for (String pattern : patterns) {
try {
final DateFormat format = getFormat(pattern);
final Calendar calendar = parse(sourceType, targetType, value, format);
return calendar;
} catch (final Exception ex) {
if (firstEx == null) {
firstEx = ex;
}
}
}
if (patterns.length > 1) {
throw new ConversionException("Error converting '" + toString(sourceType) + "' to '" + toString(targetType)
+ "' using patterns '" + displayPatterns + "'");
} else {
throw firstEx;
}
}
示例7: set
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* <p>Set the value of a simple property with the specified name.</p>
*
* @param name Name of the property whose value is to be set
* @param value Value to which this property is to be set
*
* @exception ConversionException if the specified value cannot be
* converted to the type required for this property
* @exception IllegalArgumentException if there is no property
* of the specified name
* @exception NullPointerException if the type specified for the
* property is invalid
* @exception NullPointerException if an attempt is made to set a
* primitive property to null
*/
public void set(String name, Object value) {
DynaProperty descriptor = getDynaProperty(name);
if (descriptor.getType() == null) {
throw new NullPointerException
("The type for property " + name + " is invalid");
}
if (value == null) {
if (descriptor.getType().isPrimitive()) {
throw new NullPointerException
("Primitive value for '" + name + "'");
}
} else if (!isDynaAssignable(descriptor.getType(), value.getClass())) {
throw new ConversionException
("Cannot assign value of type '" +
value.getClass().getName() +
"' to property '" + name + "' of type '" +
descriptor.getType().getName() + "'");
}
dynaValues.put(name, value);
}
示例8: convert
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
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進行轉化
}
示例9: testContextualizeConversionException
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
@Test
public void testContextualizeConversionException() {
final TestBean testBean = new TestBean();
final DefaultContext context = new DefaultContext();
context.add("val", "some string");
try {
testBean.contextualize(context);
fail("InvocationTargetException is expected");
}
catch (CheckstyleException ex) {
final String expected = "illegal value ";
assertTrue("Invalid exception cause, should be: ConversionException",
ex.getCause() instanceof ConversionException);
assertTrue("Invalid exception message, should start with: " + expected,
ex.getMessage().startsWith(expected));
}
}
示例10: set
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* <p>Set the value of a simple property with the specified name.</p>
*
* @param name Name of the property whose value is to be set
* @param value Value to which this property is to be set
* @throws ConversionException if the specified value cannot be
* converted to the type required for
* this property
* @throws IllegalArgumentException if there is no property of the
* specified name
* @throws NullPointerException if the type specified for the property
* is invalid
* @throws NullPointerException if an attempt is made to set a
* primitive property to null
*/
public void set(String name, Object value) {
DynaProperty descriptor = getDynaProperty(name);
if (descriptor.getType() == null) {
throw new NullPointerException("The type for property " + name
+ " is invalid");
}
if (value == null) {
if (descriptor.getType().isPrimitive()) {
throw new NullPointerException("Primitive value for '" + name
+ "'");
}
} else if (!isDynaAssignable(descriptor.getType(), value.getClass())) {
throw new ConversionException("Cannot assign value of type '"
+ value.getClass().getName() + "' to property '" + name
+ "' of type '" + descriptor.getType().getName() + "'");
}
dynaValues.put(name, value);
}
示例11: convertToComplexType
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
public Format convertToComplexType(String convertFrom)
throws ConversionException {
//The patterns can contain the delimiter character.
int firstSeparator = convertFrom.indexOf(DELIMITER);
if (firstSeparator == -1) {
throw new IllegalArgumentException("Cannot find the class type for the string " +
convertFrom);
}
String className = convertFrom.substring(0, firstSeparator);
String pattern = convertFrom.substring(firstSeparator + 1);
if (className.equals(DecimalFormat.class.getSimpleName())) {
return new DecimalFormat(pattern);
} else if (className.equals(SimpleDateFormat.class.getSimpleName())) {
return new SimpleDateFormat(pattern);
} else {
throw new IllegalStateException("Unknown class " + className +
" to create a format on based on the pattern " + pattern);
}
}
示例12: convert
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
*
* @exception org.apache.commons.beanutils.ConversionException if conversion cannot be performed
* successfully
*/
public Object convert(Class type, Object value) {
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
if (value instanceof Timestamp) {
return (value);
}
try {
return (Timestamp.valueOf(value.toString()));
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(e);
}
}
}
示例13: convertToWrappedPrimitive
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* Convert to wrapped primitive
* @param source Source object
* @param wrapper Primitive wrapper type
* @return Converted object
*/
public static Object convertToWrappedPrimitive(Object source, Class<?> wrapper) {
if (source == null || wrapper == null) {
return null;
}
if (wrapper.isInstance(source)) {
return source;
}
if (wrapper.isAssignableFrom(source.getClass())) {
return source;
}
if (source instanceof Number) {
return convertNumberToWrapper((Number) source, wrapper);
} else {
//ensure we dont try to convert text to a number, prevent NumberFormatException
if (Number.class.isAssignableFrom(wrapper)) {
//test for int or fp number
if (!source.toString().matches(NUMERIC_TYPE)) {
throw new ConversionException(String.format("Unable to convert string %s its not a number type: %s", source, wrapper));
}
}
return convertStringToWrapper(source.toString(), wrapper);
}
}
示例14: convertStringToWrapper
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* Convert string to primitive wrapper like Boolean or Float
* @param str String to convert
* @param wrapper Primitive wrapper type
* @return Converted object
*/
public static Object convertStringToWrapper(String str, Class<?> wrapper) {
log.trace("String: {} to wrapper: {}", str, wrapper);
if (wrapper.equals(String.class)) {
return str;
} else if (wrapper.equals(Boolean.class)) {
return Boolean.valueOf(str);
} else if (wrapper.equals(Double.class)) {
return Double.valueOf(str);
} else if (wrapper.equals(Long.class)) {
return Long.valueOf(str);
} else if (wrapper.equals(Float.class)) {
return Float.valueOf(str);
} else if (wrapper.equals(Integer.class)) {
return Integer.valueOf(str);
} else if (wrapper.equals(Short.class)) {
return Short.valueOf(str);
} else if (wrapper.equals(Byte.class)) {
return Byte.valueOf(str);
}
throw new ConversionException(String.format("Unable to convert string to: %s", wrapper));
}
示例15: convertNumberToWrapper
import org.apache.commons.beanutils.ConversionException; //導入依賴的package包/類
/**
* Convert number to primitive wrapper like Boolean or Float
* @param num Number to conver
* @param wrapper Primitive wrapper type
* @return Converted object
*/
public static Object convertNumberToWrapper(Number num, Class<?> wrapper) {
//XXX Paul: Using valueOf will reduce object creation
if (wrapper.equals(String.class)) {
return num.toString();
} else if (wrapper.equals(Boolean.class)) {
return Boolean.valueOf(num.intValue() == 1);
} else if (wrapper.equals(Double.class)) {
return Double.valueOf(num.doubleValue());
} else if (wrapper.equals(Long.class)) {
return Long.valueOf(num.longValue());
} else if (wrapper.equals(Float.class)) {
return Float.valueOf(num.floatValue());
} else if (wrapper.equals(Integer.class)) {
return Integer.valueOf(num.intValue());
} else if (wrapper.equals(Short.class)) {
return Short.valueOf(num.shortValue());
} else if (wrapper.equals(Byte.class)) {
return Byte.valueOf(num.byteValue());
}
throw new ConversionException(String.format("Unable to convert number to: %s", wrapper));
}