PropertiesUtil { @SuppressWarnings("unchecked") public static void populate(Object instance, Map<String,?> config, boolean validate){ Class<?> objectClass = instance.getClass(); Map<String, Method> setters = getSetters(objectClass); ToStringHelper toStringHelper = Objects.toStringHelper(instance); for(Map.Entry<String,?> e : config.entrySet()) { String name = e.getKey(); Method setter = setters.get(name); if (setter != null) { if (setter.getAnnotation(Deprecated.class) != null) { log.warn("Parameter {} of {} has been deprecated", name, objectClass.getName()); } Object value = e.getValue(); setProperty(instance, name, setter, value); toStringHelper.add(name,value); } else if (validate) { throw new IllegalArgumentException( "Configured class " + objectClass.getName() + " does not contain a property named " + name); } } log.debug("Configured object with properties {}", toStringHelper); } private  PropertiesUtil(); static boolean toBoolean(Object propValue, boolean defaultValue); static String toString(Object propValue, String defaultValue); static long toLong(Object propValue, long defaultValue); static int toInteger(Object propValue, int defaultValue); static double toDouble(Object propValue, double defaultValue); static Object toObject(Object propValue); static String[] toStringArray(Object propValue); static String[] toStringArray(Object propValue, String[] defaultArray); @SuppressWarnings("unchecked") static void populate(Object instance, Map<String,?> config, boolean validate);  }
@Test public void testPopulate() { Map<String, Object> props = new HashMap<String, Object>(); props.put("string", "foo"); props.put("bool", "true"); props.put("integer", "7"); props.put("aLong", "11"); TestBeanA bean = new TestBeanA(); PropertiesUtil.populate(bean, props, false); assertEquals("foo", bean.getString()); assertTrue(bean.getBool()); assertEquals(7, bean.getInteger()); assertEquals(11, bean.getaLong()); }
@Test(expected = IllegalArgumentException.class) public void testPopulateAndValidate() { Map<String, Object> props = new HashMap<String, Object>(); props.put("something", "foo"); TestBeanA bean = new TestBeanA(); PropertiesUtil.populate(bean, props, true); }