public abstract class AbstractConverter extends Object implements Converter
Converter implementation that provides the structure
for handling conversion to and from a specified type.
This implementation provides the basic structure for
converting to/from a specified type optionally using a default
value or throwing a ConversionException if a
conversion error occurs.
Implementations should provide conversion to the specified
type and from the specified type to a String value
by implementing the following methods:
convertToString(value) - convert to a String
(default implementation uses the objects toString()
method).convertToType(Class, value) - convert
to the specified type| 构造器和说明 |
|---|
AbstractConverter()
创建创造Converter转换器,可能会引发
ConversionException异常。 |
AbstractConverter(Object defaultValue)
创建创造Converter转换器,可能会引发
ConversionException异常。 |
| 限定符和类型 | 方法和说明 |
|---|---|
Object |
convert(Class<?> type,
Object value)
Convert the input object into an output object of the
specified type.
|
protected Object |
convertArray(Object value)
Return the first element from an Array (or Collection)
or the value unchanged if not an Array (or Collection).
|
protected String |
convertToString(Object value)
转换对象成为String格式。
|
protected abstract Object |
convertToType(Class<?> type,
Object value)
执行类型转换代码。
|
protected Object |
getDefault(Class<?> type)
返回指定类型的默认值.
|
protected abstract Class<?> |
getDefaultType()
获取默认值
|
protected Object |
handleError(Class<?> type,
Object value,
Throwable cause)
处理转换错误。
|
protected Object |
handleMissing(Class<?> type)
当遇到空值传入或者返回值为空的时候
|
boolean |
isUseDefault()
当转换期间发生异常时是否使用默认值。
|
protected void |
setDefaultValue(Object defaultValue)
设置默认值
|
String |
toString()
Provide a String representation of this converter.
|
String |
toString(Class<?> type)
Provide a String representation of a
java.lang.Class. |
public AbstractConverter()
ConversionException异常。public AbstractConverter(Object defaultValue)
ConversionException异常。public boolean isUseDefault()
true则表示当遇到错误时设置的默认值会被返回。如果false会引发ConversionException异常。public Object convert(Class<?> type, Object value)
convert 在接口中 Convertertype - Data type to which this value should be convertedvalue - The input value to be convertedConversionException - if conversion cannot be performed
successfully and no default is specified.protected Object handleError(Class<?> type, Object value, Throwable cause)
如果设置了default属性则当遇到错误时返回默认值。否则引发ConversionException异常。
protected String convertToString(Object value) throws Throwable
注意:这个方法简单使用toString()实现该功能,子类应当重写该方法以完成特殊的转换过程。
Throwableprotected abstract Object convertToType(Class<?> type, Object value) throws Throwable
Throwableprotected Object convertArray(Object value)
value - The value to convertprotected void setDefaultValue(Object defaultValue)
protected abstract Class<?> getDefaultType()
public String toString()
Copyright © 2022. All rights reserved.