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());
}
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);
}
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);
}
}
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);
}
}
}
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);
}
Aggregations