/** * Convert the given value to a Map of the target type. */ @Override public void setValue(@Nullable Object value) { if (value == null && this.nullAsEmptyMap) { super.setValue(createMap(this.mapType, 0)); } else if (value == null || (this.mapType.isInstance(value) && !alwaysCreateNewMap())) { // Use the source value as-is, as it matches the target type. super.setValue(value); } else if (value instanceof Map) { // Convert Map elements. Map<?, ?> source = (Map<?, ?>) value; Map<Object, Object> target = createMap(this.mapType, source.size()); source.forEach((key, val) -> target.put(convertKey(key), convertValue(val))); super.setValue(target); } else { throw new IllegalArgumentException("Value cannot be converted to Map: " + value); } }
/** * Convert the given text value to a Map with a single element. */ @Override public void setAsText(String text) throws IllegalArgumentException { setValue(text); }
this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));
this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));
/** * Convert the given value to a Map of the target type. */ @Override public void setValue(@Nullable Object value) { if (value == null && this.nullAsEmptyMap) { super.setValue(createMap(this.mapType, 0)); } else if (value == null || (this.mapType.isInstance(value) && !alwaysCreateNewMap())) { // Use the source value as-is, as it matches the target type. super.setValue(value); } else if (value instanceof Map) { // Convert Map elements. Map<?, ?> source = (Map<?, ?>) value; Map<Object, Object> target = createMap(this.mapType, source.size()); source.forEach((key, val) -> target.put(convertKey(key), convertValue(val))); super.setValue(target); } else { throw new IllegalArgumentException("Value cannot be converted to Map: " + value); } }
@Test public void testConversionToOldCollections() throws PropertyVetoException { OldCollectionsBean tb = new OldCollectionsBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(Vector.class, new CustomCollectionEditor(Vector.class)); bw.registerCustomEditor(Hashtable.class, new CustomMapEditor(Hashtable.class)); bw.setPropertyValue("vector", new String[] {"a", "b"}); assertEquals(2, tb.getVector().size()); assertEquals("a", tb.getVector().get(0)); assertEquals("b", tb.getVector().get(1)); bw.setPropertyValue("hashtable", Collections.singletonMap("foo", "bar")); assertEquals(1, tb.getHashtable().size()); assertEquals("bar", tb.getHashtable().get("foo")); }
/** * Convert the given text value to a Map with a single element. */ @Override public void setAsText(String text) throws IllegalArgumentException { setValue(text); }
/** * Convert the given value to a Map of the target type. */ @Override public void setValue(Object value) { if (value == null && this.nullAsEmptyMap) { super.setValue(createMap(this.mapType, 0)); } else if (value == null || (this.mapType.isInstance(value) && !alwaysCreateNewMap())) { // Use the source value as-is, as it matches the target type. super.setValue(value); } else if (value instanceof Map) { // Convert Map elements. Map<?, ?> source = (Map) value; Map target = createMap(this.mapType, source.size()); for (Map.Entry entry : source.entrySet()) { target.put(convertKey(entry.getKey()), convertValue(entry.getValue())); } super.setValue(target); } else { throw new IllegalArgumentException("Value cannot be converted to Map: " + value); } }
this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));
/** * Convert the given text value to a Map with a single element. */ @Override public void setAsText(String text) throws IllegalArgumentException { setValue(text); }
/** * Convert the given value to a Map of the target type. */ @Override public void setValue(Object value) { if (value == null && this.nullAsEmptyMap) { super.setValue(createMap(this.mapType, 0)); } else if (value == null || (this.mapType.isInstance(value) && !alwaysCreateNewMap())) { // Use the source value as-is, as it matches the target type. super.setValue(value); } else if (value instanceof Map) { // Convert Map elements. Map<?, ?> source = (Map<?, ?>) value; Map<Object, Object> target = createMap(this.mapType, source.size()); for (Map.Entry<?, ?> entry : source.entrySet()) { target.put(convertKey(entry.getKey()), convertValue(entry.getValue())); } super.setValue(target); } else { throw new IllegalArgumentException("Value cannot be converted to Map: " + value); } }
public void registerCustomEditors(PropertyEditorRegistry registry) { for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : editors.entrySet()) { Class<?> type = entry.getKey(); PropertyEditor editorInstance; editorInstance = BeanUtils.instantiate(entry.getValue()); registry.registerCustomEditor(type, editorInstance); } // register non-externalized types registry.registerCustomEditor(Dictionary.class, new CustomMapEditor(Hashtable.class)); registry.registerCustomEditor(Properties.class, new PropertiesEditor()); registry.registerCustomEditor(Class.class, new ClassEditor(userClassLoader)); registry.registerCustomEditor(Class[].class, new ClassArrayEditor(userClassLoader)); } }
/** * Convert the given text value to a Map with a single element. */ @Override public void setAsText(String text) throws IllegalArgumentException { setValue(text); }
this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));
registry.registerCustomEditor(SortedSet.class, new BlueprintCustomCollectionEditor(SortedSet.class)); registry.registerCustomEditor(List.class, new BlueprintCustomCollectionEditor(List.class)); registry.registerCustomEditor(SortedMap.class, new CustomMapEditor(SortedMap.class)); registry.registerCustomEditor(HashMap.class, new CustomMapEditor(HashMap.class)); registry.registerCustomEditor(LinkedHashMap.class, new CustomMapEditor(LinkedHashMap.class)); registry.registerCustomEditor(Hashtable.class, new CustomMapEditor(Hashtable.class)); registry.registerCustomEditor(TreeMap.class, new CustomMapEditor(TreeMap.class)); registry.registerCustomEditor(Properties.class, new PropertiesEditor()); registry.registerCustomEditor(ConcurrentMap.class, new CustomMapEditor(ConcurrentHashMap.class)); registry.registerCustomEditor(ConcurrentHashMap.class, new CustomMapEditor(ConcurrentHashMap.class)); registry.registerCustomEditor(Queue.class, new BlueprintCustomCollectionEditor(LinkedList.class)); registry.registerCustomEditor(Dictionary.class, new CustomMapEditor(Hashtable.class));