in src/main/java/org/apache/sling/fsprovider/internal/mapper/valuemap/ObjectConverter.java [46:122]
public static <T> T convert(Object obj, Class<T> type) {
if (obj == null) {
return null;
}
// check if direct assignment is possible
if (type.isAssignableFrom(obj.getClass())) {
return (T)obj;
}
// convert array elements individually
if (type.isArray()) {
return (T)convertToArray(obj, type.getComponentType());
}
// convert Calendar in Date and vice versa
if (Calendar.class.isAssignableFrom(type) && obj instanceof Date) {
return (T)DateUtils.toCalendar((Date)obj);
}
if (type == Date.class && obj instanceof Calendar) {
return (T)DateUtils.toDate((Calendar)obj);
}
// no direct conversion - format to string and try to parse to target type
String result = getSingleValue(obj);
if (result == null) {
return null;
}
if (type == String.class) {
return (T)result.toString();
}
if (type == Boolean.class) {
// do not rely on Boolean.parseBoolean to avoid converting nonsense to "false" without noticing
if ("true".equalsIgnoreCase(result)) {
return (T)Boolean.TRUE;
}
else if ("false".equalsIgnoreCase(result)) {
return (T)Boolean.FALSE;
}
else {
return null;
}
}
try {
if (type == Byte.class) {
return (T)(Byte)Byte.parseByte(result);
}
if (type == Short.class) {
return (T)(Short)Short.parseShort(result);
}
if (type == Integer.class) {
return (T)(Integer)Integer.parseInt(result);
}
if (type == Long.class) {
return (T)(Long)Long.parseLong(result);
}
if (type == Float.class) {
return (T)(Float)Float.parseFloat(result);
}
if (type == Double.class) {
return (T)(Double)Double.parseDouble(result);
}
if (type == BigDecimal.class) {
return (T)new BigDecimal(result);
}
}
catch (NumberFormatException e) {
return null;
}
if (Calendar.class.isAssignableFrom(type)) {
return (T)DateUtils.calendarFromString(result);
}
if (type == Date.class) {
return (T)DateUtils.dateFromString(result);
}
return null;
}