@Test public void testSetCustomEditorsWithStringTypeName() throws Exception { DefaultPropertyEditorRegistrar mapper = new DefaultPropertyEditorRegistrar(); mapper.setCustomEditors(Collections.singletonMap("java.lang.Long", new CustomNumberEditor(Long.class, true))); BeanWithIntArray result = new BeanWithIntArray(); BeanWrapperImpl wrapper = new BeanWrapperImpl(result); mapper.registerCustomEditors(wrapper); wrapper.setPropertyValues(new MutablePropertyValues(Collections.singletonMap("number", "123"))); assertEquals(123L, result.number); }
@Test public void testIntArray() throws Exception { DefaultPropertyEditorRegistrar mapper = new DefaultPropertyEditorRegistrar(); BeanWithIntArray result = new BeanWithIntArray(); mapper.setCustomEditors(Collections.singletonMap(int[].class, new IntArrayPropertyEditor())); BeanWrapperImpl wrapper = new BeanWrapperImpl(result); mapper.registerCustomEditors(wrapper); PropertiesEditor editor = new PropertiesEditor(); editor.setAsText("numbers=1,2,3, 4"); Properties props = (Properties) editor.getValue(); wrapper.setPropertyValues(props); assertEquals(4, result.numbers[3]); }
@Test public void testPlainBeanWrapper() throws Exception { TestObject result = new TestObject(); BeanWrapperImpl wrapper = new BeanWrapperImpl(result); PropertiesEditor editor = new PropertiesEditor(); editor.setAsText("varString=This is some dummy string\nvarBoolean=true\nvarChar=C"); Properties props = (Properties) editor.getValue(); wrapper.setPropertyValues(props); assertEquals("This is some dummy string", result.getVarString()); assertEquals(true, result.isVarBoolean()); assertEquals('C', result.getVarChar()); }
public void setPropertyValues(PropertyValues pvs) throws BeansException { setPropertyValues(pvs, false); }
/** * Bulk update from a Map. * Bulk updates from PropertyValues are more powerful: this method is * provided for convenience. * @param map map containing properties to set, as name-value pairs. * The map may include nested properties. * @throws BeansException if there's a fatal, low-level exception */ public void setPropertyValues(Map map) throws BeansException { setPropertyValues(new MutablePropertyValues(map)); }
/** * Sets properties from the given Map. * @see MutablePropertyValues#addPropertyValues(Map) * @since 8.0 */ public static void setProperties(Object bean, Map<String, ?> properties) { new BeanWrapperImpl(bean).setPropertyValues(new MutablePropertyValues(properties)); }
@SuppressWarnings("unchecked") public static <T> T transferMapToMessage(Class<T> clazz, Map<String, String> map) { MutablePropertyValues propertyValues = toPropertyValues(map); BeanWrapperImpl beanWrapper = new BeanWrapperImpl(clazz); beanWrapper.registerCustomEditor(byte[].class, new ByteArrayBase64PropertyEditor()); beanWrapper.setAutoGrowNestedPaths(true); beanWrapper.setPropertyValues(propertyValues); return (T) beanWrapper.getWrappedInstance(); }
public T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { MultiValueMap<String, ?> map = delegate.read(null, inputMessage); BeanWrapperImpl beanWrapper = new BeanWrapperImpl(targetType); initBeanWrapper(beanWrapper); Map<String, Object> props = new HashMap<String, Object>(); for (String key : map.keySet()) { if (beanWrapper.isWritableProperty(key)) { List<?> list = map.get(key); props.put(key, map.get(key).size()>1 ? list : map.getFirst(key)); } } beanWrapper.setPropertyValues(props); @SuppressWarnings("unchecked") T result = (T) beanWrapper.getWrappedInstance(); return result; }