org.springframework.beans.PropertyEditorRegistry Java Examples

The following examples show how to use org.springframework.beans.PropertyEditorRegistry. 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 File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #3
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #4
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #5
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 File: GrailsDataBinder.java    From AlgoTrader with 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 File: ResourceEditorRegistrar.java    From blog_demos with 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 #8
Source File: ResourceEditorRegistrar.java    From spring-analysis-note with 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 #9
Source File: GrailsDataBinder.java    From AlgoTrader with 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 #10
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #11
Source File: QuartzSchedulerBeanRegistrar.java    From light-task-scheduler with Apache License 2.0 6 votes vote down vote up
@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 #12
Source File: ResourceEditorRegistrar.java    From java-technology-stack with 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 #13
Source File: ResourceEditorRegistrar.java    From lams with 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 #14
Source File: ObjectPropertyUtils.java    From rice with 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 #15
Source File: AbstractBindingResult.java    From spring-analysis-note with 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 #16
Source File: Binder.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link Binder} instance for the specified sources.
 *
 * @param sources                   the sources used for binding
 * @param placeholdersResolver      strategy to resolve any property placeholders
 * @param conversionService         the conversion service to convert values (or {@code null}
 *                                  to use {@link ApplicationConversionService})
 * @param propertyEditorInitializer initializer used to configure the property editors
 *                                  that can convert values (or {@code null} if no initialization is required). Often
 *                                  used to call {@link ConfigurableListableBeanFactory#copyRegisteredEditorsTo}.
 */
public Binder(Iterable<ConfigurationPropertySource> sources,
              PlaceholdersResolver placeholdersResolver,
              ConversionService conversionService,
              Consumer<PropertyEditorRegistry> propertyEditorInitializer) {
    Assert.notNull(sources, "Sources must not be null");
    this.sources = sources;
    this.placeholdersResolver = (placeholdersResolver != null) ? placeholdersResolver
            : PlaceholdersResolver.NONE;
    this.conversionService = (conversionService != null) ? conversionService
            : ApplicationConversionService.getSharedInstance();
    this.propertyEditorInitializer = propertyEditorInitializer;
}
 
Example #17
Source File: BindConverter.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
private SimpleTypeConverter createTypeConverter(
        Consumer<PropertyEditorRegistry> initializer) {
    SimpleTypeConverter typeConverter = new SimpleTypeConverter();
    if (initializer != null) {
        initializer.accept(typeConverter);
    }
    return typeConverter;
}
 
Example #18
Source File: BindConverter.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
private List<ConversionService> getConversionServices(
        ConversionService conversionService,
        Consumer<PropertyEditorRegistry> propertyEditorInitializer) {
    List<ConversionService> services = new ArrayList<>();
    services.add(new TypeConverterConversionService(propertyEditorInitializer));
    services.add(conversionService);
    if (!(conversionService instanceof ApplicationConversionService)) {
        services.add(ApplicationConversionService.getSharedInstance());
    }
    return services;
}
 
Example #19
Source File: AbstractBindingResult.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This implementation delegates to the
 * {@link #getPropertyEditorRegistry() PropertyEditorRegistry}'s
 * editor lookup facility, if available.
 */
@Override
public PropertyEditor findEditor(String field, 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 #20
Source File: BindConverter.java    From spring-cloud-gray with 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);
}
 
Example #21
Source File: ConfigurationPropertiesBinder.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
private Consumer<PropertyEditorRegistry> getPropertyEditorInitializer() {
    if (this.applicationContext instanceof ConfigurableApplicationContext) {
        return ((ConfigurableApplicationContext) this.applicationContext)
                .getBeanFactory()::copyRegisteredEditorsTo;
    }
    return null;
}
 
Example #22
Source File: DataBinder.java    From lams with GNU General Public License v2.0 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 #23
Source File: PropertyEditorUtil.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
/**
 * Register the property editors provided by uimaFIT in the given property editor registry.
 * 
 * @param aRegistry a property editor registry
 */
public static void registerUimaFITEditors(PropertyEditorRegistry aRegistry) {
  aRegistry.registerCustomEditor(Charset.class, new CharsetEditor());
  aRegistry.registerCustomEditor(Locale.class, new LocaleEditor());
  aRegistry.registerCustomEditor(String.class, new GetAsTextStringEditor(aRegistry));
  aRegistry.registerCustomEditor(LinkedList.class, new CustomCollectionEditor(LinkedList.class));
}
 
Example #24
Source File: CustomPropertyEditorRegistrar.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors(org.springframework.beans.PropertyEditorRegistry)
 */
@Override
public void registerCustomEditors(PropertyEditorRegistry registry)
{
    // add custom QName editor
    registry.registerCustomEditor(QName.class, new QNameTypeEditor(namespaceService));
}
 
Example #25
Source File: BindConverter.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
static BindConverter get(ConversionService conversionService,
                         Consumer<PropertyEditorRegistry> propertyEditorInitializer) {
    if (conversionService == ApplicationConversionService.getSharedInstance()
            && propertyEditorInitializer == null) {
        if (sharedInstance == null) {
            sharedInstance = new BindConverter(conversionService,
                    propertyEditorInitializer);
        }
        return sharedInstance;
    }
    return new BindConverter(conversionService, propertyEditorInitializer);
}
 
Example #26
Source File: ConcurrentBeanFactoryTests.java    From java-technology-stack with 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 #27
Source File: BeanFactoryGenericsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 File: BeanFactoryGenericsTests.java    From java-technology-stack with 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 #29
Source File: ResourceEditorRegistrar.java    From java-technology-stack with 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 #30
Source File: DataBinder.java    From java-technology-stack with 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();
	}
}