public static Object convert()

in openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java [282:437]


    public static Object convert(Object o, Class<?> type, boolean strictNumericConversion) {
        if (o == null)
            return null;
        if (o.getClass() == type)
            return o;

        type = wrap(type);
        if (type.isAssignableFrom(o.getClass()))
            return o;

        // the non-numeric conversions we do are to string, or from
        // string/char to number, or calendar/date
        // String to Boolean
        // String to Integer
        boolean num = o instanceof Number;
        if (!num) {
            if (type == String.class) {
                return o.toString();
            }
            else if (type == Boolean.class && o instanceof String) {
                return Boolean.valueOf(o.toString());
            }
            else if (type == Integer.class && o instanceof String) {
                try {
                    return new Integer(o.toString());
                }
                catch (NumberFormatException e) {
                    throw new ClassCastException(_loc.get("cant-convert", o, o.getClass(), type).getMessage());
                }
            }
            else if (type == Character.class) {
                String str = o.toString();
                if (str != null && str.length() == 1) {
                    return str.charAt(0);
                }
            }
            else if (Calendar.class.isAssignableFrom(type) && o instanceof Date) {
                Calendar cal = Calendar.getInstance();
                cal.setTime((Date) o);
                return cal;
            }
            else if (Date.class.isAssignableFrom(type) && o instanceof Calendar) {
                return ((Calendar) o).getTime();
            }
            else if (Number.class.isAssignableFrom(type)) {
                Integer i = null;
                if (o instanceof Character) {
                    i = Integer.valueOf((Character) o);
                }
                else if (o instanceof String && ((String) o).length() == 1) {
                    i = Integer.valueOf(((String) o));
                }

                if (i != null) {
                    if (type == Integer.class) {
                        return i;
                    }
                    num = true;
                }
            } else if (Temporal.class.isAssignableFrom(type)) {
                // handling of Java8 time API.
                if (LocalDate.class.equals(type)) {
                    if (o instanceof java.sql.Date) {
                        return ((java.sql.Date) o).toLocalDate();
                    } else if (o instanceof java.util.Date) {
                        return new java.sql.Date(((java.util.Date)o).getTime()).toLocalDate();
                    } else if (o instanceof CharSequence) {
                        return LocalDate.parse((CharSequence) o);
                    }
                } else if (LocalDateTime.class.equals(type)) {
                    if (o instanceof java.sql.Timestamp) {
                        return ((java.sql.Timestamp) o).toLocalDateTime();
                    } else if (o instanceof java.util.Date) {
                        return ((java.util.Date)o).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                    } else if (o instanceof CharSequence) {
                        return LocalDateTime.parse((CharSequence) o);
                    }
                } else if (LocalTime.class.equals(type)) {
                    if (o instanceof java.sql.Time) {
                        return ((java.sql.Time) o).toLocalTime();
                    } else if (o instanceof java.util.Date) {
                        return ((java.util.Date)o).toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
                    } else if (o instanceof CharSequence) {
                        return LocalTime.parse((CharSequence) o);
                    }
                } else if (OffsetTime.class.equals(type)) {
                    if (o instanceof java.sql.Time) {
                        return ((java.sql.Time) o).toLocalTime().atOffset(OffsetDateTime.now().getOffset());
                    } else if (o instanceof java.util.Date) {
                        return ((java.util.Date)o).toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime().toOffsetTime();
                    } else if (o instanceof CharSequence) {
                        return OffsetTime.parse((CharSequence) o);
                    }
                } else if (OffsetDateTime.class.equals(type)) {
                    if (o instanceof java.sql.Timestamp) {
                        return ((java.sql.Timestamp) o).toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime();
                    } else if (o instanceof java.util.Date) {
                        return ((java.util.Date)o).toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime();
                    } else if (o instanceof CharSequence) {
                        return OffsetDateTime.parse((CharSequence) o);
                    }
                }

            } else if (o instanceof String && isJDBCTemporalSyntax(o.toString())) {
                try {
                    Object temporal = parseJDBCTemporalSyntax(o.toString());
                    if (temporal != null && type.isAssignableFrom(temporal.getClass()))
                        return temporal;
                } catch (IllegalArgumentException e) {

                }
            } else if (o instanceof String && type.isEnum()) {
                return Enum.valueOf((Class<Enum>)type, o.toString());
            }
        }
        if (!num) {
            throw new ClassCastException(_loc.get("cant-convert", o, o.getClass(), type).getMessage());
        }

        if (type == Integer.class && allowNumericConversion(o.getClass(), type, strictNumericConversion)) {
            return ((Number) o).intValue();
        } else if (type == Float.class && allowNumericConversion(o.getClass(), type, strictNumericConversion)) {
            return ((Number) o).floatValue();
        } else if (type == Double.class) {
            return ((Number) o).doubleValue();
        } else if (type == Long.class && allowNumericConversion(o.getClass(), type, strictNumericConversion)) {
            return ((Number) o).longValue();
        } else if (type == BigDecimal.class) {
            // the BigDecimal constructor doesn't handle the
            // "NaN" string version of Double.NaN and Float.NaN, nor
            // does it handle infinity; we need to instead use the Double
            // and Float versions, despite wanting to cast it to BigDecimal
            double dval = ((Number) o).doubleValue();
            if (Double.isNaN(dval) || Double.isInfinite(dval)) {
                return dval;
            }
            float fval = ((Number) o).floatValue();
            if (Float.isNaN(fval) || Float.isInfinite(fval)) {
                return fval;
            }

            return new BigDecimal(o.toString());
        } else if (type == BigInteger.class) {
            return new BigInteger(o.toString());
        } else if (type == Short.class && allowNumericConversion(o.getClass(), type, strictNumericConversion)) {
            return ((Number) o).shortValue();
        } else if (type == Byte.class && allowNumericConversion(o.getClass(), type, strictNumericConversion)) {
            return ((Number) o).byteValue();
        } else if (type == Character.class) {
            return (char) ((Number) o).intValue();
        } else if (!strictNumericConversion) {
            return ((Number) o).intValue();
        } else {
            throw new ClassCastException(_loc.get("cant-convert", o, o.getClass(), type).getMessage());
        }
    }