Example usage for org.apache.commons.beanutils PropertyUtils setProperty

List of usage examples for org.apache.commons.beanutils PropertyUtils setProperty

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils setProperty.

Prototype

public static void setProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:com.doctusoft.common.core.bean.internal.AttributeImpl.java

@Override
public void setValue(final E instance, final T value) {
    checkNotNull(instance);/*from www. j  ava2s.co  m*/

    try {
        PropertyUtils.setProperty(instance, name, value);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:br.com.renatoccosta.regexrenamer.element.base.ElementFactory.java

public static void setParameter(Element element, Field field, Object value) throws InvalidParameterException {
    try {/*from www  . j a v  a2s . c  om*/
        //for security reasons, only parameters can be set with this method
        if (field.getAnnotation(Parameter.class) != null) {
            if (value instanceof String) {
                value = utilsBean.getConvertUtils().convert((String) value, field.getType());
            }

            PropertyUtils.setProperty(element, field.getName(), value);
        } else {
            throw createInvalidParameterException(field.getName(), null);
        }
    } catch (Exception ex) {
        throw createInvalidParameterException(field.getName(), ex);
    }
}

From source file:jp.co.opentone.bsol.framework.core.util.PropertyGetUtil.java

public static void setProperty(Object bean, String name, Object value) {
    try {//from w w w . j  a v  a  2s  .com
        PropertyUtils.setProperty(bean, name, value);
    } catch (IllegalAccessException e) {
        throw new ReflectionRuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new ReflectionRuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new ReflectionRuntimeException(e);
    }
}

From source file:hermes.browser.dialog.BeanPropertyPanel.java

protected void onSetProperty(String propertyName, Object propertyValue) {
    try {/*from  w  w w . j  av a2s. c  o m*/
        if (setProperty) {
            PropertyUtils.setProperty(bean, propertyName, propertyValue);
        }

        super.onSetProperty(propertyName, propertyValue);
    } catch (Throwable e) {
        HermesBrowser.getBrowser().showErrorDialog("Cannot set property " + propertyName, e);
    }
}

From source file:com.subakva.formicid.converters.FileSetConverter.java

public FileSet convert(Class type, Object value) {
    if (value == null || value.equals(Undefined.instance)) {
        throw new ConversionException("No value specified");
    }//  w  w w .jav a 2 s . c  o m
    if (value instanceof FileSet) {
        return (FileSet) value;
    } else if (value instanceof Scriptable) {
        FileSet fileSet = (FileSet) container.getProject().createDataType("fileset");
        Scriptable options = (Scriptable) value;
        Object[] ids = options.getIds();
        for (int i = 0; i < ids.length; i++) {
            String id = (String) ids[i];
            Object val = options.get(id, options);
            try {
                PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(fileSet, id);
                Class<?> propertyType = descriptor.getPropertyType();
                Converter converter = container.getConverter(propertyType);
                Object converted = converter.convert(propertyType, val);
                PropertyUtils.setProperty(fileSet, id, converted);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
        return fileSet;
    } else if (value instanceof String) {
        FileSet fileSet = (FileSet) container.getProject().createDataType("fileset");
        fileSet.setDir(new File("."));
        FilenameSelector selector = new FilenameSelector();
        selector.setName((String) value);
        fileSet.add(selector);
        return fileSet;
    } else {
        throw new ConversionException("" + value);
    }
}

From source file:com.aosa.main.utils.tools.AOSACopyUtil.java

/**
 * Description<code>Copy properties of orig to dest Exception the Entity and Collection Type</code>      <br>
 * By mutou at 2011-8-30 ?11:36:11   <br>
 * Object      <br>//from  w  ww. ja v a  2  s  . c om
 * @param dest
 * @param orig
 * @param ignores
 * @return the dest bean
 * @throws
 */
@SuppressWarnings("rawtypes")
public static Object copyProperties(Object dest, Object orig, String[] ignores) {
    if (dest == null || orig == null) {
        return dest;
    }

    PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest);
    try {
        for (int i = 0; i < destDesc.length; i++) {
            if (contains(ignores, destDesc[i].getName())) {
                continue;
            }
            Class destType = destDesc[i].getPropertyType();
            Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName());
            if (destType != null && destType.equals(origType) && !destType.equals(Class.class)) {
                if (!Collection.class.isAssignableFrom(origType)) {
                    Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
                    PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
                }
            }
        }
        return dest;
    } catch (Exception ex) {
        throw new AOSARuntimeException(ex);
    }
}

From source file:com.thinkmore.framework.orm.hibernate.HibernateWebUtils.java

/**
 * ?ID?,???./*from w  ww  . j  a  v a2s  . c o  m*/
 * 
 * ??????id,??????id?????.
 * ???id??,??id??.
 * ?ID, ??cascade-save-or-update.
 * 
 * @param srcObjects ??,.
 * @param checkedIds  ?,ID.
 * @param clazz  ?
 * @param idName ??
 */
public static <T, ID> void mergeByCheckedIds(final Collection<T> srcObjects, final Collection<ID> checkedIds,
        final Class<T> clazz, final String idName) {

    //?
    Assert.notNull(srcObjects, "scrObjects?");
    Assert.hasText(idName, "idName?");
    Assert.notNull(clazz, "clazz?");

    //?,???.
    if (checkedIds == null) {
        srcObjects.clear();
        return;
    }

    //????,id?ID?,.
    //?,???id,?id???id.
    Iterator<T> srcIterator = srcObjects.iterator();
    try {

        while (srcIterator.hasNext()) {
            T element = srcIterator.next();
            Object id;
            id = PropertyUtils.getProperty(element, idName);

            if (!checkedIds.contains(id)) {
                srcIterator.remove();
            } else {
                checkedIds.remove(id);
            }
        }

        //ID??id????,,id??.
        for (ID id : checkedIds) {
            T obj = clazz.newInstance();
            PropertyUtils.setProperty(obj, idName, id);
            srcObjects.add(obj);
        }
    } catch (Exception e) {
        throw ReflectionUtil.convertReflectionExceptionToUnchecked(e);
    }
}

From source file:com.mindquarry.persistence.jcr.model.DefaultProperty.java

public void setContent(Object bean, Object value) {
    try {/* w  w  w  .  j  a va2s .  c  om*/
        PropertyUtils.setProperty(bean, getName(), value);
    } catch (IllegalAccessException e) {
        throw new JcrPersistenceInternalException(e);
    } catch (InvocationTargetException e) {
        throw new JcrPersistenceInternalException(e);
    } catch (NoSuchMethodException e) {
        throw new JcrPersistenceInternalException(e);
    }
}

From source file:iddb.core.cache.CacheFactory.java

@SuppressWarnings({ "static-access", "rawtypes" })
private Cache getCacheImpl(String namespace) throws UnavailableCacheException {
    Object obj = null;// w  ww.  j a v  a 2s.co m
    try {
        Class impl = this.getClass().forName(CACHE_IMPL);
        obj = impl.newInstance();
    } catch (ClassNotFoundException e) {
        log.warn(e.getMessage());
        throw new UnavailableCacheException();
    } catch (SecurityException e) {
        log.error(e.getMessage());
        throw new UnavailableCacheException();
    } catch (IllegalArgumentException e) {
        log.error(e.getMessage());
        throw new UnavailableCacheException();
    } catch (InstantiationException e) {
        log.error(e.getMessage());
        throw new UnavailableCacheException();
    } catch (IllegalAccessException e) {
        log.error(e.getMessage());
        throw new UnavailableCacheException();
    }
    try {
        PropertyUtils.setProperty(obj, "namespace", namespace);
    } catch (IllegalAccessException e) {
        log.error(e.getMessage());
    } catch (InvocationTargetException e) {
        log.error(e.getMessage());
    } catch (NoSuchMethodException e) {
        log.warn(e.getMessage());
    }
    return (Cache) obj;
}

From source file:jp.ac.tokushima_u.is.ll.common.orm.hibernate.HibernateWebUtils.java

/**
 * ?ID?,???.//  www .ja v  a2 s.c om
 * 
 * ??????id,??????id?????.
 * ???id??,??id??.
 * ?ID, ??cascade-save-or-update.
 * 
 * @param srcObjects ??,.
 * @param checkedIds  ?,ID.
 * @param clazz  ?
 * @param idName ??
 */
public static <T, ID> void mergeByCheckedIds(final Collection<T> srcObjects, final Collection<ID> checkedIds,
        final Class<T> clazz, final String idName) {

    //?
    Assert.notNull(srcObjects, "scrObjects?");
    Assert.hasText(idName, "idName?");
    Assert.notNull(clazz, "clazz?");

    //?,???.
    if (checkedIds == null) {
        srcObjects.clear();
        return;
    }

    //????,id?ID?,.
    //?,???id,?id???ID.
    Iterator<T> srcIterator = srcObjects.iterator();
    try {

        while (srcIterator.hasNext()) {
            T element = srcIterator.next();
            Object id;
            id = PropertyUtils.getProperty(element, idName);

            if (!checkedIds.contains(id)) {
                srcIterator.remove();
            } else {
                checkedIds.remove(id);
            }
        }

        //ID??id????,,id??.
        for (ID id : checkedIds) {
            T obj = clazz.newInstance();
            PropertyUtils.setProperty(obj, idName, id);
            srcObjects.add(obj);
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }
}