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.everm.propertydescriptor.App.java

/**
 * @param args//from w  w w .  j a  v  a 2 s.  c  o  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:com.eviware.soapui.monitor.PropertySupport.java

public static void applySystemProperties(Object target, String scope, ModelItem modelItem) {
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(target);
    DefaultPropertyExpansionContext context = new DefaultPropertyExpansionContext(modelItem);
    Properties properties = System.getProperties();

    for (PropertyDescriptor descriptor : descriptors) {
        String name = descriptor.getName();
        String key = scope + "." + name;
        if (PropertyUtils.isWriteable(target, name) && properties.containsKey(key)) {
            try {
                String value = context.expand(String.valueOf(properties.get(key)));
                BeanUtils.setProperty(target, name, value);
                SoapUI.log.info("Set property [" + name + "] to [" + value + "] in scope [" + scope + "]");
            } catch (Throwable e) {
                SoapUI.logError(e);//w  w  w .  ja v a 2  s. co m
            }
        }
    }
}

From source file:io.fabric8.core.jmx.BeanUtils.java

public static List<String> getFields(Class clazz) {
    List<String> answer = new ArrayList<String>();

    try {//from   w  w  w  .j  a  va2s  .  c om
        for (PropertyDescriptor desc : PropertyUtils.getPropertyDescriptors(clazz)) {
            if (desc.getReadMethod() != null) {
                answer.add(desc.getName());
            }
        }
    } catch (Exception e) {
        throw new FabricException("Failed to get property descriptors for " + clazz.toString(), e);
    }

    // few tweaks to maintain compatibility with existing views for now...
    if (clazz.getSimpleName().equals("Container")) {
        answer.add("parentId");
        answer.add("versionId");
        answer.add("profileIds");
        answer.add("childrenIds");
        answer.remove("fabricService");
    } else if (clazz.getSimpleName().equals("Profile")) {
        answer.add("id");
        answer.add("parentIds");
        answer.add("childIds");
        answer.add("containerCount");
        answer.add("containers");
        answer.add("fileConfigurations");
    } else if (clazz.getSimpleName().equals("Version")) {
        answer.add("id");
        answer.add("defaultVersion");
    }

    return answer;
}

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:35:29   <br>
 * Object      <br>// w  ww. j  a v a  2s. c o m
 * @param dest
 * @param orig
 * @return the dest bean
 * @throws
 */
@SuppressWarnings("rawtypes")
public static Object copyProperties(Object dest, Object orig) {
    if (dest == null || orig == null) {
        return dest;
    }
    PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest);
    try {
        for (int i = 0; i < destDesc.length; i++) {
            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)) {
                    try {
                        Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
                        PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
                    } catch (Exception ex) {
                    }
                }
            }
        }
        return dest;
    } catch (Exception ex) {
        throw new AOSARuntimeException(ex);
    }
}

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

/**
 * Copy all attributes from the given bean to the given map
 * /*from  w  w  w .j  ava  2s  . com*/
 * @param object The bean
 * @param map The map
 * @param overwrite If true existing attributes in the map will be
 *            overwritten
 */
static public void copyBean2Map(Object object, Map<String, Object> map, boolean overwrite) {
    for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(object)) {
        String name = pd.getName();

        if (!overwrite && map.containsKey(name)) {
            continue;
        }

        try {
            map.put(name, PropertyUtils.getProperty(object, name));
        } catch (Exception ignore) {
        }
    }
}

From source file:me.yyam.beanutils.BeanUtilEx.java

/**
 * IntrospectorPropertyDescriptor Bean --> Map
 * @param obj// w  w  w  .  j  a  va 2 s . c  o  m
 * @return
 */
public static Map transBean2Map(Object obj) throws Exception {
    if (obj == null) {
        return null;
    }
    Map<String, Object> map = new HashMap<>();
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj);
    for (PropertyDescriptor property : propertyDescriptors) {
        String key = property.getName();
        if (!key.equals("class")) {
            try {
                Object value = PropertyUtils.getProperty(obj, key);
                if (value == null) {
                    map.put(key, value);
                } else {
                    if (value instanceof List) {
                        List list = new ArrayList();
                        for (Object v : (List) value) {
                            list.add(transBean2Map(v));
                        }
                        map.put(key, list);
                    } else {
                        if (value instanceof Enum) {
                            value = value.toString();
                        }
                        if (isPrimitive(value)) {
                            map.put(key, value);
                        } else {
                            Map cmap = transBean2Map(value);
                            map.put(key, cmap);
                        }
                    }
                }
            } catch (NoSuchMethodException e) {
                System.out.println(e.toString());
            }
        }
    }
    return map;
}

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.
 * //  ww w .j  a v a2s .  c  o m
 * @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);
}

From source file:fr.isima.reflexbench.architecture.ApacheReflect.java

@Override
public void listFields(Object obj) {
    PropertyUtils.getPropertyDescriptors(obj);
}

From source file:com.reachlocal.grails.plugins.cassandra.utils.DataMapper.java

public static Map<String, Object> dataProperties(GroovyObject data, List<String> transients,
        Map<String, Class> hasMany, String expandoMapName, Collection mappedProperties) throws IOException {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    Class clazz = data.getClass();
    if (expandoMapName != null) {
        transients.add(expandoMapName);//from   ww w .  ja  v a2 s.  co  m
    }

    // Unneeded since we now get the class from the method signatures
    // Might be needed again if we ever support subclasses
    //map.put(CLASS_NAME_KEY, clazz.getName());

    for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(data)) {
        String name = pd.getName();
        if (!transients.contains(name) && !GLOBAL_TRANSIENTS.contains(name) && hasMany.get(name) == null) {
            Object prop = data.getProperty(name);
            if (prop == null) {
                if (mappedProperties.contains(name)) {
                    map.put(name + "Id", null);
                } else {
                    map.put(name, null);
                }
            } else {
                //if (OrmHelper.isMappedObject(prop)) {   // TODO new is mapped
                if (mappedProperties.contains(name)) {
                    GroovyObject g = (GroovyObject) prop;
                    String idName = name + "Id";
                    map.put(idName, g.getProperty("id"));
                } else {
                    Object value = dataProperty(prop);
                    map.put(name, value);
                }
            }
        }
    }

    if (expandoMapName != null) {
        Map<String, Object> expandoMap = (Map<String, Object>) data.getProperty(expandoMapName);
        if (expandoMap != null) {
            for (Map.Entry<String, Object> entry : expandoMap.entrySet()) {
                map.put(entry.getKey(), entry.getValue());
            }
        }
    }

    return map;
}

From source file:de.erdesignerng.dialect.ModelItemProperties.java

public void copyTo(T aObject) {

    ModelProperties theProperties = aObject.getProperties();

    try {//ww  w  .  ja v a2  s.c  o m
        for (PropertyDescriptor theDescriptor : PropertyUtils.getPropertyDescriptors(this)) {
            if (theDescriptor.getReadMethod() != null && theDescriptor.getWriteMethod() != null) {
                Object theValue = PropertyUtils.getProperty(this, theDescriptor.getName());
                if (theValue != null) {
                    theProperties.setProperty(theDescriptor.getName(), theValue.toString());
                } else {
                    theProperties.setProperty(theDescriptor.getName(), null);
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}