Search in sources :

Example 71 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project grails-core by grails.

the class ScaleConstraintTests method proceedValidation.

private Object proceedValidation(Constraint constraint, Object value) {
    BeanWrapper constrainedBean = new BeanWrapperImpl(new TestClass());
    constrainedBean.setPropertyValue(constraint.getPropertyName(), value);
    Errors errors = new BindException(constrainedBean.getWrappedInstance(), constrainedBean.getWrappedClass().getName());
    assertFalse(errors.hasErrors());
    constraint.validate(constrainedBean.getWrappedInstance(), value, errors);
    return constrainedBean.getPropertyValue(constraint.getPropertyName());
}
Also used : Errors(org.springframework.validation.Errors) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BindException(org.springframework.validation.BindException) TestClass(grails.validation.TestClass)

Example 72 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project spring-boot by spring-projects.

the class RelaxedDataBinder method modifyProperties.

/**
	 * Modify the property values so that period separated property paths are valid for
	 * map keys. Also creates new maps for properties of map type that are null (assuming
	 * all maps are potentially nested). The standard bracket {@code[...]} dereferencing
	 * is also accepted.
	 * @param propertyValues the property values
	 * @param target the target object
	 * @return modified property values
	 */
private MutablePropertyValues modifyProperties(MutablePropertyValues propertyValues, Object target) {
    propertyValues = getPropertyValuesForNamePrefix(propertyValues);
    if (target instanceof MapHolder) {
        propertyValues = addMapPrefix(propertyValues);
    }
    BeanWrapper wrapper = new BeanWrapperImpl(target);
    wrapper.setConversionService(new RelaxedConversionService(getConversionService()));
    wrapper.setAutoGrowNestedPaths(true);
    List<PropertyValue> sortedValues = new ArrayList<>();
    Set<String> modifiedNames = new HashSet<>();
    List<String> sortedNames = getSortedPropertyNames(propertyValues);
    for (String name : sortedNames) {
        PropertyValue propertyValue = propertyValues.getPropertyValue(name);
        PropertyValue modifiedProperty = modifyProperty(wrapper, propertyValue);
        if (modifiedNames.add(modifiedProperty.getName())) {
            sortedValues.add(modifiedProperty);
        }
    }
    return new MutablePropertyValues(sortedValues);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) ArrayList(java.util.ArrayList) PropertyValue(org.springframework.beans.PropertyValue) HashSet(java.util.HashSet)

Example 73 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project spring-cloud-connectors by spring-cloud.

the class RedisConnectionFactoryConfigurer method configurePool.

private void configurePool(JedisConnectionFactory connectionFactory, PooledServiceConnectorConfig config) {
    if (config.getPoolConfig() != null) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        BeanWrapper target = new BeanWrapperImpl(poolConfig);
        BeanWrapper source = new BeanWrapperImpl(config.getPoolConfig());
        Util.setCorrespondingProperties(target, source);
        connectionFactory.setPoolConfig(poolConfig);
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig)

Example 74 with BeanWrapper

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

the class AnnotationBeanUtils method copyPropertiesToBean.

/**
	 * Copy the properties of the supplied {@link Annotation} to the supplied target bean.
	 * Any properties defined in {@code excludedProperties} will not be copied.
	 * <p>A specified value resolver may resolve placeholders in property values, for example.
	 * @param ann the annotation to copy from
	 * @param bean the bean instance to copy to
	 * @param valueResolver a resolve to post-process String property values (may be {@code null})
	 * @param excludedProperties the names of excluded properties, if any
	 * @see org.springframework.beans.BeanWrapper
	 */
public static void copyPropertiesToBean(Annotation ann, Object bean, StringValueResolver valueResolver, String... excludedProperties) {
    Set<String> excluded = new HashSet<>(Arrays.asList(excludedProperties));
    Method[] annotationProperties = ann.annotationType().getDeclaredMethods();
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean);
    for (Method annotationProperty : annotationProperties) {
        String propertyName = annotationProperty.getName();
        if (!excluded.contains(propertyName) && bw.isWritableProperty(propertyName)) {
            Object value = ReflectionUtils.invokeMethod(annotationProperty, ann);
            if (valueResolver != null && value instanceof String) {
                value = valueResolver.resolveStringValue((String) value);
            }
            bw.setPropertyValue(propertyName, value);
        }
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) Method(java.lang.reflect.Method) HashSet(java.util.HashSet)

Example 75 with BeanWrapper

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

the class AbstractAutowireCapableBeanFactory method configureBean.

@Override
public Object configureBean(Object existingBean, String beanName) throws BeansException {
    markBeanAsCreated(beanName);
    BeanDefinition mbd = getMergedBeanDefinition(beanName);
    RootBeanDefinition bd = null;
    if (mbd instanceof RootBeanDefinition) {
        RootBeanDefinition rbd = (RootBeanDefinition) mbd;
        bd = (rbd.isPrototype() ? rbd : rbd.cloneBeanDefinition());
    }
    if (!mbd.isPrototype()) {
        if (bd == null) {
            bd = new RootBeanDefinition(mbd);
        }
        bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
        bd.allowCaching = ClassUtils.isCacheSafe(ClassUtils.getUserClass(existingBean), getBeanClassLoader());
    }
    BeanWrapper bw = new BeanWrapperImpl(existingBean);
    initBeanWrapper(bw);
    populateBean(beanName, bd, bw);
    return initializeBean(beanName, existingBean, bd);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

BeanWrapper (org.springframework.beans.BeanWrapper)144 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)79 Test (org.junit.jupiter.api.Test)31 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)18 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)18 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 Consumes (javax.ws.rs.Consumes)16 PUT (javax.ws.rs.PUT)16 Path (javax.ws.rs.Path)16 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 HashSet (java.util.HashSet)12 BeansException (org.springframework.beans.BeansException)12 PropertyDescriptor (java.beans.PropertyDescriptor)11 OnmsNode (org.opennms.netmgt.model.OnmsNode)9 Test (org.junit.Test)6 BigDecimal (java.math.BigDecimal)5