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:fm.pattern.minimal.Reflection.java

/**
 * Sets the specified <em>property</em> value on the given <em>instance</em>.
 * //from w w  w  .jav  a  2s  .c  o m
 * @param instance The object instance.
 * @param property The property to set.
 * @param value The property value.
 * 
 * @return The updated instance if the property was set correctly, null otherwise.
 */
public static <T> T set(T instance, String property, Object value) {
    try {
        PropertyUtils.setProperty(instance, property, value);
        return instance;
    } catch (Exception e) {
        return null;
    }
}

From source file:net.sf.json.TestJSONObjectStaticBuilders_PrimitiveBean.java

protected Object getSource() {
    PrimitiveBean bean = new PrimitiveBean();
    String[] props = getProperties();
    try {// w  ww. j a  v  a  2  s  .  co m
        for (int i = 0; i < props.length; i++) {
            PropertyUtils.setProperty(bean, props[i], PropertyConstants.getPropertyValue(props[i]));
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return bean;
}

From source file:com.esofthead.mycollab.iexporter.CSVObjectEntityConverter.java

@Override
public E convert(Class<E> cls, CSVItemMapperDef unit) {
    try {/*from   ww w.ja  v  a 2s  .com*/
        E bean = cls.newInstance();
        String[] csvLine = unit.getCsvLine();

        PropertyUtils.setProperty(bean, "saccountid", AppContext.getAccountId());
        for (ImportFieldDef importFieldDef : unit.getFieldsDef()) {
            try {
                String csvFieldItem = csvLine[importFieldDef.getColumnIndex()];
                if (importFieldDef.getFieldFormatter() != null) {
                    PropertyUtils.setProperty(bean, importFieldDef.getFieldname(),
                            importFieldDef.getFieldFormatter().format(csvFieldItem));
                } else
                    PropertyUtils.setProperty(bean, importFieldDef.getFieldname(), csvFieldItem);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return bean;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:net.jcreate.e3.table.html.tag.DecoratorTag.java

public void setProperty(String pProperty, Object pValue) {
    try {//from www  .j a v  a  2 s .co m
        PropertyUtils.setProperty(cellDecorator, pProperty, pValue);
    } catch (Exception ex) {
        final String msg = "" + this.cellDecorator.getClass().getName() + ":"
                + pProperty + "?" + ":" + pValue;
        logger.error(msg, ex);
        throw new DecorateException(msg, ex);
    }

}

From source file:com.mycollab.iexporter.CSVObjectEntityConverter.java

@Override
public E convert(Class<E> cls, CSVItemMapperDef unit) {
    try {//w ww.  j a v  a 2  s .c  om
        E bean = cls.newInstance();
        String[] csvLine = unit.getCsvLine();

        PropertyUtils.setProperty(bean, "saccountid", MyCollabUI.getAccountId());
        for (ImportFieldDef importFieldDef : unit.getFieldsDef()) {
            try {
                String csvFieldItem = csvLine[importFieldDef.getColumnIndex()];
                if (importFieldDef.getFieldFormatter() != null) {
                    PropertyUtils.setProperty(bean, importFieldDef.getFieldname(),
                            importFieldDef.getFieldFormatter().format(csvFieldItem));
                } else
                    PropertyUtils.setProperty(bean, importFieldDef.getFieldname(), csvFieldItem);

            } catch (Exception e) {
                LOG.error("Error", e);
            }
        }
        return bean;
    } catch (Exception e) {
        LOG.error("Error", e);
        return null;
    }
}

From source file:com.mycollab.aspect.InjectTimeLoggingAspect.java

@Before("execution(public * com.mycollab..service..*.saveWithSession(..)) && args(bean, username)")
public void injectDateForSaveMethod(JoinPoint joinPoint, Object bean, String username) {
    try {//from w  w w . j a  v  a2s.c om
        LOG.debug("Set createtime and lastupdatedtime if enable");
        PropertyUtils.setProperty(bean, "createdtime", new GregorianCalendar().getTime());
        PropertyUtils.setProperty(bean, "lastupdatedtime", new GregorianCalendar().getTime());
    } catch (Exception e) {
    }
}

From source file:com.khubla.cbean.serializer.impl.json.JSONUUIDFieldSerializer.java

@Override
public void deserialize(Object o, Field field, String value) throws SerializerException {
    try {//  w w  w .ja v a  2s .  c  o m
        final UUID uuid = UUID.fromString(value);
        PropertyUtils.setProperty(o, field.getName(), uuid);
    } catch (final Exception e) {
        throw new SerializerException(e);
    }
}

From source file:com.rolex.explore.beanutils.service.BeanUtilsService.java

public void exploreBeanUtil() {

    SampleBean bean = new SampleBean();

    String property1 = "name";

    String property2 = "currentAddress.city";

    String property3 = "previousAddresses[0].city";

    String property4 = "previousAddresses[3].city";

    String property5 = "vehicleLicenseModel(R60)";

    Place place1 = new Place("Sentosa", "Singapore");
    Place place2 = new Place("Colombo", "Sri Lanka");
    List<Place> places = new ArrayList<Place>();
    places.add(place1);/*from   w ww. j  a  v  a  2s  .c  o  m*/
    places.add(place2);

    String property6 = "yearlyPlacesVisited(2000)";

    String property7 = "placesVisited";

    String property8 = "placesVisited[0]";

    TourismAward award = new TourismAward("World Award Committee", "USA");

    String property9 = "yearlyPlacesVisited(2000)[0].tourismAwards[0]";

    try {
        PropertyUtils.setProperty(bean, property1, "Rolex Rolex");
        String value1 = (String) PropertyUtils.getProperty(bean, property1);
        System.out.println("###Reverse1:   " + value1);

        PropertyUtils.setProperty(bean, property2, "Hoffman Estates");
        String value2 = (String) PropertyUtils.getProperty(bean, property2);
        System.out.println("###Reverse2:   " + value2);

        PropertyUtils.setProperty(bean, property3, "Schaumburg");
        String value3 = (String) PropertyUtils.getProperty(bean, property3);
        System.out.println("###Reverse3:   " + value3);

        PropertyUtils.setProperty(bean, property4, "Des Plaines");
        String value4 = (String) PropertyUtils.getProperty(bean, property4);
        System.out.println("###Reverse4:   " + value4);

        Address[] arrayValue1 = (Address[]) PropertyUtils.getProperty(bean, "previousAddresses");
        System.out.println("###ReverseArray:   " + Arrays.toString(arrayValue1));

        PropertyUtils.setProperty(bean, property5, "Sonata");
        String value5 = (String) PropertyUtils.getProperty(bean, property5);
        System.out.println("###Reverse5:   " + value5);

        PropertyUtils.setProperty(bean, property6, places);
        List<Place> value6 = (List<Place>) PropertyUtils.getProperty(bean, property6);
        System.out.println("###Reverse6:   " + value6.get(0));

        PropertyUtils.setProperty(bean, property7, places);
        List<Place> value7 = (List<Place>) PropertyUtils.getProperty(bean, property7);
        System.out.println("###Reverse7:   " + value7.get(0));

        PropertyUtils.setProperty(bean, property8, place2);
        Place value8 = (Place) PropertyUtils.getProperty(bean, property8);
        System.out.println("###Reverse8:   " + value8);

        PropertyUtils.setProperty(bean, property9, award);
        TourismAward value9 = (TourismAward) PropertyUtils.getProperty(bean, property9);
        System.out.println("###Reverse8:   " + value8);

        System.out.println("Bean: " + bean);

        SampleBean copyBean = new SampleBean();
        BeanUtils.copyProperties(copyBean, bean);
        System.out.println("Bean: " + bean);
        System.out.println("Copy Bean: " + copyBean);

    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.esofthead.mycollab.common.interceptor.aspect.InjectTimeLoggingAspect.java

@Before("execution(public * com.esofthead.mycollab..service..*.saveWithSession(..)) && args(bean, username)")
public void injectDateForSaveMethod(JoinPoint joinPoint, Object bean, String username) {

    try {//from  w w  w .  ja  v  a 2s  .c  om
        LOG.debug("Set createtime and lastupdatedtime if enable");
        PropertyUtils.setProperty(bean, "createdtime", new GregorianCalendar().getTime());
        PropertyUtils.setProperty(bean, "lastupdatedtime", new GregorianCalendar().getTime());
    } catch (Exception e) {
    }
}

From source file:ca.sqlpower.architect.TestUtils.java

/**
 * Sets all the settable properties on the given target object
 * which are not in the given ignore set.
 * /*from  www.  j  ava  2s .com*/
 * @param target The object to change the properties of
 * @param propertiesToIgnore The properties of target not to modify or read
 * @return A Map describing the new values of all the non-ignored, readable 
 * properties in target.
 */
public static Map<String, Object> setAllInterestingProperties(Object target, Set<String> propertiesToIgnore)
        throws Exception {

    PropertyDescriptor props[] = PropertyUtils.getPropertyDescriptors(target);
    for (int i = 0; i < props.length; i++) {
        Object oldVal = null;
        if (PropertyUtils.isReadable(target, props[i].getName()) && props[i].getReadMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {
            oldVal = PropertyUtils.getProperty(target, props[i].getName());
        }
        if (PropertyUtils.isWriteable(target, props[i].getName()) && props[i].getWriteMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {

            NewValueMaker valueMaker = new ArchitectValueMaker(new SPObjectRoot());
            Object newVal = valueMaker.makeNewValue(props[i].getPropertyType(), oldVal, props[i].getName());

            System.out.println("Changing property \"" + props[i].getName() + "\" to \"" + newVal + "\"");
            PropertyUtils.setProperty(target, props[i].getName(), newVal);

        }
    }

    // read them all back at the end in case there were dependencies between properties
    return ca.sqlpower.testutil.TestUtils.getAllInterestingProperties(target, propertiesToIgnore);
}