Search in sources :

Example 26 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-framework by TAKETODAY.

the class PropertyOverrideConfigurer method applyPropertyValue.

/**
 * Apply the given property value to the corresponding bean.
 */
protected void applyPropertyValue(ConfigurableBeanFactory factory, String beanName, String property, String value) {
    BeanDefinition bd = factory.getBeanDefinition(beanName);
    BeanDefinition bdToUse = bd;
    while (bd != null) {
        bdToUse = bd;
        bd = bd.getOriginatingBeanDefinition();
    }
    PropertyValue pv = new PropertyValue(property, value);
    pv.setOptional(this.ignoreInvalidKeys);
    bdToUse.getPropertyValues().add(pv);
}
Also used : PropertyValue(cn.taketoday.beans.PropertyValue)

Example 27 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-framework by TAKETODAY.

the class WebDataBinder method checkFieldDefaults.

/**
 * Check the given property values for field defaults,
 * i.e. for fields that start with the field default prefix.
 * <p>The existence of a field defaults indicates that the specified
 * value should be used if the field is otherwise not present.
 *
 * @param values the property values to be bound (can be modified)
 * @see #getFieldDefaultPrefix
 */
protected void checkFieldDefaults(PropertyValues values) {
    String fieldDefaultPrefix = getFieldDefaultPrefix();
    if (fieldDefaultPrefix != null) {
        ConfigurablePropertyAccessor propertyAccessor = getPropertyAccessor();
        for (PropertyValue pv : values.toArray()) {
            if (pv.getName().startsWith(fieldDefaultPrefix)) {
                String field = pv.getName().substring(fieldDefaultPrefix.length());
                if (propertyAccessor.isWritableProperty(field) && !values.contains(field)) {
                    values.add(field, pv.getValue());
                }
                values.remove(pv);
            }
        }
    }
}
Also used : PropertyValue(cn.taketoday.beans.PropertyValue) ConfigurablePropertyAccessor(cn.taketoday.beans.ConfigurablePropertyAccessor)

Example 28 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-framework by TAKETODAY.

the class WebDataBinder method checkFieldMarkers.

/**
 * Check the given property values for field markers,
 * i.e. for fields that start with the field marker prefix.
 * <p>The existence of a field marker indicates that the specified
 * field existed in the form. If the property values do not contain
 * a corresponding field value, the field will be considered as empty
 * and will be reset appropriately.
 *
 * @param values the property values to be bound (can be modified)
 * @see #getFieldMarkerPrefix
 * @see #getEmptyValue(String, Class)
 */
protected void checkFieldMarkers(PropertyValues values) {
    String fieldMarkerPrefix = getFieldMarkerPrefix();
    if (fieldMarkerPrefix != null) {
        ConfigurablePropertyAccessor propertyAccessor = getPropertyAccessor();
        for (PropertyValue pv : values.toArray()) {
            if (pv.getName().startsWith(fieldMarkerPrefix)) {
                String field = pv.getName().substring(fieldMarkerPrefix.length());
                if (propertyAccessor.isWritableProperty(field) && !values.contains(field)) {
                    Class<?> fieldType = propertyAccessor.getPropertyType(field);
                    values.add(field, getEmptyValue(field, fieldType));
                }
                values.remove(pv);
            }
        }
    }
}
Also used : PropertyValue(cn.taketoday.beans.PropertyValue) ConfigurablePropertyAccessor(cn.taketoday.beans.ConfigurablePropertyAccessor)

Example 29 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-framework by TAKETODAY.

the class AbstractDataBinderParameterResolver method resolveName.

@Nullable
@Override
protected Object resolveName(String name, ResolvableMethodParameter resolvable, RequestContext context) throws Exception {
    final int parameterNameLength = name.length();
    // prepare property values
    final Map<String, String[]> parameters = context.getParameters();
    final DefaultMultiValueMap<String, PropertyValue> propertyValues = new DefaultMultiValueMap<>();
    for (final Map.Entry<String, String[]> entry : parameters.entrySet()) {
        final String[] paramValues = entry.getValue();
        if (ObjectUtils.isNotEmpty(paramValues)) {
            final String requestParameterName = entry.getKey();
            // users[key].userName=TODAY&users[key].age=20
            if (requestParameterName.startsWith(name) && requestParameterName.charAt(parameterNameLength) == '[') {
                // userList[0].name  '.' 's index
                final int separatorIndex = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(requestParameterName);
                final String property = requestParameterName.substring(separatorIndex + 1);
                final int closeKey = requestParameterName.indexOf(']');
                final String key = requestParameterName.substring(parameterNameLength + 1, closeKey);
                final PropertyValue propertyValue = new PropertyValue(property, paramValues[0]);
                propertyValues.add(key, propertyValue);
            }
        }
    }
    return doBind(propertyValues, resolvable);
}
Also used : DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) PropertyValue(cn.taketoday.beans.PropertyValue) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) Nullable(cn.taketoday.lang.Nullable)

Example 30 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-framework by TAKETODAY.

the class MetadataAttachmentTests method propertyMetadata.

@Test
public void propertyMetadata() throws Exception {
    BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("testBean3");
    PropertyValue pv = beanDefinition.getPropertyValues().get("name");
    assertThat(pv.getAttribute("surname")).isEqualTo("Harrop");
}
Also used : PropertyValue(cn.taketoday.beans.PropertyValue) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

PropertyValue (cn.taketoday.beans.PropertyValue)46 PropertyValues (cn.taketoday.beans.PropertyValues)28 Test (org.junit.jupiter.api.Test)22 ConfigurablePropertyAccessor (cn.taketoday.beans.ConfigurablePropertyAccessor)6 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)6 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)6 MultiValueMap (cn.taketoday.core.MultiValueMap)6 List (java.util.List)6 Map (java.util.Map)6 BeanMetadata (cn.taketoday.beans.BeanMetadata)4 BeanWrapper (cn.taketoday.beans.BeanWrapper)4 BeanWrapperImpl (cn.taketoday.beans.BeanWrapperImpl)4 BooleanTestBean (cn.taketoday.beans.BooleanTestBean)4 NumberTestBean (cn.taketoday.beans.NumberTestBean)4 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)4 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)4 Nullable (cn.taketoday.lang.Nullable)4 RequestContextDataBinder (cn.taketoday.web.bind.RequestContextDataBinder)4 ArrayList (java.util.ArrayList)4 AnnotationAwareAspectJAutoProxyCreator (cn.taketoday.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator)2