Search in sources :

Example 21 with BeanWrapperImpl

use of cn.taketoday.beans.BeanWrapperImpl in project today-infrastructure by TAKETODAY.

the class JmxUtilsTests method getAttributeNameWithoutStrictCasing.

@Test
void getAttributeNameWithoutStrictCasing() {
    BeanProperty pd = new BeanWrapperImpl(AttributeTestBean.class).getBeanProperty("name");
    String attributeName = JmxUtils.getAttributeName(pd, false);
    assertThat(attributeName).as("Incorrect casing on attribute name").isEqualTo("name");
}
Also used : BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) BeanProperty(cn.taketoday.beans.BeanProperty) Test(org.junit.jupiter.api.Test)

Example 22 with BeanWrapperImpl

use of cn.taketoday.beans.BeanWrapperImpl in project today-infrastructure by TAKETODAY.

the class JmxUtilsTests method getAttributeNameWithStrictCasing.

@Test
void getAttributeNameWithStrictCasing() {
    BeanProperty pd = new BeanWrapperImpl(AttributeTestBean.class).getBeanProperty("name");
    String attributeName = JmxUtils.getAttributeName(pd, true);
    assertThat(attributeName).as("Incorrect casing on attribute name").isEqualTo("Name");
}
Also used : BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) BeanProperty(cn.taketoday.beans.BeanProperty) Test(org.junit.jupiter.api.Test)

Example 23 with BeanWrapperImpl

use of cn.taketoday.beans.BeanWrapperImpl in project today-infrastructure by TAKETODAY.

the class BeanProperties method populate.

/**
 * <p>Populate the JavaBeans properties of the specified bean, based on
 * the specified name/value pairs. This method uses Java reflection APIs
 * to identify corresponding "property setter" method names, and deals
 * with setter arguments of type <code>String</code>, <code>boolean</code>,
 * <code>int</code>, <code>long</code>, <code>float</code>, and
 * <code>double</code>.  In addition, array setters for these types (or the
 * corresponding primitive types) can also be identified.</p>
 *
 * <p>The particular setter method to be called for each property is
 * determined using the usual JavaBeans introspection mechanisms.  Thus,
 * you may identify custom setter methods using a BeanInfo class that is
 * associated with the class of the bean itself.  If no such BeanInfo
 * class is available, the standard method name conversion ("set" plus
 * the capitalized name of the property in question) is used.</p>
 *
 * @param bean JavaBean whose properties are being populated
 * @param properties Map keyed by property name, with the
 * corresponding (String or String[]) value(s) to be set
 * @throws NoSuchPropertyException If no such property
 * @throws InvalidPropertyException Invalid property value
 * @see BeanWrapperImpl
 */
public static void populate(Object bean, Map<String, Object> properties, boolean ignoreUnknown) {
    Assert.notNull(bean, "target bean must not be null");
    Assert.notNull(properties, "properties must not be null");
    BeanWrapperImpl beanWrapper = new BeanWrapperImpl(bean);
    beanWrapper.setAutoGrowNestedPaths(true);
    beanWrapper.setPropertyValues(properties, ignoreUnknown, true);
}
Also used : BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl)

Example 24 with BeanWrapperImpl

use of cn.taketoday.beans.BeanWrapperImpl in project today-infrastructure by TAKETODAY.

the class BeanPropertyRowMapper method mapRow.

/**
 * Extract the values for all columns in the current row.
 * <p>Utilizes public setters and result set meta-data.
 *
 * @see ResultSetMetaData
 */
@Override
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
    BeanWrapperImpl beanWrapper = this.beanWrapper;
    T mappedObject = constructMappedInstance(rs, beanWrapper);
    beanWrapper.setBeanInstance(mappedObject);
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    Set<String> populatedProperties = isCheckFullyPopulated() ? new HashSet<>() : null;
    HashMap<String, BeanProperty> mappedFields = this.mappedFields;
    for (int index = 1; index <= columnCount; index++) {
        String column = JdbcUtils.lookupColumnName(rsmd, index);
        String field = lowerCaseName(StringUtils.delete(column, " "));
        BeanProperty property = mappedFields != null ? mappedFields.get(field) : null;
        if (property != null) {
            try {
                // TODO using TypeHandler
                Object value = getColumnValue(rs, index, property);
                if (rowNumber == 0 && debugEnabled) {
                    log.debug("Mapping column '{}' to property '{}' of type '{}'", column, property.getName(), ClassUtils.getQualifiedName(property.getType()));
                }
                try {
                    beanWrapper.setPropertyValue(property.getName(), value);
                } catch (TypeMismatchException ex) {
                    if (value == null && this.primitivesDefaultedForNullValue) {
                        if (debugEnabled) {
                            log.debug("Intercepted TypeMismatchException for row {} and column '{}'" + " with null value when setting property '{}' of type '{}' on object: {}", rowNumber, column, property.getName(), ClassUtils.getQualifiedName(property.getType()), mappedObject, ex);
                        }
                    } else {
                        throw ex;
                    }
                }
                if (populatedProperties != null) {
                    populatedProperties.add(property.getName());
                }
            } catch (NotWritablePropertyException ex) {
                throw new DataRetrievalFailureException("Unable to map column '" + column + "' to property '" + property.getName() + "'", ex);
            }
        } else {
            // No BeanProperty found
            if (rowNumber == 0 && debugEnabled) {
                log.debug("No property found for column '{}' mapped to field '{}'", column, field);
            }
        }
    }
    if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
        throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of " + this.mappedClass + ": " + this.mappedProperties);
    }
    return mappedObject;
}
Also used : BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) BeanProperty(cn.taketoday.beans.BeanProperty) ResultSetMetaData(java.sql.ResultSetMetaData) NotWritablePropertyException(cn.taketoday.beans.NotWritablePropertyException) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) DataRetrievalFailureException(cn.taketoday.dao.DataRetrievalFailureException)

Example 25 with BeanWrapperImpl

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

the class JmxUtilsTests method getAttributeNameWithoutStrictCasing.

@Test
void getAttributeNameWithoutStrictCasing() {
    BeanProperty pd = new BeanWrapperImpl(AttributeTestBean.class).getBeanProperty("name");
    String attributeName = JmxUtils.getAttributeName(pd, false);
    assertThat(attributeName).as("Incorrect casing on attribute name").isEqualTo("name");
}
Also used : BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) BeanProperty(cn.taketoday.beans.BeanProperty) Test(org.junit.jupiter.api.Test)

Aggregations

BeanWrapperImpl (cn.taketoday.beans.BeanWrapperImpl)72 Test (org.junit.jupiter.api.Test)64 BeanWrapper (cn.taketoday.beans.BeanWrapper)60 NumberTestBean (cn.taketoday.beans.NumberTestBean)42 BooleanTestBean (cn.taketoday.beans.BooleanTestBean)40 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)34 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)34 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)34 PropertyEditorSupport (java.beans.PropertyEditorSupport)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28 PropertyValues (cn.taketoday.beans.PropertyValues)18 BeansException (cn.taketoday.beans.BeansException)8 BeanProperty (cn.taketoday.beans.BeanProperty)6 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)6 BigDecimal (java.math.BigDecimal)6 NumberFormat (java.text.NumberFormat)6 PropertyValue (cn.taketoday.beans.PropertyValue)4 BeanCreationException (cn.taketoday.beans.factory.BeanCreationException)4 BeanDefinitionStoreException (cn.taketoday.beans.factory.BeanDefinitionStoreException)4 InjectionPoint (cn.taketoday.beans.factory.InjectionPoint)4