Search in sources :

Example 11 with BeanMetadata

use of cn.taketoday.beans.BeanMetadata in project today-framework 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()));
}
Also used : BeanMetadata(cn.taketoday.beans.BeanMetadata) Test(org.junit.jupiter.api.Test)

Example 12 with BeanMetadata

use of cn.taketoday.beans.BeanMetadata in project today-framework 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;
}
Also used : BeanMetadata(cn.taketoday.beans.BeanMetadata) RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder)

Example 13 with BeanMetadata

use of cn.taketoday.beans.BeanMetadata in project today-framework 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);
                    }
                }
            }
        }
    }
}
Also used : SimpleTypeConverter(cn.taketoday.beans.SimpleTypeConverter) BeanMetadata(cn.taketoday.beans.BeanMetadata) Map(java.util.Map) BeanProperty(cn.taketoday.beans.BeanProperty)

Example 14 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 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;
}
Also used : BeanMetadata(cn.taketoday.beans.BeanMetadata)

Example 15 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 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;
}
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