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

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

Introduction

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

Prototype

public static PropertyDescriptor[] getPropertyDescriptors(Object bean) 

Source Link

Document

Retrieve the property descriptors for the specified bean, introspecting and caching them the first time a particular bean class is encountered.

For more details see PropertyUtilsBean.

Usage

From source file:com.timesoft.kaitoo.ws.hibernate.AbstractPojo.java

/**
 * DOCUMENT ME!/*w  w w .j ava  2s  . co  m*/
 *
 * @return DOCUMENT ME!
 */
public String toString() {
    PropertyDescriptor[] pd = PropertyUtils.getPropertyDescriptors(this);
    StringBuffer buffer = new StringBuffer();

    if (((List) callStack.get()).contains(this)) {
        buffer.append("Cyclic Reference!!!");
    } else {
        ((List) callStack.get()).add(this);

        for (int index = 0; index < pd.length; ++index) {
            if ((null != PropertyUtils.getReadMethod(pd[index]))
                    && (pd[index].getPropertyType() != Class.class)) {
                if (buffer.length() > 0) {
                    buffer.append(", ");
                }

                String prop_name = pd[index].getName();
                buffer.append(prop_name).append("=");

                try {
                    buffer.append(PropertyUtils.getProperty(this, prop_name));
                } catch (Exception e) {
                    buffer.append(e.getMessage());
                }
            }
        }

        ((List) callStack.get()).remove(this);
    }

    buffer.insert(0, " { ").insert(0, getClass().getName()).append(" }");

    return buffer.toString();
}

From source file:de.iritgo.simplelife.bean.BeanTools.java

/**
 * Return true if the given property exists in the given class
 * /* w  w w.jav a2  s  .  c o  m*/
 * @param klass The class
 * @param propertyName The property name
 * @return True if the property exists
 */
static public boolean hasProperty(Class klass, String propertyName) {
    for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(klass)) {
        if (pd.getName().equals(propertyName)) {
            return true;
        }
    }

    return false;
}

From source file:com.afeng.common.utils.reflection.MyBeanUtils.java

private static void convert(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {

    // Validate existence of the specified beans
    if (dest == null) {
        throw new IllegalArgumentException("No destination bean specified");
    }/*from   w  w w  .  j  ava 2s  .  c o  m*/
    if (orig == null) {
        throw new IllegalArgumentException("No origin bean specified");
    }

    // Copy the properties, converting as necessary
    if (orig instanceof DynaBean) {
        DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass().getDynaProperties();
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((DynaBean) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else if (orig instanceof Map) {
        Iterator names = ((Map) orig).keySet().iterator();
        while (names.hasNext()) {
            String name = (String) names.next();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((Map) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else
    /* if (orig is a standard JavaBean) */
    {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            //              String type = origDescriptors[i].getPropertyType().toString();
            if ("class".equals(name)) {
                continue; // No point in trying to set an object's class
            }
            if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) {
                try {
                    Object value = PropertyUtils.getSimpleProperty(orig, name);
                    copyProperty(dest, name, value);
                } catch (IllegalArgumentException ie) {
                    ; // Should not happen
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    }

}

From source file:com.eryansky.common.utils.reflection.MyBeanUtils.java

private static void convert(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {

    // Validate existence of the specified beans
    if (dest == null) {
        throw new IllegalArgumentException("No destination bean specified");
    }/*  w ww .  j av a 2  s  .c om*/
    if (orig == null) {
        throw new IllegalArgumentException("No origin bean specified");
    }

    // Copy the properties, converting as necessary
    if (orig instanceof DynaBean) {
        DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass().getDynaProperties();
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((DynaBean) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else if (orig instanceof Map) {
        Iterator names = ((Map) orig).keySet().iterator();
        while (names.hasNext()) {
            String name = (String) names.next();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((Map) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else
    /* if (orig is a standard JavaBean) */
    {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            //              String type = origDescriptors[i].getPropertyType().toString();
            if ("class".equals(name)) {
                continue; // No point in trying to set an object's class
            }
            if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) {
                try {
                    Object value = PropertyUtils.getSimpleProperty(orig, name);
                    copyProperty(dest, name, value);
                } catch (java.lang.IllegalArgumentException ie) {
                    ; // Should not happen
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    }

}

From source file:name.martingeisse.wicket.autoform.describe.DefaultAutoformBeanDescriberHelper.java

@Override
protected PropertyDescriptor[] getPropertyDescriptors(final Class<? extends Serializable> beanDescriptor) {
    return PropertyUtils.getPropertyDescriptors(beanDescriptor);
}

From source file:com.cyclopsgroup.tornado.utils.BeanDynaTagSupport.java

/**
 * Overwrite or implement method getAttributeType()
 *
 * @see com.cyclopsgroup.waterview.utils.DynaTagSupport#getAttributeType(java.lang.String)
 *///from   w w  w .  j  av a  2  s  . c  om
public Class getAttributeType(String attributeName) throws JellyTagException {
    if (beanClass == null) {
        return super.getAttributeType(attributeName);
    }
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
    for (int i = 0; i < descriptors.length; i++) {
        PropertyDescriptor descriptor = descriptors[i];
        if (descriptor.getName().equals(attributeName)) {
            return descriptor.getPropertyType();
        }
    }
    return super.getAttributeType(attributeName);
}

From source file:com.eryansky.common.utils.reflection.BeanUtils.java

private static void convert(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {

    // Validate existence of the specified beans
    if (dest == null) {
        throw new IllegalArgumentException("No destination bean specified");
    }/*from ww  w. j a v a  2s  .com*/
    if (orig == null) {
        throw new IllegalArgumentException("No origin bean specified");
    }

    // Copy the properties, converting as necessary
    if (orig instanceof DynaBean) {
        DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass().getDynaProperties();
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((DynaBean) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else if (orig instanceof Map) {
        Iterator names = ((Map) orig).keySet().iterator();
        while (names.hasNext()) {
            String name = (String) names.next();
            if (PropertyUtils.isWriteable(dest, name)) {
                Object value = ((Map) orig).get(name);
                try {
                    copyProperty(dest, name, value);
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    } else
    /* if (orig is a standard JavaBean) */ {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            //              String type = origDescriptors[i].getPropertyType().toString();
            if ("class".equals(name)) {
                continue; // No point in trying to set an object's class
            }
            if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) {
                try {
                    Object value = PropertyUtils.getSimpleProperty(orig, name);
                    copyProperty(dest, name, value);
                } catch (IllegalArgumentException ie) {
                    ; // Should not happen
                } catch (Exception e) {
                    ; // Should not happen
                }

            }
        }
    }

}

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>// w w  w . j av a 2  s.  co  m
 * @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:fr.opensagres.struts2.views.xdocreport.LazyCommonsBeanUtilsPopulateContext.java

@Override
protected PropertyDescriptor[] getPropertyDescriptors(Object pojo) throws Exception {
    return PropertyUtils.getPropertyDescriptors(pojo.getClass());
}

From source file:net.mojodna.searchable.SearchableBeanUtils.java

/**
 * Gets the name of the property that should be used to generate a search
 * excerpt./*from w  w  w . j a  v a2 s  . c  om*/
 * 
 * @param clazz Class to reflect on.
 * @return Name of the excerptable property; null if none present.
 */
public static String getExcerptPropertyName(final Class<? extends Result> clazz) {
    final PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(clazz);
    for (final PropertyDescriptor d : descriptors) {
        if (AnnotationUtils.isAnnotationPresent(d.getReadMethod(), Searchable.Excerptable.class))
            return d.getName();
    }

    return null;
}