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