use of cn.taketoday.beans.BeanMetadata in project today-infrastructure 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;
}
use of cn.taketoday.beans.BeanMetadata in project today-infrastructure 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.BeanMetadata in project today-infrastructure 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.BeanMetadata in project today-infrastructure by TAKETODAY.
the class BeanProperties method copy.
/**
* Ignore read-only properties
*/
@SuppressWarnings("unchecked")
private static void copy(Object source, BeanMetadata destination, Object destinationInstance, @Nullable TypeConverter converter, @Nullable String[] ignoreProperties) {
if (converter == null) {
converter = new SimpleTypeConverter();
}
if (ObjectUtils.isNotEmpty(ignoreProperties)) {
Set<String> ignorePropertiesSet = Set.of(ignoreProperties);
if (source instanceof Map) {
for (Map.Entry<String, Object> entry : ((Map<String, Object>) source).entrySet()) {
String propertyName = entry.getKey();
if (!ignorePropertiesSet.contains(propertyName)) {
BeanProperty beanProperty = destination.getBeanProperty(propertyName);
if (beanProperty != null && beanProperty.isWriteable()) {
beanProperty.setValue(destinationInstance, entry.getValue(), converter);
}
}
}
} else {
BeanMetadata sourceMetadata = BeanMetadata.from(source);
for (BeanProperty property : sourceMetadata) {
if (property.isReadable()) {
String propertyName = property.getName();
if (!ignorePropertiesSet.contains(propertyName)) {
BeanProperty beanProperty = destination.getBeanProperty(propertyName);
if (beanProperty != null && beanProperty.isWriteable()) {
beanProperty.setValue(destinationInstance, property.getValue(source), converter);
}
}
}
}
}
} else {
if (source instanceof Map) {
for (Map.Entry<String, Object> entry : ((Map<String, Object>) source).entrySet()) {
String propertyName = entry.getKey();
BeanProperty beanProperty = destination.getBeanProperty(propertyName);
if (beanProperty != null && beanProperty.isWriteable()) {
beanProperty.setValue(destinationInstance, entry.getValue(), converter);
}
}
} else {
BeanMetadata sourceMetadata = BeanMetadata.from(source);
for (BeanProperty property : sourceMetadata) {
if (property.isReadable()) {
String propertyName = property.getName();
BeanProperty beanProperty = destination.getBeanProperty(propertyName);
if (beanProperty != null && beanProperty.isWriteable()) {
beanProperty.setValue(destinationInstance, property.getValue(source), converter);
}
}
}
}
}
}
use of cn.taketoday.beans.BeanMetadata in project today-infrastructure 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 class
* @param converter type-converter to convert bean-properties
* @return returns a destination type object
*/
@SuppressWarnings("unchecked")
public static <T> T copy(Object source, Class<T> destination, @Nullable TypeConverter converter) {
Assert.notNull(source, "source object must not be null");
Assert.notNull(destination, "destination class must not be null");
BeanMetadata destinationMetadata = BeanMetadata.from(destination);
// destination
Object destinationInstance = destinationMetadata.newInstance();
copy(source, destinationMetadata, destinationInstance, converter, null);
return (T) destinationInstance;
}
Aggregations