Example usage for org.springframework.beans BeanWrapper convertIfNecessary

List of usage examples for org.springframework.beans BeanWrapper convertIfNecessary

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper convertIfNecessary.

Prototype

@Nullable
<T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType) throws TypeMismatchException;

Source Link

Document

Convert the value to the required type (if necessary from a String).

Usage

From source file:org.jdbcluster.dao.Dao.java

/**
 * creates new instance of a Dao using given Dao class
 * @param daoClass class of Object to create
 * @return created dao instance/*from  w  w w  .j  a v a2 s. c  o m*/
 */
public static Object newInstance(Class<?> daoClass) {
    Object dao = JDBClusterUtil.createClassObject(daoClass);
    //get property of DAO and initial value from jdbcluster.dao.conf.xml
    HashMap<String, String> hm;
    synchronized (classToPropsMap) {
        hm = classToPropsMap.get(daoClass);
        if (hm == null)
            hm = putCacheMap(daoClass);
    }

    BeanWrapper beanWrapper = new BeanWrapperImpl();
    beanWrapper.setWrappedInstance(dao);

    for (String prop : hm.keySet()) {
        String value = hm.get(prop);
        //get property of DAO
        Class<?> propClass = beanWrapper.getPropertyType(prop);
        //convert to Type if necessary
        Object o = beanWrapper.convertIfNecessary(value, propClass);
        //set the value of the predefined property read from jdbcluster.dao.conf.xml
        beanWrapper.setPropertyValue(new PropertyValue(prop, o));
    }
    return dao;
}

From source file:com.alibaba.citrus.service.form.impl.GroupImpl.java

/**
 * fields//w w  w.j a  v  a  2s .c om
 * <p>
 * <code>isValidated()</code><code>true</code>group
 * </p>
 */
public void mapTo(Object object) {
    if (isValidated() || object == null) {
        return;
    }

    if (log.isDebugEnabled()) {
        log.debug("Mapping properties to fields: group=\"{}\", object={}", getName(),
                ObjectUtil.identityToString(object));
    }

    BeanWrapper bean = new BeanWrapperImpl(object);
    getForm().getFormConfig().getPropertyEditorRegistrar().registerCustomEditors(bean);

    for (Field field : getFields()) {
        String propertyName = field.getFieldConfig().getPropertyName();

        if (bean.isReadableProperty(propertyName)) {
            Object propertyValue = bean.getPropertyValue(propertyName);
            Class<?> propertyType = bean.getPropertyType(propertyName);
            PropertyEditor editor = bean.findCustomEditor(propertyType, propertyName);

            if (editor == null) {
                editor = BeanUtils.findEditorByConvention(propertyType);
            }

            if (editor == null) {
                if (propertyType.isArray() || CollectionFactory.isApproximableCollectionType(propertyType)) {
                    field.setValues((String[]) bean.convertIfNecessary(propertyValue, String[].class));
                } else {
                    field.setValue(bean.convertIfNecessary(propertyValue, String.class));
                }
            } else {
                editor.setValue(propertyValue);
                field.setValue(editor.getAsText());
            }
        } else {
            log.debug("No readable property \"{}\" found in type {}", propertyName,
                    object.getClass().getName());
        }
    }
}

From source file:org.opennms.netmgt.alarmd.northbounder.snmptrap.SnmpTrapNorthbounderConfigDaoTest.java

/**
 * Test bean wrapper.//from  w  w  w . j  a v  a2s  .  c o m
 *
 * @throws Exception the exception
 */
@Test
public void testBeanWrapper() throws Exception {
    SnmpTrapSink sink = configDao.getConfig().getSnmpTrapSink("localTest2");
    final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(sink);
    Map<String, String> params = new HashMap<String, String>();
    params.put("ipAddress", "192.168.0.1");
    Assert.assertEquals("127.0.0.2", sink.getIpAddress());
    boolean modified = false;
    for (final String key : params.keySet()) {
        if (wrapper.isWritableProperty(key)) {
            final String stringValue = params.get(key);
            final Object value = wrapper.convertIfNecessary(stringValue,
                    (Class<?>) wrapper.getPropertyType(key));
            wrapper.setPropertyValue(key, value);
            modified = true;
        }
    }
    Assert.assertTrue(modified);
    Assert.assertEquals("192.168.0.1", sink.getIpAddress());
    configDao.save();
    configDao.reload();
    Assert.assertEquals("192.168.0.1", configDao.getConfig().getSnmpTrapSink("localTest2").getIpAddress());
}