use of cn.taketoday.beans.BeanMetadata in project today-infrastructure by TAKETODAY.
the class BeanMetadataTests method beanMetadata.
@Test
public void beanMetadata() {
final BeanMetadata beanMetadata = BeanMetadata.from(BeanMappingTestBean.class);
final Object instance = beanMetadata.newInstance();
assertThat(instance).isInstanceOf(BeanMappingTestBean.class);
BeanMappingTestBean bean = (BeanMappingTestBean) instance;
bean.setAnotherNested(bean);
assertThat(bean.getDoubleProperty()).isEqualTo(321.0);
beanMetadata.setProperty(instance, "doubleProperty", 123.45);
assertThat(bean.getDoubleProperty()).isEqualTo(123.45);
beanMetadata.obtainBeanProperty("doubleProperty").setValue(instance, 321.0);
assertThat(bean.getDoubleProperty()).isEqualTo(321.0);
assertThatThrownBy(() -> {
beanMetadata.obtainBeanProperty("1243");
}).hasMessageStartingWith(String.format("Invalid property '1243' of bean class [%s]: Property not found", BeanMappingTestBean.class.getName()));
}
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 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);
}
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 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);
}
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 converter type-converter to convert bean-properties
*/
@SuppressWarnings("unchecked")
public static <T> T copy(Object source, Class<T> destination, @Nullable TypeConverter converter, @Nullable String... ignoreProperties) {
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, ignoreProperties);
return (T) destinationInstance;
}
use of cn.taketoday.beans.BeanMetadata in project today-infrastructure by TAKETODAY.
the class DataBinderParameterResolver method resolveArgument.
/**
* @return Pojo parameter
*/
@Override
public Object resolveArgument(final RequestContext context, final ResolvableMethodParameter resolvable) throws Throwable {
final Class<?> parameterClass = resolvable.getParameterType();
BeanMetadata beanMetadata = BeanMetadata.from(parameterClass);
Object target = beanMetadata.newInstance();
RequestContextDataBinder dataBinder = new RequestContextDataBinder(target, resolvable.getName());
dataBinder.setConversionService(conversionService);
dataBinder.bind(context);
// #30 Support annotation-supported in the form of DataBinder
resolveAnnotatedProperty(context, resolvable, dataBinder);
// todo dataBinder.validate();
return target;
}
Aggregations