Example usage for org.springframework.beans BeanWrapper setPropertyValue

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

Introduction

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

Prototype

void setPropertyValue(String propertyName, @Nullable Object value) throws BeansException;

Source Link

Document

Set the specified value as current property value.

Usage

From source file:com.htmlhifive.sync.resource.AbstractCrudSyncResource.java

/**
 * ???ID???????.//  www  . j ava2 s  .com
 *
 * @param common ?
 * @return 
 */
private T createDeletedItem(ResourceItemCommonData common) {
    Class<T> itemType = getItemType();

    BeanWrapper deletedWrapper = PropertyAccessorFactory
            .forBeanPropertyAccess(BeanUtils.instantiateClass(itemType));
    deletedWrapper.setPropertyValue(getIdFieldName(), common.getTargetItemId());

    return itemType.cast(deletedWrapper.getWrappedInstance());
}

From source file:net.sourceforge.vulcan.web.struts.actions.ManagePluginAction.java

public final ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    final PluginConfigForm configForm = (PluginConfigForm) form;
    final String target = configForm.getTarget();

    final String[] split = target.split("\\[");
    final String arrayProperty = split[0];
    final int index = Integer.parseInt(split[1].substring(0, split[1].length() - 1));

    final BeanWrapper bw = new BeanWrapperImpl(form);
    final Class<?> type = PropertyUtils.getPropertyType(form, arrayProperty).getComponentType();

    Object[] array = (Object[]) bw.getPropertyValue(arrayProperty);

    Object[] tmp = (Object[]) Array.newInstance(type, array.length - 1);
    System.arraycopy(array, 0, tmp, 0, index);
    System.arraycopy(array, index + 1, tmp, index, array.length - index - 1);

    bw.setPropertyValue(arrayProperty, tmp);

    configForm.putBreadCrumbsInRequest(request);

    return mapping.findForward("configure");
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperGenericsTests.java

@Test
public void testGenericSet() {
    GenericBean<?> gb = new GenericBean<Object>();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(gb);
    Set<String> input = new HashSet<String>();
    input.add("4");
    input.add("5");
    bw.setPropertyValue("integerSet", input);
    assertTrue(gb.getIntegerSet().contains(new Integer(4)));
    assertTrue(gb.getIntegerSet().contains(new Integer(5)));
}

From source file:uk.co.blackpepper.support.retrofit.jsoup.spring.AbstractBeanHtmlConverter.java

private void setAsText(Object bean, String propertyName, String text) throws ConversionException {
    BeanWrapper beanWrapper = wrap(bean);
    TypeDescriptor typeDescriptor = beanWrapper.getPropertyTypeDescriptor(propertyName);

    Object value;//from w w w.  ja  va 2 s. c o  m
    try {
        value = conversionService.convert(text, TypeDescriptor.valueOf(String.class), typeDescriptor);
    } catch (TypeMismatchException exception) {
        String message = String.format("Error converting from '%s' to type '%s' for property '%s'", text,
                typeDescriptor, propertyName);
        throw new ConversionException(message, exception);
    }

    try {
        beanWrapper.setPropertyValue(propertyName, value);
    } catch (BeansException exception) {
        String message = String.format("Error setting bean property '%s' to: %s", propertyName, value);
        throw new ConversionException(message, exception);
    }
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperGenericsTests.java

@Test
public void testGenericSetWithConversionFailure() {
    GenericBean<?> gb = new GenericBean<Object>();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(gb);
    Set<TestBean> input = new HashSet<TestBean>();
    input.add(new TestBean());
    try {//from   ww  w.  j a va  2 s  .  c  o m
        bw.setPropertyValue("integerSet", input);
        fail("Should have thrown TypeMismatchException");
    } catch (TypeMismatchException ex) {
        assertTrue(ex.getMessage().indexOf("java.lang.Integer") != -1);
    }
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperGenericsTests.java

@Test
public void testGenericMap() {
    GenericBean<?> gb = new GenericBean<Object>();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(gb);
    Map<String, String> input = new HashMap<String, String>();
    input.put("4", "5");
    input.put("6", "7");
    bw.setPropertyValue("shortMap", input);
    assertEquals(new Integer(5), gb.getShortMap().get(new Short("4")));
    assertEquals(new Integer(7), gb.getShortMap().get(new Short("6")));
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperGenericsTests.java

@Test
public void testGenericMapWithKeyType() {
    GenericBean<?> gb = new GenericBean<Object>();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(gb);
    Map<String, String> input = new HashMap<String, String>();
    input.put("4", "5");
    input.put("6", "7");
    bw.setPropertyValue("longMap", input);
    assertEquals("5", gb.getLongMap().get(new Long("4")));
    assertEquals("7", gb.getLongMap().get(new Long("6")));
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperGenericsTests.java

@Test
public void testGenericList() throws MalformedURLException {
    GenericBean<?> gb = new GenericBean<Object>();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(gb);
    List<String> input = new ArrayList<String>();
    input.add("http://localhost:8080");
    input.add("http://localhost:9090");
    bw.setPropertyValue("resourceList", input);
    assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0));
    assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1));
}

From source file:org.grails.datastore.mapping.core.AbstractSession.java

/**
 * This default implementation of updateAll is unlikely to be optimal as it iterates and updates each object one by one.
 *
 * Subclasses should override to optimize for the batch operation capability of the underlying datastore
 *
 * @param criteria The criteria//from   w  ww  . ja  va 2s .  com
 * @param properties The properties
 */
public int updateAll(QueryableCriteria criteria, Map<String, Object> properties) {
    List list = criteria.list();
    for (Object o : list) {
        BeanWrapper bean = new BeanWrapperImpl(o);
        for (String property : properties.keySet()) {
            bean.setPropertyValue(property, properties.get(property));
        }
    }
    persist(list);
    return list.size();
}

From source file:org.springmodules.cache.provider.ReflectionCacheModelEditor.java

/**
 * @throws IllegalStateException/*from   w  w w  .j a  va  2s  .co  m*/
 *           if the class of the cache model to create has not been set.
 * @see SemicolonSeparatedPropertiesParser#parseProperties(String)
 * @see PropertyEditor#setAsText(String)
 * @see org.springframework.beans.PropertyAccessor#setPropertyValue(String,
 *      Object)
 */
public final void setAsText(String text) {
    if (cacheModelClass == null) {
        throw new IllegalStateException("cacheModelClass should not be null");
    }

    Properties properties = SemicolonSeparatedPropertiesParser.parseProperties(text);

    BeanWrapper beanWrapper = new BeanWrapperImpl(cacheModelClass);

    if (properties != null) {
        for (Iterator i = properties.keySet().iterator(); i.hasNext();) {
            String propertyName = (String) i.next();
            String textProperty = properties.getProperty(propertyName);

            Object propertyValue = null;

            PropertyEditor propertyEditor = getPropertyEditor(propertyName);
            if (propertyEditor != null) {
                propertyEditor.setAsText(textProperty);
                propertyValue = propertyEditor.getValue();
            } else {
                propertyValue = textProperty;
            }
            beanWrapper.setPropertyValue(propertyName, propertyValue);
        }
    }

    setValue(beanWrapper.getWrappedInstance());
}