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.cyclopsgroup.tornado.hibernate.taglib.NewTag.java

/**
 * Overwrite or implement method getAttributeType()
 *
 * @see com.cyclopsgroup.waterview.utils.DynaTagSupport#getAttributeType(java.lang.String)
 *///from  w  ww .  j av  a2 s . co m
public Class getAttributeType(String attributeName) throws JellyTagException {
    if (attributeName.equals("var")) {
        return String.class;
    }
    if (descriptors == null) {
        ClassTag classTag = (ClassTag) requireParent(ClassTag.class);
        descriptors = PropertyUtils.getPropertyDescriptors(classTag.getEntityClass());
    }
    for (int i = 0; i < descriptors.length; i++) {
        PropertyDescriptor descriptor = descriptors[i];
        if (descriptor.getName().equals(attributeName)) {
            return descriptor.getPropertyType();
        }
    }
    throw new JellyTagException("Attribute " + attributeName + " is not supported");
}

From source file:net.jakubholy.jeeutils.jsfelcheck.util.BeanPropertyUtils.java

/**
 * Find the type of the given property./*  w w  w  .j a  va  2  s.c  o  m*/
 * @param propertyName (required)
 * @return the type of the property or null if not found
 */
public Class<?> getPropertyTypeOf(String propertyName) {
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(type);
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getName().equals(propertyName)) {
            return descriptor.getPropertyType();
        }
    }

    LOG.fine("No property '" + propertyName + "' found on the class " + type.getName());

    return null;
}

From source file:net.mojodna.sprout.support.SproutUtils.java

/**
 * Bean initialization method.  Uses reflection to determine properties
 * for which auto-wiring may be appropriate.  Subsequently attempts to
 * retrieve appropriate beans from the WebApplicationContext and set them
 * locally.//  w w  w . ja  v a2 s  .c o  m
 * 
 * @param bean Bean to initialize.
 * @param context WebApplicationContext containing Spring beans.
 * @param clazz Type of Sprout.  This is used to determine which declared
 * methods are candidates for auto-wiring.
 */
public static void initialize(final Object bean, final WebApplicationContext context, final Class clazz) {
    final Collection<Method> methods = SproutUtils.getDeclaredMethods(bean.getClass(), clazz);

    final PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(bean.getClass());
    for (final PropertyDescriptor descriptor : descriptors) {
        final Class type = descriptor.getPropertyType();

        // beans should never be of type String
        // there must be a write method present
        // the write method must exist within the relevant subset of declared methods
        if (!type.equals(String.class) && null != descriptor.getWriteMethod()
                && methods.contains(descriptor.getWriteMethod())) {
            final Object serviceBean = context.getBean(descriptor.getName());
            if (null != serviceBean) {
                try {
                    log.debug("Wiring property '" + descriptor.getName() + "' with bean of type "
                            + serviceBean.getClass().getName());
                    PropertyUtils.setProperty(bean, descriptor.getName(), serviceBean);
                } catch (final IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (final InvocationTargetException e) {
                    throw new RuntimeException(e);
                } catch (final NoSuchMethodException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * TODO additional lifecycle interface callbacks as defined in BeanFactory
     * should be implemented here
     * @see org.springframework.beans.factory.BeanFactory
     */

    // InitializingBean callback
    if (bean instanceof InitializingBean) {
        try {
            ((InitializingBean) bean).afterPropertiesSet();
        } catch (final Exception e) {
            log.warn("Exception while running afterPropertiesSet() on an InitializingBean: " + e.getMessage(),
                    e);
        }
    }
}

From source file:com.hack23.cia.service.data.impl.util.LoadHelper.java

/**
 * Recursive initliaze.//from  w  w  w.  j av  a 2s.  c o m
 *
 * @param obj
 *            the obj
 * @param dejaVu
 *            the deja vu
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 * @throws NoSuchMethodException
 *             the no such method exception
 */
private static void recursiveInitialize(final Object obj, final Set<Object> dejaVu)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (dejaVu.contains(obj)) {
        return;
    } else {
        dejaVu.add(obj);

        if (!Hibernate.isInitialized(obj)) {
            Hibernate.initialize(obj);
        }

        if (obj instanceof HibernateProxy || obj instanceof PersistentCollection) {

            initProxyAndCollections(obj, PropertyUtils.getPropertyDescriptors(obj), dejaVu);
        }
    }
}

From source file:com.qcadoo.model.internal.classconverter.ModelXmlToClassConverterTest.java

@BeforeClass
public static void init() throws Exception {
    MODEL_XML_TO_CLASSCONVERTER.setBeanClassLoader(ClassLoader.getSystemClassLoader());

    for (Class<?> clazz : MODEL_XML_TO_CLASSCONVERTER.convert(Utils.FULL_FIRST_ENTITY_XML_RESOURCE,
            Utils.FULL_SECOND_ENTITY_XML_RESOURCE, Utils.FULL_THIRD_ENTITY_XML_RESOURCE,
            Utils.OTHER_FIRST_ENTITY_XML_RESOURCE, Utils.OTHER_SECOND_ENTITY_XML_RESOURCE)) {
        classes.put(clazz.getCanonicalName(), clazz);
    }//from  ww  w .jav a  2  s.  c o  m

    for (PropertyDescriptor propertyDescriptor : PropertyUtils.getPropertyDescriptors(
            classes.get(ClassNameUtils.getFullyQualifiedClassName("full", "firstEntity")))) {
        propertyDescriptors.put(propertyDescriptor.getName(), propertyDescriptor);
    }
}

From source file:de.alpharogroup.lang.object.MergeObjectExtensions.java

/**
 * Merge the given to object with the given 'with' object.
 *
 * @param <TO>//from  w w  w .j  av  a  2  s  .  com
 *            the generic type of the object to merge in
 * @param <WITH>
 *            the generic type of the object to merge with
 * @param toObject
 *            the object to merge in
 * @param withObject
 *            the object to merge with
 * @throws InvocationTargetException
 *             if the property accessor method throws an exception
 * @throws IllegalAccessException
 *             if the caller does not have access to the property accessor method
 */
public static final <TO, WITH> void merge(final TO toObject, final WITH withObject)
        throws InvocationTargetException, IllegalAccessException {
    Check.get().notNull(toObject, "toObject").notNull(withObject, "toObject");

    final Class<?> toClass = toObject.getClass();

    final PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(toClass);

    for (final PropertyDescriptor descriptor : propertyDescriptors) {
        mergeProperty(toObject, withObject, descriptor);
    }
}

From source file:io.lightlink.dao.mapping.BeanMapper.java

public BeanMapper(Class paramClass, List<String> fieldsOfLine) {

    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(paramClass);
    descriptorMap = new CaseInsensitiveMap();
    for (PropertyDescriptor descriptor : descriptors) {
        descriptorMap.put(normalizePropertyName(descriptor.getName()), descriptor);
    }// w w w.java 2s . co m

    List<String> ownFields = new ArrayList<String>();
    Map<String, List<String>> fieldsByChild = new CaseInsensitiveMap();

    groupFields(fieldsOfLine, ownFields, fieldsByChild);

    this.ownFields = ownFields;
    for (Map.Entry<String, List<String>> entry : fieldsByChild.entrySet()) {
        String childProperty = normalizePropertyName(entry.getKey());
        PropertyDescriptor descriptor = descriptorMap.get(childProperty);
        if (descriptor != null) {
            List<String> properties = entry.getValue();
            if (descriptor.getPropertyType().isAssignableFrom(ArrayList.class)) {
                Type[] typeArguments = ((ParameterizedType) descriptor.getReadMethod().getGenericReturnType())
                        .getActualTypeArguments();
                if (typeArguments.length == 0)
                    throw new RuntimeException("Cannot define Generic list type of " + entry.getKey()
                            + " property of " + paramClass);
                Type firstType = typeArguments[0];
                if (firstType instanceof Class) {
                    childListMappers.put(childProperty, new BeanMapper((Class) firstType, properties));
                }
            } else {
                childMappers.put(childProperty, new BeanMapper(descriptor.getPropertyType(), properties));
            }
        } else {
            throw new RuntimeException(
                    "cannot define mapping for class:" + paramClass.getName() + " property:" + childProperty);
        }
    }

    this.paramClass = paramClass;
}

From source file:com.erinors.hpb.server.handler.HibernateEmbeddedObjectHandler.java

@Override
public Object merge(MergingContext context, Object object) {
    if (object == null || !object.getClass().isAnnotationPresent(Embeddable.class)) {
        return null;
    }/*from   w  w w . j av a  2 s . com*/

    boolean empty = true;
    try {
        for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(object)) {
            String propertyName = pd.getName();

            if (!"class".equals(propertyName) && !pd.getReadMethod().isAnnotationPresent(Transient.class)) {
                if (PropertyUtils.getSimpleProperty(object, propertyName) != null) {
                    empty = false;
                    break;
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error while checking embedded object: " + object, e);
    }

    if (empty) {
        context.addProcessedObject(object, null);
        return ProcessedToNull;
    } else {
        return null;
    }
}

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

public void initializeFrom(T aObject) {
    ModelProperties theProperties = aObject.getProperties();

    try {//from  w  w w  .j a  v  a 2s  .c o  m
        for (PropertyDescriptor theDescriptor : PropertyUtils.getPropertyDescriptors(this)) {
            if (theDescriptor.getReadMethod() != null && theDescriptor.getWriteMethod() != null) {
                String theValue = theProperties.getProperty(theDescriptor.getName());
                if (!StringUtils.isEmpty(theValue)) {
                    Class theType = theDescriptor.getPropertyType();

                    if (theType.isEnum()) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(),
                                Enum.valueOf(theType, theValue));
                    }
                    if (String.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), theValue);
                    }
                    if (Long.class.equals(theType) || long.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), Long.parseLong(theValue));
                    }
                    if (Integer.class.equals(theType) || int.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), Integer.parseInt(theValue));
                    }
                    if (Boolean.class.equals(theType) || boolean.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(),
                                Boolean.parseBoolean(theValue));
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:io.github.moosbusch.lumpi.util.FormUtil.java

public static Map<String, Class<?>> getPropertyTypesMap(final Class<?> type,
        Map<String, Set<Class<?>>> excludedProperties) {
    final Map<String, Class<?>> result = new HashMap<>();
    PropertyDescriptor[] propDescs = PropertyUtils.getPropertyDescriptors(type);
    Map<String, Set<Class<?>>> excludedProps = excludedProperties;

    if (excludedProps != null) {
        for (PropertyDescriptor propDesc : propDescs) {
            Class<?> propertyType = propDesc.getPropertyType();

            if (propertyType != null) {
                String propertyName = propDesc.getName();

                if (excludedProps.containsKey(propertyName)) {
                    Set<Class<?>> ignoredPropertyTypes = excludedProps.get(propertyName);

                    if (!ignoredPropertyTypes.contains(type)) {
                        Set<Class<?>> superTypes = LumpiUtil.getSuperTypes(type, false, true, true);

                        for (Class<?> superType : superTypes) {
                            if (ignoredPropertyTypes.contains(superType)) {
                                result.put(propertyName, propertyType);
                                break;
                            }/*from  www.  j  a v a  2s.c  o  m*/
                        }
                    }

                }
            }
        }
    }

    return result;
}