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:org.openspotlight.persist.support.SimplePersistImpl.java

@Override
public <T> Iterable<T> findByProperties(final StorageNode parent, final Class<T> beanType,
        final String[] propertyNames, final Object[] propertyValues) {
    try {//from   ww w .  j a v  a 2  s. c om
        checkNotNull("currentPartition", currentPartition);
        checkNotNull("currentSession", currentSession);
        checkNotNull("beanType", beanType);
        checkNotNull("propertyNames", propertyNames);
        checkNotNull("propertyValues", propertyValues);
        checkCondition("namesAndValues:sameSize", propertyNames.length == propertyValues.length);

        final NodeCriteriaBuilder builder = currentSession.withPartition(currentPartition).createCriteria()
                .withNodeType(internalGetNodeName(beanType));
        final Map<String, PropertyDescriptor> allDescriptors = createMapWith(
                PropertyUtils.getPropertyDescriptors(beanType));

        for (int i = 0, size = propertyNames.length; i < size; i++) {

            final PropertyDescriptor descriptor = allDescriptors.get(propertyNames[i]);
            if (descriptor == null) {
                throw new SLRuntimeException("invalid property:" + propertyNames[i]);
            }
            builder.withProperty(propertyNames[i])
                    .equalsTo(Conversion.convert(propertyValues[i], String.class));
        }
        final Iterable<StorageNode> foundItems = builder.buildCriteria().andSearch(currentSession);

        final IteratorBuilder.SimpleIteratorBuilder<T, StorageNode> b = IteratorBuilder
                .<T, StorageNode>createIteratorBuilder();
        b.withConverter(new IteratorBuilder.Converter<T, StorageNode>() {
            @Override
            public T convert(final StorageNode nodeEntry) throws Exception {
                return (T) convertNodeToBean(nodeEntry);
            }
        });
        b.withReferee(new IteratorBuilder.NextItemReferee<StorageNode>() {
            @Override
            public boolean canAcceptAsNewItem(final StorageNode nodeEntry) {
                final String typeAsString = nodeEntry.getPropertyValueAsString(currentSession, NODE_ENTRY_TYPE);
                if (typeAsString != null && typeAsString.equals(beanType.getName())) {
                    if (parent != null) {
                        StorageNode parentNode = nodeEntry;
                        while (parentNode != null) {
                            if (parentNode.getKey().equals(parent.getKey())) {
                                return true;
                            }
                            parentNode = parentNode.getParent(currentSession);
                        }
                        return false;
                    }
                    return true;
                }
                return false;
            }
        });
        final Iterable<T> result = b.withItems(foundItems).andBuild();
        return result;

    } catch (final Exception e) {
        throw logAndReturnNew(e, SLRuntimeException.class);

    }

}

From source file:org.openspotlight.persist.util.SimpleNodeTypeVisitorSupport.java

@SuppressWarnings("unchecked")
public static Set<SimpleNodeType> fillItems(final SimpleNodeType rootNode,
        final Class<? extends Annotation>... annotationsToIgnore) throws Exception {

    if (rootNode == null) {
        return Collections.emptySet();
    }/*from ww  w  .j  av  a  2s .  co  m*/
    final Set<SimpleNodeType> itemsToVisit = new LinkedHashSet<SimpleNodeType>();

    final PropertyDescriptor[] allDescriptors = PropertyUtils.getPropertyDescriptors(rootNode);
    looping: for (final PropertyDescriptor desc : allDescriptors) {
        final Method readMethod = desc.getReadMethod();
        if (readMethod.isAnnotationPresent(ParentProperty.class)) {
            continue looping;
        }
        for (final Class<? extends Annotation> annotation : annotationsToIgnore) {
            if (readMethod.isAnnotationPresent(annotation)) {
                continue looping;
            }
        }
        final Class<?> currentType = desc.getPropertyType();
        if (SimpleNodeType.class.isAssignableFrom(currentType)) {
            final SimpleNodeType bean = (SimpleNodeType) readMethod.invoke(rootNode);
            itemsToVisit.add(bean);
        } else if (Iterable.class.isAssignableFrom(currentType)) {
            final UnwrappedCollectionTypeFromMethodReturn<Object> metadata = Reflection
                    .unwrapCollectionFromMethodReturn(readMethod);
            if (SimpleNodeType.class.isAssignableFrom(metadata.getItemType())) {
                final Iterable<SimpleNodeType> collection = (Iterable<SimpleNodeType>) readMethod
                        .invoke(rootNode);
                if (collection != null) {
                    for (final SimpleNodeType t : collection) {
                        itemsToVisit.add(t);
                    }
                }
            }
        } else if (Map.class.isAssignableFrom(currentType)) {
            final UnwrappedMapTypeFromMethodReturn<Object, Object> metadata = Reflection
                    .unwrapMapFromMethodReturn(readMethod);
            if (SimpleNodeType.class.isAssignableFrom(metadata.getItemType().getK2())) {
                final Map<Object, SimpleNodeType> map = (Map<Object, SimpleNodeType>) readMethod
                        .invoke(rootNode);
                if (map != null) {
                    for (final Entry<Object, SimpleNodeType> entry : map.entrySet()) {
                        itemsToVisit.add(entry.getValue());
                    }
                }
            }
        }
    }
    return itemsToVisit;
}

From source file:org.opoo.oqs.core.mapper.BeanPropertyMapper.java

/**
 * Map<String, ?> in JDK1.5/*from   w ww . j a v  a  2 s  .c o m*/
 *
 * @param rs ResultSet
 * @param rowNum int
 * @param type Class
 * @return Map
 * @throws SQLException
 */
private Map getValues(ResultSet rs, int rowNum, Class type) throws SQLException {
    Map map = new HashMap();
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(type);
    for (int i = 0; i < mappers.length; i++) {
        PropertyMapper pm = mappers[i];
        if (PropertyTypeAware.class.isInstance(pm)) { //(pm instanceof PropertyTypeAware)
            PropertyDescriptor pd = findPropertyDescriptor(pds, mappers[i].getName());
            if (pd != null) {
                ((PropertyTypeAware) pm).setPropertyType(pd.getPropertyType());
            }
        }
        Object value = pm.map(rs, rowNum);
        map.put(pm.getName(), value);
        //setProperty(target, mappers[i].getName(), value);
    }
    return map;
}

From source file:org.ppwcode.vernacular.l10n_III.dojo.DojoI18nServlet.java

private HashMap<String, String> getTranslations(String fqcn, Locale loc) {
    HashMap<String, String> translations = new HashMap<String, String>();
    System.out.println("Request started");

    Class<?> type = TypeHelpers.type(fqcn);

    DefaultResourceBundleLoadStrategy strategy = new DefaultResourceBundleLoadStrategy();
    strategy.setLocale(loc);/*from w  w w. j a va 2  s  . co  m*/

    String typeNameSingle = I18nLabelHelpers.i18nTypeLabel(type, false, strategy);
    String typeNamePlural = I18nLabelHelpers.i18nTypeLabel(type, true, strategy);

    if (typeNameSingle != null)
        translations.put(I18nLabelHelpers.I18N_TYPE_LABEL_KEY, typeNameSingle);
    if (typeNamePlural != null)
        translations.put(I18nLabelHelpers.I18N_PLURAL_TYPE_LABEL_KEY, typeNamePlural);

    System.out.println("Entity name: " + typeNameSingle);
    System.out.println("Entity name plural: " + typeNamePlural);

    PropertyDescriptor[] props = PropertyUtils.getPropertyDescriptors(type);
    for (PropertyDescriptor prop : props) {
        String propLabelLong = I18nLabelHelpers.i18nPropertyLabel(prop.getName(), type, false, strategy);
        String propLabelShort = I18nLabelHelpers.i18nPropertyLabel(prop.getName(), type, true, strategy);

        if (propLabelLong != null)
            translations.put(I18nLabelHelpers.I18N_PROPERTY_LABEL_KEY_PREFIX + prop.getName(), propLabelLong);
        if (propLabelShort != null)
            translations.put(I18nLabelHelpers.I18N_SHORT_PROPERTY_LABEL_KEY_PREFIX + prop.getName(),
                    propLabelShort);

        System.out.println("Property: " + prop.getName());
        System.out.println("Property name: " + propLabelLong);
        System.out.println("Property name short: " + propLabelShort);
    }
    System.out.println("Request finished");
    return translations;
}

From source file:org.seasar.mayaa.impl.util.ObjectUtil.java

/**
 * ???????????/*  ww  w .java2  s  .  c o  m*/
 *
 * @param beanClass ??????
 * @return ????
 */
public static String[] getPropertyNames(Class beanClass) {
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
    String[] result = new String[descriptors.length];
    for (int i = 0; i < descriptors.length; i++) {
        result[i] = descriptors[i].getName();
    }
    return result;
}

From source file:org.seasar.mayaa.impl.util.ObjectUtil.java

/**
 * ????????????/*from   ww w  . java 2  s .  c om*/
 *
 * @param beanClass ?
 * @param propertyName ???
 * @return ???true
 */
public static boolean hasProperty(Class beanClass, String propertyName) {
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
    for (int i = 0; i < descriptors.length; i++) {
        if (descriptors[i].getName().equals(propertyName)) {
            return true;
        }
    }
    return false;
}

From source file:org.seasar.mayaa.impl.util.ObjectUtil.java

/**
 * ?????//from  w ww.  ja  v  a 2s .  c  o  m
 *
 * @param beanClass 
 * @param propertyName ??
 * @return beanClass??
 */
public static Class getPropertyClass(Class beanClass, String propertyName) {
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
    for (int i = 0; i < descriptors.length; i++) {
        if (descriptors[i].getName().equals(propertyName)) {
            return descriptors[i].getPropertyType();
        }
    }
    return null;
}

From source file:org.snowfk.util.ObjectUtil.java

@Deprecated
public static void copyNotNull(Object src, Object dest) {
    try {/*from ww  w  .  j  a v a  2s  .  c  om*/

        PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(src);
        for (PropertyDescriptor pd : pds) {
            String name = pd.getName();
            if (pd.getWriteMethod() != null) {
                Object value = PropertyUtils.getProperty(src, name);
                if (value != null) {
                    PropertyUtils.setProperty(dest, name, value);
                }
            }
        }

    } catch (Exception e) {
        logger.error(e.getMessage());
        throw (new RuntimeException(e));
    }
}

From source file:org.snowfk.util.ObjectUtil.java

/**
 * Copy all the properties are that not null, or not empty (for List and
 * array objects) to the dest object./* ww  w.ja v  a2  s  .  c  om*/
 * 
 * @param src
 * @param dest
 */
@Deprecated
public static void copyNotNullNotEmpty(Object src, Object dest) {
    try {

        PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(src);
        for (PropertyDescriptor pd : pds) {
            String name = pd.getName();

            if (pd.getWriteMethod() != null) {
                Object value = PropertyUtils.getProperty(src, name);
                boolean copy = true;

                if (value == null) {
                    copy = false;
                } else if (value instanceof List && ((List) value).size() < 1) {
                    copy = false;
                } else if (value.getClass().isArray() && ((Object[]) value).length < 1) {
                    copy = false;
                }

                if (copy) {
                    PropertyUtils.setProperty(dest, name, value);
                }
            }
        }
        // BeanUtils.copyProperties(dest, src);
    } catch (Exception e) {
        logger.error(e.getMessage());
        throw (new RuntimeException(e));
    }
}

From source file:org.thiesen.helenaorm.HelenaColumnDAO.java

public HelenaColumnDAO(final Class<T> clz, final String hostname, final int port,
        final SerializeUnknownClasses serializationPolicy,
        final ImmutableMap<Class<?>, TypeMapping<?>> typeMappings) {

    if (!clz.isAnnotationPresent(HelenaColumnBean.class)) {
        throw new IllegalArgumentException(
                "Trying to get a HelenaColumnDAO for a class that is not mapped with @HelenaColumnBean");
    }//from   w w  w .j  ava 2  s .co  m

    final HelenaColumnBean annotation = clz.getAnnotation(HelenaColumnBean.class);
    this.typeConverter = new TypeConverter(typeMappings, serializationPolicy);
    //      this.clz = clz;

    this.keyspace = annotation.keyspace();
    this.columnFamily = annotation.columnFamily();
    this.secondaryColumnFamily = annotation.secondaryColumnFamily();

    this.hostname = hostname;
    this.port = port;

    if (annotation.consistency() != null) {
        this.setConsistencyLevel(annotation.consistency());
    }

    this.fields = new HashMap<String, Field>();
    for (Field field : clz.getDeclaredFields()) {
        this.fields.put(field.getName(), field);
    }

    this.propertyDescriptors = PropertyUtils.getPropertyDescriptors(clz);
    for (final PropertyDescriptor descriptor : propertyDescriptors) {
        if (isKeyProperty(descriptor)) {
            this.keyPropertyDescriptor = descriptor;
        } else if (isColumnProperty(descriptor)) {
            this.columnPropertyDescriptor = descriptor;
        } else if (isValueProperty(descriptor)) {
            this.valuePropertyDescriptor = descriptor;
        }
    }

    if (keyPropertyDescriptor == null) {
        throw new HelenaRuntimeException(
                "Could not find key of class " + clz.getName() + ", did you annotate with @KeyProperty");
    }

    if (columnPropertyDescriptor == null) {
        throw new HelenaRuntimeException(
                "Could not find column of class " + clz.getName() + ", did you annotate with @ColumnProperty");
    }
}