Search in sources :

Example 1 with SimpleTypeConverter

use of org.springframework.beans.SimpleTypeConverter in project spring-framework by spring-projects.

the class AbstractBeanFactory method getTypeConverter.

@Override
public TypeConverter getTypeConverter() {
    TypeConverter customConverter = getCustomTypeConverter();
    if (customConverter != null) {
        return customConverter;
    } else {
        // Build default TypeConverter, registering custom editors.
        SimpleTypeConverter typeConverter = new SimpleTypeConverter();
        typeConverter.setConversionService(getConversionService());
        registerCustomEditors(typeConverter);
        return typeConverter;
    }
}
Also used : TypeConverter(org.springframework.beans.TypeConverter) SimpleTypeConverter(org.springframework.beans.SimpleTypeConverter) SimpleTypeConverter(org.springframework.beans.SimpleTypeConverter)

Example 2 with SimpleTypeConverter

use of org.springframework.beans.SimpleTypeConverter in project mzzb-server by mingzuozhibi.

the class JsonArgumentResolver method readObject.

private <T> T readObject(String body, String name, String defaults, Class<T> requiredType) {
    SimpleTypeConverter converter = new SimpleTypeConverter();
    try {
        Class<?> readType = requiredType.isEnum() ? String.class : requiredType;
        Object read = JsonPath.parse(body).read(name, readType);
        return converter.convertIfNecessary(read, requiredType);
    } catch (PathNotFoundException e) {
        return converter.convertIfNecessary(defaults, requiredType);
    }
}
Also used : SimpleTypeConverter(org.springframework.beans.SimpleTypeConverter) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException)

Example 3 with SimpleTypeConverter

use of org.springframework.beans.SimpleTypeConverter in project com.revolsys.open by revolsys.

the class BeanConfigurrer method processOverride.

/**
 * Process the given key as 'beanName.property' entry.
 */
protected void processOverride(final ConfigurableListableBeanFactory factory, final String key, Object value) {
    try {
        if (value instanceof BeanReference) {
            final BeanReference reference = (BeanReference) value;
            value = reference.getBean();
        }
        final Matcher matcher = BeanConfigurrer.KEY_PATTERN.matcher(key);
        if (matcher.matches()) {
            final String beanName = matcher.group(1);
            final String mapKey = matcher.group(2);
            final String propertyName = matcher.group(3);
            if (mapKey == null) {
                if (propertyName == null) {
                    if (factory.containsBean(beanName)) {
                        BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);
                        try {
                            final ClassLoader classLoader = this.applicationContext.getClassLoader();
                            final String beanClassName = beanDefinition.getBeanClassName();
                            final Class<?> beanClass = Class.forName(beanClassName, true, classLoader);
                            if (Parameter.class.isAssignableFrom(beanClass)) {
                                while (beanDefinition.getOriginatingBeanDefinition() != null) {
                                    beanDefinition = beanDefinition.getOriginatingBeanDefinition();
                                }
                                final MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
                                PropertyValue propertyValue = new PropertyValue("value", value);
                                final PropertyValue typeValue = propertyValues.getPropertyValue("type");
                                if (typeValue != null) {
                                    try {
                                        final Class<?> typeClass;
                                        final Object typeValueObject = typeValue.getValue();
                                        if (typeValueObject instanceof Class<?>) {
                                            typeClass = (Class<?>) typeValueObject;
                                        } else {
                                            final String typeClassName = typeValueObject.toString();
                                            typeClass = Class.forName(typeClassName, true, classLoader);
                                        }
                                        final Object convertedValue = new SimpleTypeConverter().convertIfNecessary(value, typeClass);
                                        propertyValue = new PropertyValue("value", convertedValue);
                                    } catch (final Throwable e) {
                                        Logs.error(this, "Unable to set " + beanName + ".value=" + value, e);
                                    }
                                }
                                propertyValues.addPropertyValue(propertyValue);
                            }
                        } catch (final ClassNotFoundException e) {
                            Logs.error(this, "Unable to set " + beanName + ".value=" + value, e);
                        }
                    } else if (value != null) {
                        newParameterBeanDefinition(factory, beanName, value);
                    }
                } else {
                    setAttributeValue(factory, beanName, propertyName, value);
                    Logs.debug(this, "Property '" + key + "' set to value [" + value + "]");
                }
            } else if (propertyName == null) {
                setMapValue(factory, key, beanName, mapKey, value);
            } else {
                Logs.error(this, "Invalid syntax unable to set " + key + "=" + value);
            }
        }
    } catch (final BeansException ex) {
        final String msg = "Could not process key '" + key + "' in PropertyOverrideConfigurer";
        if (!this.ignoreInvalidKeys) {
            throw new BeanInitializationException(msg, ex);
        }
        Logs.debug(this, msg, ex);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Matcher(java.util.regex.Matcher) PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) SimpleTypeConverter(org.springframework.beans.SimpleTypeConverter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanReference(com.revolsys.spring.BeanReference) BeansException(org.springframework.beans.BeansException)

Example 4 with SimpleTypeConverter

use of org.springframework.beans.SimpleTypeConverter in project com.revolsys.open by revolsys.

the class BeanConfigurrer method setAttributeValue.

/**
 * Apply the given property value to the corresponding bean.
 */
public static void setAttributeValue(final ConfigurableListableBeanFactory factory, final String beanName, final String property, final Object value) {
    final ClassLoader classLoader = factory.getBeanClassLoader();
    BeanDefinition bd = factory.getBeanDefinition(beanName);
    while (bd.getOriginatingBeanDefinition() != null) {
        bd = bd.getOriginatingBeanDefinition();
    }
    final MutablePropertyValues propertyValues = bd.getPropertyValues();
    PropertyValue propertyValue = new PropertyValue(property, value);
    final String beanClassName = bd.getBeanClassName();
    if (!TargetBeanFactoryBean.class.getName().equals(beanClassName)) {
        if (Parameter.class.getName().equals(beanClassName)) {
            final PropertyValue typeValue = propertyValues.getPropertyValue("type");
            if (typeValue != null) {
                final String typeClassName = typeValue.getValue().toString();
                try {
                    final Class<?> typeClass = Class.forName(typeClassName, true, classLoader);
                    final Object convertedValue = new SimpleTypeConverter().convertIfNecessary(value, typeClass);
                    propertyValue = new PropertyValue(property, convertedValue);
                } catch (final Throwable e) {
                    Logs.error(BeanConfigurrer.class, "Unable to set " + beanName + "." + property + "=" + value, e);
                }
            }
        }
        propertyValues.addPropertyValue(propertyValue);
    }
}
Also used : SimpleTypeConverter(org.springframework.beans.SimpleTypeConverter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValue(org.springframework.beans.PropertyValue) Parameter(com.revolsys.spring.factory.Parameter) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition)

Example 5 with SimpleTypeConverter

use of org.springframework.beans.SimpleTypeConverter in project spring-integration by spring-projects.

the class BeanFactoryTypeConverterTests method initialConcurrency.

@Test
public void initialConcurrency() throws Exception {
    // can convert nothing so we drop down to P.E.s
    ConversionService conversionService = mock(ConversionService.class);
    final BeanFactoryTypeConverter beanFactoryTypeConverter = new BeanFactoryTypeConverter(conversionService);
    ConfigurableBeanFactory beanFactory = mock(ConfigurableBeanFactory.class);
    SimpleTypeConverter typeConverter = spy(new SimpleTypeConverter());
    when(beanFactory.getTypeConverter()).thenReturn(typeConverter);
    final AtomicBoolean inGetDefaultEditor = new AtomicBoolean();
    final AtomicBoolean concurrentlyInGetDefaultEditor = new AtomicBoolean();
    final AtomicInteger count = new AtomicInteger();
    doAnswer(invocation -> {
        count.incrementAndGet();
        Thread.sleep(500);
        concurrentlyInGetDefaultEditor.set(inGetDefaultEditor.getAndSet(true));
        Thread.sleep(500);
        inGetDefaultEditor.set(false);
        return invocation.callRealMethod();
    }).when(typeConverter).getDefaultEditor(UUID.class);
    beanFactoryTypeConverter.setBeanFactory(beanFactory);
    final TypeDescriptor sourceType = TypeDescriptor.valueOf(UUID.class);
    final TypeDescriptor targetType = TypeDescriptor.valueOf(String.class);
    ExecutorService exec = Executors.newFixedThreadPool(2);
    Runnable test = () -> {
        beanFactoryTypeConverter.canConvert(sourceType, targetType);
        beanFactoryTypeConverter.convertValue(UUID.randomUUID(), sourceType, targetType);
    };
    exec.execute(test);
    exec.execute(test);
    exec.shutdown();
    assertTrue(exec.awaitTermination(10, TimeUnit.SECONDS));
    assertEquals(4, count.get());
    assertFalse(concurrentlyInGetDefaultEditor.get());
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SimpleTypeConverter(org.springframework.beans.SimpleTypeConverter) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConversionService(org.springframework.core.convert.ConversionService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Aggregations

SimpleTypeConverter (org.springframework.beans.SimpleTypeConverter)6 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 PropertyValue (org.springframework.beans.PropertyValue)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)1 BeanReference (com.revolsys.spring.BeanReference)1 Parameter (com.revolsys.spring.factory.Parameter)1 Annotation (java.lang.annotation.Annotation)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Matcher (java.util.regex.Matcher)1 Test (org.junit.Test)1 BeansException (org.springframework.beans.BeansException)1 TypeConverter (org.springframework.beans.TypeConverter)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)1 ConversionService (org.springframework.core.convert.ConversionService)1 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)1