@Test public void testIncorrectIntegerValue() throws Exception { thrown.expect(ODataUnmarshallingException.class); thrown.expectMessage(BAD_VALUE); JsonParserUtils.getAppropriateFieldValue(Integer.class, BAD_VALUE); }
public static Object getAppropriateFieldValue(Class<?> type, String fieldValue) throws ODataUnmarshallingException { Class<?> wrappedType = PrimitiveUtil.wrap(type); if (String.class.isAssignableFrom(wrappedType)) { return fieldValue; } if (wrappedType == byte[].class) { return Base64.getDecoder().decode(fieldValue); } if (UUID.class.isAssignableFrom(wrappedType)) { return UUID.fromString(fieldValue); } if (hasMethod(wrappedType, "parse", String.class)) { try { return wrappedType.getMethod("parse", String.class).invoke(null, fieldValue); } catch (ReflectiveOperationException e) { throw new ODataUnmarshallingException("Could not invoke 'parse' method on " + wrappedType.getCanonicalName() + " for " + fieldValue, e); } } else if (hasMethod(wrappedType, "valueOf", String.class)) { try { return wrappedType.getMethod("valueOf", String.class).invoke(null, fieldValue); } catch (ReflectiveOperationException e) { throw new ODataUnmarshallingException("Could not invoke 'valueOf' method on " + wrappedType.getCanonicalName() + " for " + fieldValue, e); } } else if (wrappedType == ZonedDateTime.class) { return ZonedDateTime.parse(fieldValue); } return null; }
JsonParserUtils { public static Object getAppropriateFieldValue(Class<?> type, String fieldValue) throws ODataUnmarshallingException { Class<?> wrappedType = PrimitiveUtil.wrap(type); if (String.class.isAssignableFrom(wrappedType)) { return fieldValue; } if (wrappedType == byte[].class) { return Base64.getDecoder().decode(fieldValue); } if (UUID.class.isAssignableFrom(wrappedType)) { return UUID.fromString(fieldValue); } if (hasMethod(wrappedType, "parse", String.class)) { try { return wrappedType.getMethod("parse", String.class).invoke(null, fieldValue); } catch (ReflectiveOperationException e) { throw new ODataUnmarshallingException("Could not invoke 'parse' method on " + wrappedType.getCanonicalName() + " for " + fieldValue, e); } } else if (hasMethod(wrappedType, "valueOf", String.class)) { try { return wrappedType.getMethod("valueOf", String.class).invoke(null, fieldValue); } catch (ReflectiveOperationException e) { throw new ODataUnmarshallingException("Could not invoke 'valueOf' method on " + wrappedType.getCanonicalName() + " for " + fieldValue, e); } } else if (wrappedType == ZonedDateTime.class) { return ZonedDateTime.parse(fieldValue); } return null; } }
JsonParserUtils { public static Object getAppropriateFieldValue(Class<?> type, String fieldValue) throws ODataUnmarshallingException { Class<?> wrappedType = PrimitiveUtil.wrap(type); if (String.class.isAssignableFrom(wrappedType)) { return fieldValue; } if (wrappedType == byte[].class) { return Base64.getDecoder().decode(fieldValue); } if (UUID.class.isAssignableFrom(wrappedType)) { return UUID.fromString(fieldValue); } if (hasMethod(wrappedType, "parse", String.class)) { try { return wrappedType.getMethod("parse", String.class).invoke(null, fieldValue); } catch (ReflectiveOperationException e) { throw new ODataUnmarshallingException("Could not invoke 'parse' method on " + wrappedType.getCanonicalName() + " for " + fieldValue, e); } } else if (hasMethod(wrappedType, "valueOf", String.class)) { try { return wrappedType.getMethod("valueOf", String.class).invoke(null, fieldValue); } catch (ReflectiveOperationException e) { throw new ODataUnmarshallingException("Could not invoke 'valueOf' method on " + wrappedType.getCanonicalName() + " for " + fieldValue, e); } } else if (wrappedType == ZonedDateTime.class) { return ZonedDateTime.parse(fieldValue); } return null; } private  JsonParserUtils(); }
JsonParserUtils { public static Object getAppropriateFieldValue(Class<?> type, String fieldValue) throws ODataUnmarshallingException { Class<?> wrappedType = PrimitiveUtil.wrap(type); if (String.class.isAssignableFrom(wrappedType)) { return fieldValue; } if (wrappedType == byte[].class) { return Base64.getDecoder().decode(fieldValue); } if (UUID.class.isAssignableFrom(wrappedType)) { return UUID.fromString(fieldValue); } if (hasMethod(wrappedType, "parse", String.class)) { try { return wrappedType.getMethod("parse", String.class).invoke(null, fieldValue); } catch (ReflectiveOperationException e) { throw new ODataUnmarshallingException("Could not invoke 'parse' method on " + wrappedType.getCanonicalName() + " for " + fieldValue, e); } } else if (hasMethod(wrappedType, "valueOf", String.class)) { try { return wrappedType.getMethod("valueOf", String.class).invoke(null, fieldValue); } catch (ReflectiveOperationException e) { throw new ODataUnmarshallingException("Could not invoke 'valueOf' method on " + wrappedType.getCanonicalName() + " for " + fieldValue, e); } } else if (wrappedType == ZonedDateTime.class) { return ZonedDateTime.parse(fieldValue); } return null; } private  JsonParserUtils(); static StructuredType getStructuredType(String entityName, EntityDataModel edm); static List<StructuralProperty> getAllProperties(StructuredType structuredType,
                                                            EntityDataModel entityDataModel); static Object getAppropriateFieldValue(Class<?> type, String fieldValue); static void setFieldValue(Field field, Object entity, Object value); }
JsonParserUtils { public static Object getAppropriateFieldValue(Class<?> type, String fieldValue) throws ODataUnmarshallingException { Class<?> wrappedType = PrimitiveUtil.wrap(type); if (String.class.isAssignableFrom(wrappedType)) { return fieldValue; } if (wrappedType == byte[].class) { return Base64.getDecoder().decode(fieldValue); } if (UUID.class.isAssignableFrom(wrappedType)) { return UUID.fromString(fieldValue); } if (hasMethod(wrappedType, "parse", String.class)) { try { return wrappedType.getMethod("parse", String.class).invoke(null, fieldValue); } catch (ReflectiveOperationException e) { throw new ODataUnmarshallingException("Could not invoke 'parse' method on " + wrappedType.getCanonicalName() + " for " + fieldValue, e); } } else if (hasMethod(wrappedType, "valueOf", String.class)) { try { return wrappedType.getMethod("valueOf", String.class).invoke(null, fieldValue); } catch (ReflectiveOperationException e) { throw new ODataUnmarshallingException("Could not invoke 'valueOf' method on " + wrappedType.getCanonicalName() + " for " + fieldValue, e); } } else if (wrappedType == ZonedDateTime.class) { return ZonedDateTime.parse(fieldValue); } return null; } private  JsonParserUtils(); static StructuredType getStructuredType(String entityName, EntityDataModel edm); static List<StructuralProperty> getAllProperties(StructuredType structuredType,
                                                            EntityDataModel entityDataModel); static Object getAppropriateFieldValue(Class<?> type, String fieldValue); static void setFieldValue(Field field, Object entity, Object value);  }