Class TypeCoercionUtil
Converts a runtime value toward a target Java type. This is the fallback used by
SimpleResultSetBeanMapper and
SimpleMapBeanMapper when no custom converter is configured.
null input always yields null. For many target types an incompatible
value also yields null instead of throwing. Parsing failures and unsupported
target types throw TypeCoercionException.
Supported targets include:
- scalars:
String, numeric wrappers,Boolean,Character,UUID, enums (by name, ignoring case, or ordinal) - identifiers:
Currency,Locale,Charset,URI,URL,File,Path - arrays of those scalars, including primitive arrays and the matching wrapper arrays
(
byte[],short[],int[],long[],float[],double[],char[],boolean[]) - JDBC:
Blob,Clob,NClob,Date,Time,Timestamp - legacy and
java.time:Date,Calendar,TimeZone,Duration,Period,Month,DayOfWeek,LocalDate,LocalTime,LocalDateTime,Instant,ZonedDateTime,OffsetDateTime,Year,YearMonth,MonthDay,OffsetTime,ZoneId,ZoneOffset
JDBC date/time targets also accept java.time values, Calendar,
epoch Number, and all-digit epoch-millis strings. Other strings delegate to
DateTimeUtil (LocalDate, LocalTime, LocalDateTime formats).
A primitive array and the array of its wrapper are copied through NumberUtils.
Any other array is converted element by element. null elements stay null
in a wrapper array. A null element stored into a primitive array throws
TypeCoercionException. Blob still converts to byte[].
A String converts to byte[]/Byte[] as UTF-8
and to char[]/Character[] as its characters.
The reverse uses new String: chars as written, bytes and Blob decoded as UTF-8.
char[]/Character[] become a Clob the same way a string does.
Byte[] is accepted wherever byte[] is: Blob and UUID.
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
TypeCoercionUtil
public TypeCoercionUtil()
-
-
Method Details
-
coerce
CoercesrawtowardtargetType.- Type Parameters:
T- target type- Parameters:
raw- source value;nullyieldsnulltargetType- desired type, including primitives- Returns:
- coerced value compatible with
targetType, ornull - Throws:
TypeCoercionException- when coercion fails definitively
-