Java Code Examples for org.springframework.beans.PropertyEditorRegistry

The following examples show how to use org.springframework.beans.PropertyEditorRegistry. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: AbstractBindingResult.java    License: MIT License 6 votes vote down vote up
/**
 * This implementation delegates to the
 * {@link #getPropertyEditorRegistry() PropertyEditorRegistry}'s
 * editor lookup facility, if available.
 */
@Override
@Nullable
public PropertyEditor findEditor(@Nullable String field, @Nullable Class<?> valueType) {
	PropertyEditorRegistry editorRegistry = getPropertyEditorRegistry();
	if (editorRegistry != null) {
		Class<?> valueTypeToUse = valueType;
		if (valueTypeToUse == null) {
			valueTypeToUse = getFieldType(field);
		}
		return editorRegistry.findCustomEditor(valueTypeToUse, fixedField(field));
	}
	else {
		return null;
	}
}
 
Example 2
Source Project: AlgoTrader   Source File: GrailsDataBinder.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Registers all known
 *
 * @param registry
 * @param locale
 */
public static void registerCustomEditors(PropertyEditorRegistry registry, Locale locale) {
	// Formatters for the different number types.
	NumberFormat floatFormat = NumberFormat.getInstance(locale);
	NumberFormat integerFormat = NumberFormat.getIntegerInstance(locale);

	DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT, locale);

	registry.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	registry.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, floatFormat, true));
	registry.registerCustomEditor(BigInteger.class, new CustomNumberEditor(BigInteger.class, floatFormat, true));
	registry.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, floatFormat, true));
	registry.registerCustomEditor(double.class, new CustomNumberEditor(Double.class, floatFormat, true));
	registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, floatFormat, true));
	registry.registerCustomEditor(float.class, new CustomNumberEditor(Float.class, floatFormat, true));
	registry.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, integerFormat, true));
	registry.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, integerFormat, true));
	registry.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, integerFormat, true));
	registry.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, integerFormat, true));
	registry.registerCustomEditor(Short.class, new CustomNumberEditor(Short.class, integerFormat, true));
	registry.registerCustomEditor(short.class, new CustomNumberEditor(Short.class, integerFormat, true));
	registry.registerCustomEditor(Date.class, new StructuredDateEditor(dateFormat, true));
	registry.registerCustomEditor(Calendar.class, new StructuredDateEditor(dateFormat, true));

	registerCustomEditors(registry);
}
 
Example 3
Source Project: spring-analysis-note   Source File: ResourceEditorRegistrar.java    License: MIT License 6 votes vote down vote up
/**
 * Populate the given {@code registry} with the following resource editors:
 * ResourceEditor, InputStreamEditor, InputSourceEditor, FileEditor, URLEditor,
 * URIEditor, ClassEditor, ClassArrayEditor.
 * <p>If this registrar has been configured with a {@link ResourcePatternResolver},
 * a ResourceArrayPropertyEditor will be registered as well.
 * @see org.springframework.core.io.ResourceEditor
 * @see org.springframework.beans.propertyeditors.InputStreamEditor
 * @see org.springframework.beans.propertyeditors.InputSourceEditor
 * @see org.springframework.beans.propertyeditors.FileEditor
 * @see org.springframework.beans.propertyeditors.URLEditor
 * @see org.springframework.beans.propertyeditors.URIEditor
 * @see org.springframework.beans.propertyeditors.ClassEditor
 * @see org.springframework.beans.propertyeditors.ClassArrayEditor
 * @see org.springframework.core.io.support.ResourceArrayPropertyEditor
 */
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
	ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
	doRegisterEditor(registry, Resource.class, baseEditor);
	doRegisterEditor(registry, ContextResource.class, baseEditor);
	doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
	doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
	doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
	doRegisterEditor(registry, Path.class, new PathEditor(baseEditor));
	doRegisterEditor(registry, Reader.class, new ReaderEditor(baseEditor));
	doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));

	ClassLoader classLoader = this.resourceLoader.getClassLoader();
	doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
	doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
	doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));

	if (this.resourceLoader instanceof ResourcePatternResolver) {
		doRegisterEditor(registry, Resource[].class,
				new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
	}
}
 
Example 4
@Test
public void testCustomEditor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1,1");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 5
@Test
public void testCustomEditorWithBeanReference() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", new RuntimeBeanReference("myFloat"));
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	lbf.registerSingleton("myFloat", "1,1");
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 6
Source Project: AlgoTrader   Source File: GrailsDataBinder.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Collects all PropertyEditorRegistrars in the application context and
 * calls them to register their custom editors
 *
 * @param registry The PropertyEditorRegistry instance
 */
private static void registerCustomEditors(PropertyEditorRegistry registry) {
	final ServletContext servletContext = ServletContextHolder.getServletContext();
	if (servletContext == null) {
		return;
	}

	WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
	if (context == null) {
		return;
	}

	Map<String, PropertyEditorRegistrar> editors = context.getBeansOfType(PropertyEditorRegistrar.class);
	for (PropertyEditorRegistrar editorRegistrar : editors.values()) {
		editorRegistrar.registerCustomEditors(registry);
	}
}
 
Example 7
Source Project: java-technology-stack   Source File: ResourceEditorRegistrar.java    License: MIT License 6 votes vote down vote up
/**
 * Populate the given {@code registry} with the following resource editors:
 * ResourceEditor, InputStreamEditor, InputSourceEditor, FileEditor, URLEditor,
 * URIEditor, ClassEditor, ClassArrayEditor.
 * <p>If this registrar has been configured with a {@link ResourcePatternResolver},
 * a ResourceArrayPropertyEditor will be registered as well.
 * @see org.springframework.core.io.ResourceEditor
 * @see org.springframework.beans.propertyeditors.InputStreamEditor
 * @see org.springframework.beans.propertyeditors.InputSourceEditor
 * @see org.springframework.beans.propertyeditors.FileEditor
 * @see org.springframework.beans.propertyeditors.URLEditor
 * @see org.springframework.beans.propertyeditors.URIEditor
 * @see org.springframework.beans.propertyeditors.ClassEditor
 * @see org.springframework.beans.propertyeditors.ClassArrayEditor
 * @see org.springframework.core.io.support.ResourceArrayPropertyEditor
 */
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
	ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
	doRegisterEditor(registry, Resource.class, baseEditor);
	doRegisterEditor(registry, ContextResource.class, baseEditor);
	doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
	doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
	doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
	doRegisterEditor(registry, Path.class, new PathEditor(baseEditor));
	doRegisterEditor(registry, Reader.class, new ReaderEditor(baseEditor));
	doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));

	ClassLoader classLoader = this.resourceLoader.getClassLoader();
	doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
	doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
	doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));

	if (this.resourceLoader instanceof ResourcePatternResolver) {
		doRegisterEditor(registry, Resource[].class,
				new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
	}
}
 
Example 8
@Test
public void testCustomEditor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1,1");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 9
@Test
public void testCustomEditorWithBeanReference() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", new RuntimeBeanReference("myFloat"));
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	lbf.registerSingleton("myFloat", "1,1");
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 10
Source Project: lams   Source File: ResourceEditorRegistrar.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Populate the given {@code registry} with the following resource editors:
 * ResourceEditor, InputStreamEditor, InputSourceEditor, FileEditor, URLEditor,
 * URIEditor, ClassEditor, ClassArrayEditor.
 * <p>If this registrar has been configured with a {@link ResourcePatternResolver},
 * a ResourceArrayPropertyEditor will be registered as well.
 * @see org.springframework.core.io.ResourceEditor
 * @see org.springframework.beans.propertyeditors.InputStreamEditor
 * @see org.springframework.beans.propertyeditors.InputSourceEditor
 * @see org.springframework.beans.propertyeditors.FileEditor
 * @see org.springframework.beans.propertyeditors.URLEditor
 * @see org.springframework.beans.propertyeditors.URIEditor
 * @see org.springframework.beans.propertyeditors.ClassEditor
 * @see org.springframework.beans.propertyeditors.ClassArrayEditor
 * @see org.springframework.core.io.support.ResourceArrayPropertyEditor
 */
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
	ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
	doRegisterEditor(registry, Resource.class, baseEditor);
	doRegisterEditor(registry, ContextResource.class, baseEditor);
	doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
	doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
	doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
	if (pathClass != null) {
		doRegisterEditor(registry, pathClass, new PathEditor(baseEditor));
	}
	doRegisterEditor(registry, Reader.class, new ReaderEditor(baseEditor));
	doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));

	ClassLoader classLoader = this.resourceLoader.getClassLoader();
	doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
	doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
	doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));

	if (this.resourceLoader instanceof ResourcePatternResolver) {
		doRegisterEditor(registry, Resource[].class,
				new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
	}
}
 
Example 11
Source Project: blog_demos   Source File: ResourceEditorRegistrar.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Populate the given {@code registry} with the following resource editors:
 * ResourceEditor, InputStreamEditor, InputSourceEditor, FileEditor, URLEditor,
 * URIEditor, ClassEditor, ClassArrayEditor.
 * <p>If this registrar has been configured with a {@link ResourcePatternResolver},
 * a ResourceArrayPropertyEditor will be registered as well.
 * @see ResourceEditor
 * @see org.springframework.beans.propertyeditors.InputStreamEditor
 * @see org.springframework.beans.propertyeditors.InputSourceEditor
 * @see org.springframework.beans.propertyeditors.FileEditor
 * @see org.springframework.beans.propertyeditors.URLEditor
 * @see org.springframework.beans.propertyeditors.URIEditor
 * @see org.springframework.beans.propertyeditors.ClassEditor
 * @see org.springframework.beans.propertyeditors.ClassArrayEditor
 * @see ResourceArrayPropertyEditor
 */
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
	ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
	doRegisterEditor(registry, Resource.class, baseEditor);
	doRegisterEditor(registry, ContextResource.class, baseEditor);
	doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
	doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
	doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
	doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));

	ClassLoader classLoader = this.resourceLoader.getClassLoader();
	doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
	doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
	doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));

	if (this.resourceLoader instanceof ResourcePatternResolver) {
		doRegisterEditor(registry, Resource[].class,
				new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
	}
}
 
Example 12
@Test
public void testCustomEditor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1,1");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 13
@Test
public void testCustomEditorWithBeanReference() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", new RuntimeBeanReference("myFloat"));
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	lbf.registerSingleton("myFloat", "1,1");
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 14
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
    if (!(registry instanceof BeanWrapperImpl)) {
        return;
    }

    BeanWrapperImpl beanWrapper = (BeanWrapperImpl) registry;

    Class<?> clazz = null;
    try {
        clazz = Class.forName(SchedulerFactoryBean, true, registry.getClass().getClassLoader());
    } catch (Throwable e) {
        LOGGER.info("cannot find class for " + SchedulerFactoryBean, e);
    }

    if (null == clazz
            || null == beanWrapper.getWrappedClass()
            || !clazz.isAssignableFrom(beanWrapper.getWrappedClass())) {
        return;
    }

    registry.registerCustomEditor(Object.class, "triggers",
            new QuartzSchedulerBeanTargetEditor(context));
}
 
Example 15
Source Project: rice   Source File: ObjectPropertyUtils.java    License: Educational Community License v2.0 6 votes vote down vote up
/**
 * Registers a default set of property editors for use with KRAD in a given property editor registry.
 *
 * @param registry property editor registry
 */
public static void registerPropertyEditors(PropertyEditorRegistry registry) {
    DataDictionaryService dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
    Map<Class<?>, String> propertyEditorMap = dataDictionaryService.getPropertyEditorMap();

    if (propertyEditorMap == null) {
        LOG.warn("No propertyEditorMap defined in data dictionary");
        return;
    }

    for (Entry<Class<?>, String> propertyEditorEntry : propertyEditorMap.entrySet()) {
        PropertyEditor editor = (PropertyEditor) dataDictionaryService.getDataDictionary().getDictionaryPrototype(
                propertyEditorEntry.getValue());
        registry.registerCustomEditor(propertyEditorEntry.getKey(), editor);

        if (LOG.isDebugEnabled()) {
            LOG.debug("registered " + propertyEditorEntry);
        }
    }
}
 
Example 16
@Test
public void testGenericMapWithCollectionValueFactoryMethod() throws MalformedURLException {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
		}
	});
	RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
	rbd.setFactoryMethodName("createInstance");

	Map<String, AbstractCollection<?>> input = new HashMap<String, AbstractCollection<?>>();
	HashSet<Integer> value1 = new HashSet<Integer>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<Boolean>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(input);

	bf.registerBeanDefinition("genericBean", rbd);
	GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 17
Source Project: spring-analysis-note   Source File: DataBinder.java    License: MIT License 5 votes vote down vote up
/**
 * Return the underlying TypeConverter of this binder's BindingResult.
 */
protected PropertyEditorRegistry getPropertyEditorRegistry() {
	if (getTarget() != null) {
		return getInternalBindingResult().getPropertyAccessor();
	}
	else {
		return getSimpleTypeConverter();
	}
}
 
Example 18
Source Project: rice   Source File: ObjectPropertyUtils.java    License: Educational Community License v2.0 5 votes vote down vote up
/**
 * Gets a property editor given a specific bean and property path.
 * 
 * @param bean The bean instance.
 * @param path The property path.
 * @return property editor
 */
public static PropertyEditor getPropertyEditor(Object bean, String path) {
    Class<?> propertyType = getPrimitiveType(getPropertyType(bean, path));
    
    PropertyEditor editor = null;

    PropertyEditorRegistry registry = getPropertyEditorRegistry();
    if (registry != null) {
        editor = registry.findCustomEditor(propertyType, path);
        
        if (editor != null && editor != registry.findCustomEditor(propertyType, null)) {
            return editor;
        }
        
        if (registry instanceof BeanWrapper
                && bean == ((BeanWrapper) registry).getWrappedInstance()
                && (bean instanceof ViewModel)) {
            
            ViewModel viewModel = (ViewModel) bean;
            ViewPostMetadata viewPostMetadata = viewModel.getViewPostMetadata();
            PropertyEditor editorFromView = viewPostMetadata == null ? null : viewPostMetadata.getFieldEditor(path);

            if (editorFromView != null) {
                registry.registerCustomEditor(propertyType, path, editorFromView);
                editor = registry.findCustomEditor(propertyType, path);
            }
        }
    }

    if (editor != null) {
        return editor;
    }
    
    return getPropertyEditor(propertyType);
}
 
Example 19
Source Project: spring-analysis-note   Source File: AbstractBeanFactory.java    License: MIT License 5 votes vote down vote up
/**
 * Initialize the given PropertyEditorRegistry with the custom editors
 * that have been registered with this BeanFactory.
 * <p>To be called for BeanWrappers that will create and populate bean
 * instances, and for SimpleTypeConverter used for constructor argument
 * and factory method type conversion.
 * @param registry the PropertyEditorRegistry to initialize
 */
protected void registerCustomEditors(PropertyEditorRegistry registry) {
	PropertyEditorRegistrySupport registrySupport =
			(registry instanceof PropertyEditorRegistrySupport ? (PropertyEditorRegistrySupport) registry : null);
	if (registrySupport != null) {
		registrySupport.useConfigValueEditors();
	}
	if (!this.propertyEditorRegistrars.isEmpty()) {
		for (PropertyEditorRegistrar registrar : this.propertyEditorRegistrars) {
			try {
				registrar.registerCustomEditors(registry);
			}
			catch (BeanCreationException ex) {
				Throwable rootCause = ex.getMostSpecificCause();
				if (rootCause instanceof BeanCurrentlyInCreationException) {
					BeanCreationException bce = (BeanCreationException) rootCause;
					String bceBeanName = bce.getBeanName();
					if (bceBeanName != null && isCurrentlyInCreation(bceBeanName)) {
						if (logger.isDebugEnabled()) {
							logger.debug("PropertyEditorRegistrar [" + registrar.getClass().getName() +
									"] failed because it tried to obtain currently created bean '" +
									ex.getBeanName() + "': " + ex.getMessage());
						}
						onSuppressedException(ex);
						continue;
					}
				}
				throw ex;
			}
		}
	}
	if (!this.customEditors.isEmpty()) {
		this.customEditors.forEach((requiredType, editorClass) ->
				registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass)));
	}
}
 
Example 20
Source Project: spring-analysis-note   Source File: ResourceEditorRegistrar.java    License: MIT License 5 votes vote down vote up
/**
 * Override default editor, if possible (since that's what we really mean to do here);
 * otherwise register as a custom editor.
 */
private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
	if (registry instanceof PropertyEditorRegistrySupport) {
		((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
	}
	else {
		registry.registerCustomEditor(requiredType, editor);
	}
}
 
Example 21
Source Project: spring-analysis-note   Source File: BeanFactoryGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericMapWithCollectionValueConstructor() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
		}
	});
	RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);

	Map<String, AbstractCollection<?>> input = new HashMap<>();
	HashSet<Integer> value1 = new HashSet<>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(input);

	bf.registerBeanDefinition("genericBean", rbd);
	GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 22
Source Project: spring-analysis-note   Source File: BeanFactoryGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericMapWithCollectionValueFactoryMethod() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
		}
	});
	RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
	rbd.setFactoryMethodName("createInstance");

	Map<String, AbstractCollection<?>> input = new HashMap<>();
	HashSet<Integer> value1 = new HashSet<>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(input);

	bf.registerBeanDefinition("genericBean", rbd);
	GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 23
Source Project: java-technology-stack   Source File: DataBinder.java    License: MIT License 5 votes vote down vote up
/**
 * Return the underlying TypeConverter of this binder's BindingResult.
 */
protected PropertyEditorRegistry getPropertyEditorRegistry() {
	if (getTarget() != null) {
		return getInternalBindingResult().getPropertyAccessor();
	}
	else {
		return getSimpleTypeConverter();
	}
}
 
Example 24
Source Project: java-technology-stack   Source File: AbstractBeanFactory.java    License: MIT License 5 votes vote down vote up
/**
 * Initialize the given PropertyEditorRegistry with the custom editors
 * that have been registered with this BeanFactory.
 * <p>To be called for BeanWrappers that will create and populate bean
 * instances, and for SimpleTypeConverter used for constructor argument
 * and factory method type conversion.
 * @param registry the PropertyEditorRegistry to initialize
 */
protected void registerCustomEditors(PropertyEditorRegistry registry) {
	PropertyEditorRegistrySupport registrySupport =
			(registry instanceof PropertyEditorRegistrySupport ? (PropertyEditorRegistrySupport) registry : null);
	if (registrySupport != null) {
		registrySupport.useConfigValueEditors();
	}
	if (!this.propertyEditorRegistrars.isEmpty()) {
		for (PropertyEditorRegistrar registrar : this.propertyEditorRegistrars) {
			try {
				registrar.registerCustomEditors(registry);
			}
			catch (BeanCreationException ex) {
				Throwable rootCause = ex.getMostSpecificCause();
				if (rootCause instanceof BeanCurrentlyInCreationException) {
					BeanCreationException bce = (BeanCreationException) rootCause;
					String bceBeanName = bce.getBeanName();
					if (bceBeanName != null && isCurrentlyInCreation(bceBeanName)) {
						if (logger.isDebugEnabled()) {
							logger.debug("PropertyEditorRegistrar [" + registrar.getClass().getName() +
									"] failed because it tried to obtain currently created bean '" +
									ex.getBeanName() + "': " + ex.getMessage());
						}
						onSuppressedException(ex);
						continue;
					}
				}
				throw ex;
			}
		}
	}
	if (!this.customEditors.isEmpty()) {
		this.customEditors.forEach((requiredType, editorClass) ->
				registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass)));
	}
}
 
Example 25
Source Project: java-technology-stack   Source File: ResourceEditorRegistrar.java    License: MIT License 5 votes vote down vote up
/**
 * Override default editor, if possible (since that's what we really mean to do here);
 * otherwise register as a custom editor.
 */
private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
	if (registry instanceof PropertyEditorRegistrySupport) {
		((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
	}
	else {
		registry.registerCustomEditor(requiredType, editor);
	}
}
 
Example 26
Source Project: java-technology-stack   Source File: BeanFactoryGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericMapWithCollectionValueConstructor() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
		}
	});
	RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);

	Map<String, AbstractCollection<?>> input = new HashMap<>();
	HashSet<Integer> value1 = new HashSet<>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(input);

	bf.registerBeanDefinition("genericBean", rbd);
	GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 27
@Test
public void testGenericMapWithCollectionValueConstructor() throws MalformedURLException {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
		}
	});
	RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);

	Map<String, AbstractCollection<?>> input = new HashMap<String, AbstractCollection<?>>();
	HashSet<Integer> value1 = new HashSet<Integer>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<Boolean>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(input);

	bf.registerBeanDefinition("genericBean", rbd);
	GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 28
Source Project: java-technology-stack   Source File: ConcurrentBeanFactoryTests.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	Assume.group(TestGroup.PERFORMANCE);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(factory).loadBeanDefinitions(CONTEXT);
	factory.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Date.class, new CustomDateEditor((DateFormat) DATE_FORMAT.clone(), false));
		}
	});
	this.factory = factory;
}
 
Example 29
private Consumer<PropertyEditorRegistry> getPropertyEditorInitializer() {
    if (this.applicationContext instanceof ConfigurableApplicationContext) {
        return ((ConfigurableApplicationContext) this.applicationContext)
                .getBeanFactory()::copyRegisteredEditorsTo;
    }
    return null;
}
 
Example 30
Source Project: spring-cloud-gray   Source File: BindConverter.java    License: Apache License 2.0 5 votes vote down vote up
private BindConverter(ConversionService conversionService,
                      Consumer<PropertyEditorRegistry> propertyEditorInitializer) {
    Assert.notNull(conversionService, "ConversionService must not be null");
    List<ConversionService> conversionServices = getConversionServices(
            conversionService, propertyEditorInitializer);
    this.conversionService = new CompositeConversionService(conversionServices);
}