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:it.sample.parser.util.CommonsUtil.java

/**
 * Metodo di utilita' per rendere null un'istanza di una classe che e' istanziata ma che ha tutti i campi null
 * // www. j  a  v  a  2 s  .  c om
 * @param instance
 */
public static <T> void sanitizeObject(T instance) {
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(instance);
    if (descriptors != null) {
        for (PropertyDescriptor descriptor : descriptors) {
            try {
                String propertyName = descriptor.getName();
                if (descriptor.getReadMethod() != null) {
                    Object val = PropertyUtils.getProperty(instance, propertyName);
                    if (val != null && !BeanUtils.isSimpleProperty(val.getClass())
                            && descriptor.getWriteMethod() != null && !isFilled(val)
                            && !val.getClass().getName().startsWith("java.util")) {
                        PropertyUtils.setProperty(instance, propertyName, null);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:net.openkoncept.vroom.VroomUtilities.java

public static JSONObject convertObjectToJSONObject(Object object) throws JSONException {
    JSONObject jo = new JSONObject();
    if (object instanceof Character || object instanceof String || object instanceof Boolean
            || object instanceof Short || object instanceof Integer || object instanceof Long
            || object instanceof Float || object instanceof Double || object instanceof java.util.Date
            || object instanceof java.math.BigDecimal || object instanceof java.math.BigInteger) {
        jo.put("value", getValueForJSONObject(object));
    } else if (object instanceof java.util.Map) {
        Map m = (Map) object;
        Iterator iter = m.keySet().iterator();
        while (iter.hasNext()) {
            String key = (String) iter.next();
            jo.put(key, getValueForJSONObject(m.get(key)));
        }//from   ww w.j a v  a 2s  . c o m
    } else if (object instanceof Collection) {
        Collection c = (Collection) object;
        Iterator iter = c.iterator();
        JSONArray ja = new JSONArray();
        while (iter.hasNext()) {
            ja.put(getValueForJSONObject(iter.next()));
        }
        jo.put("array", ja);
    } else if (object != null && object.getClass().isArray()) {
        Object[] oa = (Object[]) object;
        JSONArray ja = new JSONArray();
        for (int i = 0; i < oa.length; i++) {
            ja.put(getValueForJSONObject(oa[i]));
        }
        jo.put("array", ja);
    } else if (object instanceof ResourceBundle) {
        ResourceBundle rb = (ResourceBundle) object;
        Enumeration e = rb.getKeys();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            Object value = getValueForJSONObject(rb.getObject(key));
            jo.put(key, value);
        }
    } else if (object != null) {
        Class clazz = object.getClass();
        PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(object);
        for (int i = 0; i < pds.length; i++) {
            PropertyDescriptor pd = pds[i];
            String name = pd.getName();
            if (!"class".equals(name) && PropertyUtils.isReadable(object, pd.getName())) {
                try {
                    Object value = PropertyUtils.getProperty(object, name);
                    jo.put(name, getValueForJSONObject(value));
                } catch (Exception e) {
                    // Useless...
                }
            }
        }
    } else {
        jo.put("value", "");
    }
    return jo;
}

From source file:io.milton.property.BeanPropertySource.java

@Override
public List<QName> getAllPropertyNames(Resource r) {
    BeanPropertyResource anno = getAnnotation(r);
    if (anno == null) {
        return null;
    }//from   w  w w  .j  a  v a  2s  .  c  om
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(r);
    List<QName> list = new ArrayList<QName>();
    for (PropertyDescriptor pd : pds) {
        if (pd.getReadMethod() != null) {
            list.add(new QName(anno.value(), pd.getName()));
        }
    }
    return list;
}

From source file:net.mojodna.searchable.util.SearchableUtils.java

/**
 * @param clazz/*from  w  w  w  . j  a v  a  2 s . c om*/
 * @param propertyName
 * @return Return type.
 */
public static final Class<?> getReturnType(final Class<?> clazz, final String propertyName) {
    final String key = clazz.getName() + "#" + propertyName;

    if (returnTypeCache.containsKey(key)) {
        return returnTypeCache.get(key);
    }

    if (returnTypeMissCache.containsKey(key)) {
        return null;
    }

    for (final PropertyDescriptor d : PropertyUtils.getPropertyDescriptors(clazz)) {
        if (d.getName().equals(propertyName)) {
            final Class<?> returnType = d.getReadMethod().getReturnType();
            returnTypeCache.put(key, returnType);
            return returnType;
        }
    }

    returnTypeMissCache.put(key, key);
    return null;
}

From source file:com.expressui.core.view.field.SelectField.java

private void assertValidPropertyId() {
    boolean propertyIdFound = false;
    Class<T> beanType = typedForm.getType();
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(beanType);
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        if (propertyDescriptor.getName().equals(getPropertyId())) {
            propertyIdFound = true;//from w  w  w  .j a v  a 2s. c o  m
        }
    }

    Assert.PROGRAMMING.isTrue(propertyIdFound,
            "property not found: " + beanType.getName() + "." + getPropertyId());
}

From source file:io.github.moosbusch.lumpi.gui.form.spi.AbstractDynamicForm.java

protected PropertyDescriptor[] getPropertyDescriptors(Class<?> beanClass) {
    return PropertyUtils.getPropertyDescriptors(beanClass);
}

From source file:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java

@Override
public long searchCount(T condition) {
    StringBuilder qString = new StringBuilder(
            "select count(model) from " + entityClass.getSimpleName() + " model");
    StringBuilder qWhere = new StringBuilder(" where ");
    StringBuilder qCondition = new StringBuilder();
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(entityClass);
    for (int i = 0, count = propertyDescriptors.length; i < count; i++) {
        PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
        String name = propertyDescriptor.getName();
        Class<?> type = propertyDescriptor.getPropertyType();
        String value = null;/*from  w w w  .j a  va 2 s.  c om*/
        try {
            value = BeanUtils.getProperty(condition, name);
        } catch (Exception e) {
            // ?
            continue;
        }
        if (value == null || name.equals("class")) {
            continue;
        }
        if ("java.lang.String".equals(type.getName())) {
            qCondition.append("model.");
            qCondition.append(name);
            qCondition.append(" like ");
            qCondition.append("'%");
            qCondition.append(value);
            qCondition.append("%'");
        } else {
            qCondition.append("model.");
            qCondition.append(name);
            qCondition.append("=");
            qCondition.append(value);
        }
        qCondition.append(" and ");
    }
    if (qCondition.length() != 0) {
        qString.append(qWhere).append(qCondition);
        if (qCondition.toString().endsWith(" and ")) {
            qString.delete(qString.length() - " and ".length(), qString.length());
        }
    }
    Query query = em.createQuery(qString.toString());
    return (Long) query.getSingleResult();
}

From source file:com.stephenduncanjr.easymock.EasyMockPropertyUtils.java

/**
 * Gets the properties and values from the object as a map, ignoring the
 * properties specified.//from   w w w  .ja  va2 s. c o m
 * 
 * @param bean
 * @param ignoredProperties
 * @return map of properties names to values.
 */
private static Map<String, Object> retrieveAndFilterProperties(final Object bean,
        final List<String> ignoredProperties) {
    final Map<String, Object> map = new HashMap<String, Object>();

    try {
        for (final PropertyDescriptor p : PropertyUtils.getPropertyDescriptors(bean)) {
            if (!ignoredProperties.contains(p.getName()) && !"class".equals(p.getName())) {
                final Method readMethod = p.getReadMethod();

                if (readMethod != null) {
                    map.put(p.getName(), readMethod.invoke(bean));
                }
            }
        }
    } catch (final IllegalAccessException e) {
        throw new IllegalArgumentException(e);
    } catch (final InvocationTargetException e) {
        throw new IllegalArgumentException(e);
    }

    return map;
}

From source file:gov.nih.nci.caarray.application.permissions.PermissionsManagementServiceBean.java

private User convertToLikeProperties(User u)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    final User newUser = new User();
    if (u != null) {
        PropertyUtils.copyProperties(newUser, u);
        for (final PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(newUser)) {
            convertToLikeProperty(u, newUser, pd);
        }/* w w w .j  a v  a2  s . c  o  m*/
    }
    return newUser;
}

From source file:de.micromata.genome.jpa.metainf.MetaInfoUtils.java

/**
 * To entity meta data./*from ww  w  .  j a v a2  s. c o m*/
 *
 * @param mt the mt
 * @return the entity metadata
 */
private static EntityMetadata toEntityMetaData(ManagedType<?> mt) {
    EntityMetadataBean ret = new EntityMetadataBean();
    ret.setJavaType(mt.getJavaType());
    if (mt instanceof EntityType) {
        EntityType ift = (EntityType) mt;
        ret.setDatabaseName(ift.getName());
    }
    // TODO RK delete propDesc
    List<PropertyDescriptor> propDescs = Arrays.asList(PropertyUtils.getPropertyDescriptors(ret.getJavaType()));
    Set<?> attr = mt.getAttributes();
    for (Object oa : attr) {

        Attribute<?, ?> at = (Attribute<?, ?>) oa;
        Optional<PropertyDescriptor> pdo = propDescs.stream().filter((el) -> el.getName().equals(at.getName()))
                .findFirst();
        ColumnMetadata colm = getColumnMetaData(ret, mt.getJavaType(), at, pdo);
        if (colm != null) {
            ret.getColumns().put(colm.getName(), colm);
        }
    }
    /// EntityType.getName() is not correct.
    Table[] tabs = mt.getJavaType().getAnnotationsByType(Table.class);
    if (tabs != null && tabs.length > 0) {
        for (Table tab : tabs) {
            if (StringUtils.isNotBlank(tab.name()) == true) {
                ret.setDatabaseName(tab.name());
                break;
            }
        }
    }
    return ret;
}