@Test public final void testUpdateDate() throws Exception { String message = "sets should be set appropriately"; final Activity activity = new Activity(); HashSet<Activity> coords = new HashSet<Activity>(); coords.add(activity); Date date = new Date(); HashMap<String, Serializable> map = new HashMap<String, Serializable>(); map.put("updated", date); ReflectiveUpdater reflector = new ReflectiveUpdater(); reflector.setProperties(activity, map); assertEquals(message, date, activity.getUpdated()); }
public void setProperties(final Object instance, final Map<String, Serializable> properties) { PropertyDescriptor[] descriptors; try { descriptors = Introspector.getBeanInfo(instance.getClass()).getPropertyDescriptors(); } catch (IntrospectionException ie) { String message = "Couldn't get bean info for " + instance.getClass(); throw new RuntimeException(message, ie); } ValidationException validationException = new ValidationException(); for (PropertyDescriptor property : descriptors) { String propertyName = property.getName(); if (!properties.containsKey(propertyName)) { continue; } Method setter = property.getWriteMethod(); if (setter == null) { continue; } Serializable valueObj = properties.get(propertyName); try { if (property.getPropertyType().equals(String.class)) { String value = (String) valueObj; if ("".equals(value)) { value = null; } setter.invoke(instance, value); continue; } if (property.getPropertyType().equals(List.class)) { setter.invoke(instance, (List) valueObj); continue; } if (property.getPropertyType().equals(HashMap.class)) { setter.invoke(instance, (HashMap) valueObj); continue; } if (property.getPropertyType().equals(Set.class)) { setter.invoke(instance, (Set) valueObj); continue; } if (property.getPropertyType().equals(boolean.class) || property.getPropertyType().equals(Boolean.class)) { setter.invoke(instance, ((Boolean) valueObj).booleanValue()); continue; } if (property.getPropertyType().equals(Integer.class) || property.getPropertyType().equals(int.class)) { setter.invoke(instance, (Integer) valueObj); continue; } if (property.getPropertyType().equals(float.class)) { setter.invoke(instance, ((Float) valueObj).floatValue()); continue; } if (property.getPropertyType().equals(Date.class)) { setter.invoke(instance, (Date) valueObj); continue; } validationException.addError(propertyName, "Type not found."); } catch (IllegalArgumentException e) { validationException.addError(propertyName, valueObj + " had an illegal argument"); } catch (IllegalAccessException e) { validationException.addError(propertyName, valueObj + " couldn't be accessed"); } catch (InvocationTargetException e) { validationException.addError(propertyName, valueObj + " couldn't be invoked"); } } ClassValidator validator = new ClassValidator(instance.getClass()); InvalidValue[] invalidValues = validator.getInvalidValues(instance); for (InvalidValue invalidValue : invalidValues) { validationException.addError(invalidValue.getPropertyName(), invalidValue.getMessage()); } if (validationException.getErrors().size() > 0) { throw validationException; } }
ReflectiveUpdater implements UpdaterStrategy { public void setProperties(final Object instance, final Map<String, Serializable> properties) { PropertyDescriptor[] descriptors; try { descriptors = Introspector.getBeanInfo(instance.getClass()).getPropertyDescriptors(); } catch (IntrospectionException ie) { String message = "Couldn't get bean info for " + instance.getClass(); throw new RuntimeException(message, ie); } ValidationException validationException = new ValidationException(); for (PropertyDescriptor property : descriptors) { String propertyName = property.getName(); if (!properties.containsKey(propertyName)) { continue; } Method setter = property.getWriteMethod(); if (setter == null) { continue; } Serializable valueObj = properties.get(propertyName); try { if (property.getPropertyType().equals(String.class)) { String value = (String) valueObj; if ("".equals(value)) { value = null; } setter.invoke(instance, value); continue; } if (property.getPropertyType().equals(List.class)) { setter.invoke(instance, (List) valueObj); continue; } if (property.getPropertyType().equals(HashMap.class)) { setter.invoke(instance, (HashMap) valueObj); continue; } if (property.getPropertyType().equals(Set.class)) { setter.invoke(instance, (Set) valueObj); continue; } if (property.getPropertyType().equals(boolean.class) || property.getPropertyType().equals(Boolean.class)) { setter.invoke(instance, ((Boolean) valueObj).booleanValue()); continue; } if (property.getPropertyType().equals(Integer.class) || property.getPropertyType().equals(int.class)) { setter.invoke(instance, (Integer) valueObj); continue; } if (property.getPropertyType().equals(float.class)) { setter.invoke(instance, ((Float) valueObj).floatValue()); continue; } if (property.getPropertyType().equals(Date.class)) { setter.invoke(instance, (Date) valueObj); continue; } validationException.addError(propertyName, "Type not found."); } catch (IllegalArgumentException e) { validationException.addError(propertyName, valueObj + " had an illegal argument"); } catch (IllegalAccessException e) { validationException.addError(propertyName, valueObj + " couldn't be accessed"); } catch (InvocationTargetException e) { validationException.addError(propertyName, valueObj + " couldn't be invoked"); } } ClassValidator validator = new ClassValidator(instance.getClass()); InvalidValue[] invalidValues = validator.getInvalidValues(instance); for (InvalidValue invalidValue : invalidValues) { validationException.addError(invalidValue.getPropertyName(), invalidValue.getMessage()); } if (validationException.getErrors().size() > 0) { throw validationException; } } }
ReflectiveUpdater implements UpdaterStrategy { public void setProperties(final Object instance, final Map<String, Serializable> properties) { PropertyDescriptor[] descriptors; try { descriptors = Introspector.getBeanInfo(instance.getClass()).getPropertyDescriptors(); } catch (IntrospectionException ie) { String message = "Couldn't get bean info for " + instance.getClass(); throw new RuntimeException(message, ie); } ValidationException validationException = new ValidationException(); for (PropertyDescriptor property : descriptors) { String propertyName = property.getName(); if (!properties.containsKey(propertyName)) { continue; } Method setter = property.getWriteMethod(); if (setter == null) { continue; } Serializable valueObj = properties.get(propertyName); try { if (property.getPropertyType().equals(String.class)) { String value = (String) valueObj; if ("".equals(value)) { value = null; } setter.invoke(instance, value); continue; } if (property.getPropertyType().equals(List.class)) { setter.invoke(instance, (List) valueObj); continue; } if (property.getPropertyType().equals(HashMap.class)) { setter.invoke(instance, (HashMap) valueObj); continue; } if (property.getPropertyType().equals(Set.class)) { setter.invoke(instance, (Set) valueObj); continue; } if (property.getPropertyType().equals(boolean.class) || property.getPropertyType().equals(Boolean.class)) { setter.invoke(instance, ((Boolean) valueObj).booleanValue()); continue; } if (property.getPropertyType().equals(Integer.class) || property.getPropertyType().equals(int.class)) { setter.invoke(instance, (Integer) valueObj); continue; } if (property.getPropertyType().equals(float.class)) { setter.invoke(instance, ((Float) valueObj).floatValue()); continue; } if (property.getPropertyType().equals(Date.class)) { setter.invoke(instance, (Date) valueObj); continue; } validationException.addError(propertyName, "Type not found."); } catch (IllegalArgumentException e) { validationException.addError(propertyName, valueObj + " had an illegal argument"); } catch (IllegalAccessException e) { validationException.addError(propertyName, valueObj + " couldn't be accessed"); } catch (InvocationTargetException e) { validationException.addError(propertyName, valueObj + " couldn't be invoked"); } } ClassValidator validator = new ClassValidator(instance.getClass()); InvalidValue[] invalidValues = validator.getInvalidValues(instance); for (InvalidValue invalidValue : invalidValues) { validationException.addError(invalidValue.getPropertyName(), invalidValue.getMessage()); } if (validationException.getErrors().size() > 0) { throw validationException; } } ReflectiveUpdater(); }
ReflectiveUpdater implements UpdaterStrategy { public void setProperties(final Object instance, final Map<String, Serializable> properties) { PropertyDescriptor[] descriptors; try { descriptors = Introspector.getBeanInfo(instance.getClass()).getPropertyDescriptors(); } catch (IntrospectionException ie) { String message = "Couldn't get bean info for " + instance.getClass(); throw new RuntimeException(message, ie); } ValidationException validationException = new ValidationException(); for (PropertyDescriptor property : descriptors) { String propertyName = property.getName(); if (!properties.containsKey(propertyName)) { continue; } Method setter = property.getWriteMethod(); if (setter == null) { continue; } Serializable valueObj = properties.get(propertyName); try { if (property.getPropertyType().equals(String.class)) { String value = (String) valueObj; if ("".equals(value)) { value = null; } setter.invoke(instance, value); continue; } if (property.getPropertyType().equals(List.class)) { setter.invoke(instance, (List) valueObj); continue; } if (property.getPropertyType().equals(HashMap.class)) { setter.invoke(instance, (HashMap) valueObj); continue; } if (property.getPropertyType().equals(Set.class)) { setter.invoke(instance, (Set) valueObj); continue; } if (property.getPropertyType().equals(boolean.class) || property.getPropertyType().equals(Boolean.class)) { setter.invoke(instance, ((Boolean) valueObj).booleanValue()); continue; } if (property.getPropertyType().equals(Integer.class) || property.getPropertyType().equals(int.class)) { setter.invoke(instance, (Integer) valueObj); continue; } if (property.getPropertyType().equals(float.class)) { setter.invoke(instance, ((Float) valueObj).floatValue()); continue; } if (property.getPropertyType().equals(Date.class)) { setter.invoke(instance, (Date) valueObj); continue; } validationException.addError(propertyName, "Type not found."); } catch (IllegalArgumentException e) { validationException.addError(propertyName, valueObj + " had an illegal argument"); } catch (IllegalAccessException e) { validationException.addError(propertyName, valueObj + " couldn't be accessed"); } catch (InvocationTargetException e) { validationException.addError(propertyName, valueObj + " couldn't be invoked"); } } ClassValidator validator = new ClassValidator(instance.getClass()); InvalidValue[] invalidValues = validator.getInvalidValues(instance); for (InvalidValue invalidValue : invalidValues) { validationException.addError(invalidValue.getPropertyName(), invalidValue.getMessage()); } if (validationException.getErrors().size() > 0) { throw validationException; } } ReflectiveUpdater(); void setProperties(final Object instance, final Map<String, Serializable> properties); }
ReflectiveUpdater implements UpdaterStrategy { public void setProperties(final Object instance, final Map<String, Serializable> properties) { PropertyDescriptor[] descriptors; try { descriptors = Introspector.getBeanInfo(instance.getClass()).getPropertyDescriptors(); } catch (IntrospectionException ie) { String message = "Couldn't get bean info for " + instance.getClass(); throw new RuntimeException(message, ie); } ValidationException validationException = new ValidationException(); for (PropertyDescriptor property : descriptors) { String propertyName = property.getName(); if (!properties.containsKey(propertyName)) { continue; } Method setter = property.getWriteMethod(); if (setter == null) { continue; } Serializable valueObj = properties.get(propertyName); try { if (property.getPropertyType().equals(String.class)) { String value = (String) valueObj; if ("".equals(value)) { value = null; } setter.invoke(instance, value); continue; } if (property.getPropertyType().equals(List.class)) { setter.invoke(instance, (List) valueObj); continue; } if (property.getPropertyType().equals(HashMap.class)) { setter.invoke(instance, (HashMap) valueObj); continue; } if (property.getPropertyType().equals(Set.class)) { setter.invoke(instance, (Set) valueObj); continue; } if (property.getPropertyType().equals(boolean.class) || property.getPropertyType().equals(Boolean.class)) { setter.invoke(instance, ((Boolean) valueObj).booleanValue()); continue; } if (property.getPropertyType().equals(Integer.class) || property.getPropertyType().equals(int.class)) { setter.invoke(instance, (Integer) valueObj); continue; } if (property.getPropertyType().equals(float.class)) { setter.invoke(instance, ((Float) valueObj).floatValue()); continue; } if (property.getPropertyType().equals(Date.class)) { setter.invoke(instance, (Date) valueObj); continue; } validationException.addError(propertyName, "Type not found."); } catch (IllegalArgumentException e) { validationException.addError(propertyName, valueObj + " had an illegal argument"); } catch (IllegalAccessException e) { validationException.addError(propertyName, valueObj + " couldn't be accessed"); } catch (InvocationTargetException e) { validationException.addError(propertyName, valueObj + " couldn't be invoked"); } } ClassValidator validator = new ClassValidator(instance.getClass()); InvalidValue[] invalidValues = validator.getInvalidValues(instance); for (InvalidValue invalidValue : invalidValues) { validationException.addError(invalidValue.getPropertyName(), invalidValue.getMessage()); } if (validationException.getErrors().size() > 0) { throw validationException; } } ReflectiveUpdater(); void setProperties(final Object instance, final Map<String, Serializable> properties);  }