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:org.ng200.openolympus.util.Beans.java

public static <T> void copy(T from, T to) {
    final BeanWrapper src = new BeanWrapperImpl(from);
    final BeanWrapper trg = new BeanWrapperImpl(to);

    for (final String propertyName : Stream.of(src.getPropertyDescriptors()).map(pd -> pd.getName())
            .collect(Collectors.toList())) {
        if (!trg.isWritableProperty(propertyName)) {
            continue;
        }//w  w  w. j  av  a2 s  . c o m

        trg.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
    }
}

From source file:org.dspace.servicemanager.DSpaceServiceManager.java

/**
 * Adds configuration settings into services if possible.
 * Skips any that are invalid.//from   w w  w . ja v  a 2  s  .  c  om
 * 
 * @param serviceName the name of the service
 * @param service the service object
 * @param serviceNameConfigs all known service configuration settings (from the DS config service impl)
 */
public static void configureService(String serviceName, Object service,
        Map<String, Map<String, ServiceConfig>> serviceNameConfigs) {
    // stuff the config settings into the bean if there are any
    if (serviceNameConfigs.containsKey(serviceName)) {
        BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(service);

        Map<String, ServiceConfig> configs = serviceNameConfigs.get(serviceName);
        for (ServiceConfig config : configs.values()) {
            try {
                beanWrapper.setPropertyValue(config.getParamName(), config.getValue());

                log.info("Set param (" + config.getParamName() + ") on service bean (" + serviceName + ") to: "
                        + config.getValue());
            } catch (RuntimeException e) {
                log.error("Unable to set param (" + config.getParamName() + ") on service bean (" + serviceName
                        + "): " + e.getMessage(), e);
            }
        }
    }
}

From source file:net.solarnetwork.util.ClassUtils.java

/**
 * Copy non-null bean properties from one object to another.
 * //from   w  ww.  j a v a 2 s  .  c o m
 * @param src the object to copy values from
 * @param dest the object to copy values to
 * @param ignore a set of property names to ignore (optional)
 * @param emptyStringToNull if <em>true</em> then String values that 
 * are empty or contain only whitespace will be treated as if they
 * where <em>null</em>
 */
public static void copyBeanProperties(Object src, Object dest, Set<String> ignore, boolean emptyStringToNull) {
    if (ignore == null) {
        ignore = DEFAULT_BEAN_PROP_NAME_IGNORE;
    }
    BeanWrapper bean = PropertyAccessorFactory.forBeanPropertyAccess(src);
    BeanWrapper to = PropertyAccessorFactory.forBeanPropertyAccess(dest);
    PropertyDescriptor[] props = bean.getPropertyDescriptors();
    for (PropertyDescriptor prop : props) {
        if (prop.getReadMethod() == null) {
            continue;
        }
        String propName = prop.getName();
        if (ignore != null && ignore.contains(propName)) {
            continue;
        }
        Object propValue = bean.getPropertyValue(propName);
        if (propValue == null || (emptyStringToNull && (propValue instanceof String)
                && !StringUtils.hasText((String) propValue))) {
            continue;
        }
        if (to.isWritableProperty(propName)) {
            to.setPropertyValue(propName, propValue);
        }
    }
}

From source file:ar.com.zauber.commons.conversion.setters.FieldSetSetterStrategy.java

/** @see FieldSetterStrategy#setProperty(BeanWrapper, String, Object) */
public final void setProperty(final BeanWrapper bean, final String targetName, final Object value) {
    bean.setPropertyValue(targetName, value);
}

From source file:com.aw.support.beans.BeanUtils.java

public static Object copyProperties(Object source, Object target, String[] propertyNamesToIgnore,
        boolean ignoreProxy, boolean ignoreCollections) {
    List<String> propertyNamesToIgnoreList = propertyNamesToIgnore == null ? Collections.EMPTY_LIST
            : Arrays.asList(propertyNamesToIgnore);
    BeanWrapper sourceWrap = new BeanWrapperImpl(source);
    BeanWrapper targetWrap = new BeanWrapperImpl(target);
    for (PropertyDescriptor propDescriptor : sourceWrap.getPropertyDescriptors()) {
        String propName = propDescriptor.getName();
        //chequear que no esta en la lista a ignorar
        if (propertyNamesToIgnoreList.contains(propName))
            continue;
        //chequear que se puede leer
        if (!sourceWrap.isReadableProperty(propName))
            continue;
        //chequear que se puede escribir
        if (!targetWrap.isWritableProperty(propName))
            continue;

        Object sourceValue = sourceWrap.getPropertyValue(propName);

        //chequear que objeto no es un proxy
        if (ignoreProxy && sourceValue != null && Proxy.isProxyClass(sourceValue.getClass()))
            continue;

        //chequear que objeto no una collection
        if (ignoreCollections && sourceValue instanceof Collection)
            continue;

        targetWrap.setPropertyValue(propName, sourceValue);
    }/*from ww w  .j a  v  a 2s .  c  o  m*/
    return target;
}

From source file:ru.ilb.common.jpa.tools.DescriptorUtils.java

public void fixInverseLinks(Object entity) {
    final BeanWrapper src = new BeanWrapperImpl(entity);
    java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
    ClassDescriptor cd = entityManager.unwrap(Session.class).getDescriptor(entity);
    if (cd == null) {
        return;/*from  w  ww  .  j  a v a  2 s  .  c  o m*/
    }
    for (java.beans.PropertyDescriptor pd : pds) {
        DatabaseMapping dm = cd.getMappingForAttributeName(pd.getName());
        if (dm != null) {
            if (dm instanceof OneToManyMapping) {
                OneToManyMapping dmOtM = (OneToManyMapping) dm;
                if (dmOtM.getMappedBy() != null) {
                    List srcValue = (List) src.getPropertyValue(pd.getName());
                    if (srcValue != null) {
                        for (Object v : srcValue) {
                            final BeanWrapper srcv = new BeanWrapperImpl(v);
                            srcv.setPropertyValue(dmOtM.getMappedBy(), entity);
                            fixInverseLinks(v);
                        }
                    }

                }
            }
            if (dm instanceof OneToOneMapping) {
                OneToOneMapping dmOtO = (OneToOneMapping) dm;
                if (dmOtO.getMappedBy() != null) {
                    Object srcValue = src.getPropertyValue(pd.getName());
                    if (srcValue != null) {
                        final BeanWrapper srcv = new BeanWrapperImpl(srcValue);
                        srcv.setPropertyValue(dmOtO.getMappedBy(), entity);
                        fixInverseLinks(srcValue);
                    }

                }
            }
        }

    }
}

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

@Test
public void testCustomEnumWithNull() {
    GenericBean<?> gb = new GenericBean<Object>();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(gb);
    bw.setPropertyValue("customEnum", null);
    assertEquals(null, gb.getCustomEnum());
}

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

@Test
public void testCustomEnumWithEmptyString() {
    GenericBean<?> gb = new GenericBean<Object>();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(gb);
    bw.setPropertyValue("customEnum", "");
    assertEquals(null, gb.getCustomEnum());
}

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

@Test
public void testCustomEnumArrayWithSingleValue() {
    GenericBean<?> gb = new GenericBean<Object>();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(gb);
    bw.setPropertyValue("customEnumArray", "VALUE_1");
    assertEquals(1, gb.getCustomEnumArray().length);
    assertEquals(CustomEnum.VALUE_1, gb.getCustomEnumArray()[0]);
}

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

@Test
public void testCustomEnumSetWithSingleValue() {
    GenericBean<?> gb = new GenericBean<Object>();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(gb);
    bw.setPropertyValue("customEnumSet", "VALUE_1");
    assertEquals(1, gb.getCustomEnumSet().size());
    assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1));
}