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

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

Introduction

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

Prototype

public static Object getProperty(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return 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:Main.java

public static void main(String[] args) throws Exception {
    Recording recording = new Recording();
    recording.setTitle("Magical Mystery Tour");

    Class type = PropertyUtils.getPropertyType(recording, "title");
    System.out.println("type = " + type.getName());

    String value = (String) PropertyUtils.getProperty(recording, "title");
    System.out.println("value = " + value);
}

From source file:com.everm.propertydescriptor.App.java

/**
 * @param args/*ww  w  .  j  a  v a 2s  .co  m*/
 */
public static void main(String[] args) {
    try {
        Object bean = new App();
        PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean);
        Class beanClass = bean.getClass();
        for (int i = 0; i < pds.length; i++) {
            String key = pds[i].getName();
            Class type = pds[i].getPropertyType();
            if (pds[i].getReadMethod() != null) {
                Object value = PropertyUtils.getProperty(bean, key);
            } else {
                String warning = "Property '" + key + "' has no read method. SKIPPED";
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:BeanUtilsExampleV4.java

  public static void main(String args[]) throws Exception {
    BeanUtilsExampleV4 diff = new BeanUtilsExampleV4();
    Actor actor = diff.prepareData();//from w w w  .  j  a v  a2  s . c o m

    ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
    convertUtilsBean.deregister(String.class);
    convertUtilsBean.register(new MyStringConverter(), String.class);

    convertUtilsBean.deregister(Long.class);
    convertUtilsBean.register(new MyLongConverter(), Long.class);
    convertUtilsBean.register(new MyLongConverter(), Long.TYPE);

    BeanUtilsBean beanUtilsBean =
      new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());

    System.err.println("==== Values before calling describe ==== ");
    System.err.println("By PropertyUtils: " +
      PropertyUtils.getProperty(actor, "name"));
    System.err.println("By BeanUtils: " +
      beanUtilsBean.getProperty(actor, "name"));
    System.err.println(beanUtilsBean.getProperty(actor, "worth"));

    Map describedData = beanUtilsBean.describe(actor);

    // check the map
    System.err.println("==== Values in Map ==== ");
    System.err.println(describedData.get("name"));
    System.err.println(describedData.get("worth"));

    // create a new Actor Bean
    Actor newActor = new Actor();
    beanUtilsBean.populate(newActor, describedData);

    System.err.println("==== Values after calling populate ==== ");
    System.err.println(beanUtilsBean.getProperty(newActor, "name"));
    System.err.println(beanUtilsBean.getProperty(newActor, "worth"));

}

From source file:com.discursive.jccook.bean.BeanUtilExample.java

public static void main(String[] pArgs) {

    if (pArgs.length > 0 && pArgs[0] != null) {
        propName = pArgs[0];/* w w  w  . j a v  a 2 s.c  o m*/
    }
    System.out.println("Getting Property: " + propName);

    List books = createBooks();

    Iterator i = books.iterator();
    while (i.hasNext()) {
        Book book = (Book) i.next();
        Object propVal = null;
        try {
            propVal = PropertyUtils.getProperty(book, propName);
        } catch (Exception e) {
            System.out.println("Error getting property: " + propName + ", from book: " + book.getName()
                    + ", exception: " + e.getMessage());
        }
        System.out.println("Property Value: " + propVal);
    }
}

From source file:com.xdtech.core.orm.utils.CollectionUtils.java

/**
 * ????(Getter), ??List.//from  w w  w  .j a  va 2 s. c o  m
 * 
 * @param collection ???.
 * @param propertyName ??????.
 */
public static List extractElementPropertyToList(final Collection collection, final String propertyName) {
    List list = new ArrayList();

    try {
        for (Object obj : collection) {
            list.add(PropertyUtils.getProperty(obj, propertyName));
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }

    return list;
}

From source file:com.googlecode.commons.swing.util.BeanUtils.java

public static Object getProperty(Object bean, String property) {
    try {/*from   w w w . j ava 2s  .c  om*/
        return PropertyUtils.getProperty(bean, property);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return null;
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        return null;
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:mojo.view.util.BeanUtils.java

@SuppressWarnings("unchecked")
public static <T> T getProperty(Object obj, String prop) {
    try {//from   w w  w  . jav a2s.  c o  m
        return (T) PropertyUtils.getProperty(obj, prop);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.dotosoft.dot4command.utils.BeanUtils.java

public static <T extends Object> T getProperty(Object data, String expression, T defaultValue) {
    try {//from   w w w. j  a v  a  2  s . c om
        T returnValue = (T) PropertyUtils.getProperty(data, expression);
        if (returnValue != null) {
            return returnValue;
        }
    } catch (Exception ex) {
    }

    return defaultValue;
}

From source file:com.whollyframework.util.ConvertUtils.java

/**
 * ????(getter), ??List./* www . j a  v  a  2  s.  c  om*/
 * 
 * @param collection ???.
 * @param propertyName ??????.
 */
@SuppressWarnings("unchecked")
public static List convertElementPropertyToList(final Collection collection, final String propertyName) {
    List list = new ArrayList();

    try {
        for (Object obj : collection) {
            list.add(PropertyUtils.getProperty(obj, propertyName));
        }
    } catch (Exception e) {
        throw ReflectUtil.convertReflectionExceptionToUnchecked(e);
    }

    return list;
}

From source file:com.dosport.system.utils.ConvertUtils.java

/**
 * ????(getter), ??List.//  w  w  w.ja v a  2 s  . c  om
 * 
 * @param collection
 *            ???.
 * @param propertyName
 *            ??????.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List convertElementPropertyToList(final Collection collection, final String propertyName) {
    List list = new ArrayList();

    try {
        for (Object obj : collection) {
            list.add(PropertyUtils.getProperty(obj, propertyName));
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }

    return list;
}