Search in sources :

Example 6 with BeanMetadata

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

the class BeanProperties method copy.

/**
 * Copy the property values of the given source bean into the given target bean.
 * <p>Note: The source and target classes do not have to match or even be derived
 * from each other, as long as the properties match. Any bean properties that the
 * source bean exposes but the target bean does not will silently be ignored.
 *
 * @param source source object
 * @param destination destination object
 * @param converter type-converter to convert bean-properties
 */
public static void copy(Object source, Object destination, @Nullable TypeConverter converter) {
    Assert.notNull(source, "source object must not be null");
    Assert.notNull(destination, "destination object must not be null");
    BeanMetadata destinationMetadata = BeanMetadata.from(destination);
    copy(source, destinationMetadata, destination, converter, null);
}
Also used : BeanMetadata(cn.taketoday.beans.BeanMetadata)

Example 7 with BeanMetadata

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

the class BeanProperties method copy.

/**
 * Copy the property values of the given source bean into the given target bean.
 * <p>Note: The source and target classes do not have to match or even be derived
 * from each other, as long as the properties match. Any bean properties that the
 * source bean exposes but the target bean does not will silently be ignored.
 *
 * @param source the source bean
 * @param converter type-converter to convert bean-properties
 * @param ignoreProperties array of property names to ignore
 */
public static void copy(Object source, Object destination, @Nullable TypeConverter converter, @Nullable String... ignoreProperties) {
    Assert.notNull(source, "source object must not be null");
    Assert.notNull(destination, "destination object must not be null");
    BeanMetadata destinationMetadata = BeanMetadata.from(destination);
    copy(source, destinationMetadata, destination, converter, ignoreProperties);
}
Also used : BeanMetadata(cn.taketoday.beans.BeanMetadata)

Example 8 with BeanMetadata

use of cn.taketoday.beans.BeanMetadata 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 9 with BeanMetadata

use of cn.taketoday.beans.BeanMetadata 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 10 with BeanMetadata

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

the class BeanPropertyRowMapper method initialize.

/**
 * Initialize the mapping meta-data for the given class.
 *
 * @param mappedClass the mapped class
 */
private void initialize(Class<T> mappedClass) {
    BeanMetadata metadata = BeanMetadata.from(mappedClass);
    initialize(mappedClass, metadata);
    this.metadata = metadata;
}
Also used : BeanMetadata(cn.taketoday.beans.BeanMetadata)

Aggregations

BeanMetadata (cn.taketoday.beans.BeanMetadata)21 RequestContextDataBinder (cn.taketoday.web.bind.RequestContextDataBinder)7 Map (java.util.Map)6 PropertyValue (cn.taketoday.beans.PropertyValue)4 PropertyValues (cn.taketoday.beans.PropertyValues)4 MultiValueMap (cn.taketoday.core.MultiValueMap)4 List (java.util.List)4 BeanProperty (cn.taketoday.beans.BeanProperty)2 SimpleTypeConverter (cn.taketoday.beans.SimpleTypeConverter)2 MethodParameter (cn.taketoday.core.MethodParameter)2 ResolvableType (cn.taketoday.core.ResolvableType)2 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)2 Test (org.junit.jupiter.api.Test)2