Search in sources :

Example 16 with PropertyValue

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

the class DataBinderFieldAccessTests method nestedBindingWithDisabledAutoGrow.

@Test
void nestedBindingWithDisabledAutoGrow() throws Exception {
    FieldAccessBean rod = new FieldAccessBean();
    DataBinder binder = new DataBinder(rod, "person");
    binder.setAutoGrowNestedPaths(false);
    binder.initDirectFieldAccess();
    PropertyValues pvs = new PropertyValues();
    pvs.add(new PropertyValue("spouse.name", "Kerry"));
    assertThatExceptionOfType(NullValueInNestedPathException.class).isThrownBy(() -> binder.bind(pvs));
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) NullValueInNestedPathException(cn.taketoday.beans.NullValueInNestedPathException) PropertyValue(cn.taketoday.beans.PropertyValue) Test(org.junit.jupiter.api.Test)

Example 17 with PropertyValue

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

the class DataBinderCollectionParameterResolver method doBind.

/**
 * @return Collection object
 * @throws ParameterIndexExceededException {@code valueIndex} exceed {@link #maxValueIndex}
 * @throws ParameterFormatException {@code valueIndex} number format error
 * @see #createCollection(MultiValueMap, ResolvableMethodParameter)
 */
@Override
@SuppressWarnings({ "rawtypes" })
protected Object doBind(MultiValueMap<String, PropertyValue> propertyValues, ResolvableMethodParameter resolvable) {
    Collection<Object> collection = createCollection(propertyValues, resolvable);
    boolean isList = collection instanceof List;
    int maxValueIndex = getMaxValueIndex();
    Class<?> parameterClass = getComponentType(resolvable);
    BeanMetadata parameterMetadata = BeanMetadata.from(parameterClass);
    for (Map.Entry<String, List<PropertyValue>> entry : propertyValues.entrySet()) {
        Object rootObject = parameterMetadata.newInstance();
        RequestContextDataBinder dataBinder = new RequestContextDataBinder(rootObject, resolvable.getName());
        dataBinder.setAutoGrowCollectionLimit(maxValueIndex);
        List<PropertyValue> propertyValueList = entry.getValue();
        dataBinder.bind(new PropertyValues(propertyValueList));
        if (isList) {
            try {
                String key = entry.getKey();
                int valueIndex = Integer.parseInt(key);
                if (valueIndex > maxValueIndex) {
                    throw new ParameterIndexExceededException(resolvable.getParameter());
                }
                CollectionUtils.setValue((List) collection, valueIndex, rootObject);
            } catch (NumberFormatException e) {
                throw new ParameterFormatException(resolvable.getParameter(), e);
            }
        } else {
            collection.add(rootObject);
        }
    }
    return collection;
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) BeanMetadata(cn.taketoday.beans.BeanMetadata) RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) List(java.util.List) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap)

Example 18 with PropertyValue

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

the class DataBinderMapParameterResolver method doBind.

/**
 * Resolve {@link Map} parameter.
 */
@Override
protected Object doBind(MultiValueMap<String, PropertyValue> propertyValues, ResolvableMethodParameter resolvable) {
    MethodParameter parameter = resolvable.getParameter();
    Map<String, Object> map = CollectionUtils.createMap(parameter.getParameterType(), propertyValues.size());
    ResolvableType generic = resolvable.getResolvableType().asMap().getGeneric(1);
    Class<?> parameterClass = generic.resolve();
    BeanMetadata parameterMetadata = BeanMetadata.from(parameterClass);
    for (Map.Entry<String, List<PropertyValue>> entry : propertyValues.entrySet()) {
        Object rootObject = parameterMetadata.newInstance();
        RequestContextDataBinder dataBinder = new RequestContextDataBinder(rootObject, resolvable.getName());
        List<PropertyValue> propertyValueList = entry.getValue();
        dataBinder.bind(new PropertyValues(propertyValueList));
        map.put(entry.getKey(), rootObject);
    }
    return map;
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) BeanMetadata(cn.taketoday.beans.BeanMetadata) RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) List(java.util.List) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableType(cn.taketoday.core.ResolvableType) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap)

Example 19 with PropertyValue

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

the class DataBinderParameterResolver method resolveAnnotatedProperty.

/**
 * @since 4.0
 */
static void resolveAnnotatedProperty(RequestContext context, ResolvableMethodParameter parameter, RequestContextDataBinder dataBinder) throws Throwable {
    Object attribute = parameter.getAttribute(ANNOTATED_RESOLVERS_KEY);
    if (attribute instanceof List) {
        PropertyValues propertyValues = new PropertyValues();
        @SuppressWarnings("unchecked") List<AnnotatedPropertyResolver> resolvers = (List<AnnotatedPropertyResolver>) attribute;
        for (final AnnotatedPropertyResolver resolver : resolvers) {
            PropertyValue propertyValue = resolver.resolve(context);
            propertyValues.add(propertyValue);
        }
        if (!propertyValues.isEmpty()) {
            dataBinder.bind(propertyValues);
        }
    }
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) ArrayList(java.util.ArrayList) List(java.util.List)

Example 20 with PropertyValue

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

the class WebDataBinder method adaptEmptyArrayIndices.

/**
 * Check for property values with names that end on {@code "[]"}. This is
 * used by some clients for array syntax without an explicit index value.
 * If such values are found, drop the brackets to adapt to the expected way
 * of expressing the same for data binding purposes.
 *
 * @param values the property values to be bound (can be modified)
 */
protected void adaptEmptyArrayIndices(PropertyValues values) {
    ConfigurablePropertyAccessor propertyAccessor = getPropertyAccessor();
    for (PropertyValue pv : values) {
        String name = pv.getName();
        if (name.endsWith("[]")) {
            String field = name.substring(0, name.length() - 2);
            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)

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