Example usage for org.springframework.beans BeanUtils getPropertyDescriptor

List of usage examples for org.springframework.beans BeanUtils getPropertyDescriptor

Introduction

In this page you can find the example usage for org.springframework.beans BeanUtils getPropertyDescriptor.

Prototype

@Nullable
public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName)
        throws BeansException 

Source Link

Document

Retrieve the JavaBeans PropertyDescriptors for the given property.

Usage

From source file:grails.util.GrailsClassUtils.java

/**
 * Retrieves a property of the given class of the specified name and type
 * @param clazz The class to retrieve the property from
 * @param propertyName The name of the property
 * @param propertyType The type of the property
 *
 * @return A PropertyDescriptor instance or null if none exists
 *//*www . j  a  v  a2s . c  o m*/
public static PropertyDescriptor getProperty(Class<?> clazz, String propertyName, Class<?> propertyType) {
    if (clazz == null || propertyName == null || propertyType == null) {
        return null;
    }

    try {
        PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, propertyName);
        if (pd != null && pd.getPropertyType().equals(propertyType)) {
            return pd;
        }
        return null;
    } catch (Exception e) {
        // if there are any errors in instantiating just return null for the moment
        return null;
    }
}

From source file:grails.util.GrailsClassUtils.java

/**
 * Retrieves a property of the given class of the specified name and type
 * @param clazz The class to retrieve the property from
 * @param propertyName The name of the property
 *
 * @return A PropertyDescriptor instance or null if none exists
 *//*  w w  w  .  j a  va 2  s. com*/
public static PropertyDescriptor getProperty(Class<?> clazz, String propertyName) {
    if (clazz == null || propertyName == null) {
        return null;
    }

    try {
        return BeanUtils.getPropertyDescriptor(clazz, propertyName);
    } catch (Exception e) {
        // if there are any errors in instantiating just return null for the moment
        return null;
    }
}

From source file:grails.util.GrailsClassUtils.java

/**
 * Checks whether the specified property is inherited from a super class
 *
 * @param clz The class to check/*from  www  .ja  va 2s  .c om*/
 * @param propertyName The property name
 * @return true if the property is inherited
 */
@SuppressWarnings("rawtypes")
public static boolean isPropertyInherited(Class clz, String propertyName) {
    if (clz == null)
        return false;
    Assert.isTrue(StringUtils.hasText(propertyName), "Argument [propertyName] cannot be null or blank");

    Class<?> superClass = clz.getSuperclass();

    PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(superClass, propertyName);
    if (pd != null && pd.getReadMethod() != null) {
        return true;
    }
    return false;
}

From source file:it.doqui.index.ecmengine.client.webservices.AbstractWebServiceDelegateBase.java

@SuppressWarnings("unchecked")
protected Object convertDTO(Object sourceDTO, Class destDTOClass) throws Exception {
    if (log.isDebugEnabled()) {
        log.debug("[" + getClass().getSimpleName() + "::convertDTO] BEGIN");
    }/* w w  w.j a  v a 2 s .  c  om*/

    Object destDTO = null;
    try {
        if (sourceDTO != null) {
            if (log.isDebugEnabled()) {
                log.debug("[" + getClass().getSimpleName() + "::convertDTO] converting DTO from type "
                        + sourceDTO.getClass().getName() + " to type " + destDTOClass.getName());
            }
            destDTO = BeanUtils.instantiateClass(destDTOClass);
            PropertyDescriptor[] targetpds = BeanUtils.getPropertyDescriptors(destDTOClass);
            PropertyDescriptor sourcepd = null;
            if (log.isDebugEnabled()) {
                log.debug(" found " + targetpds.length + " properties for type " + destDTOClass.getName());
            }

            for (int i = 0; i < targetpds.length; i++) {
                if (targetpds[i].getWriteMethod() != null) {
                    Method writeMethod = targetpds[i].getWriteMethod();
                    sourcepd = BeanUtils.getPropertyDescriptor(sourceDTO.getClass(), targetpds[i].getName());
                    if (sourcepd != null && sourcepd.getReadMethod() != null) {
                        if (log.isDebugEnabled()) {
                            log.debug("[" + getClass().getSimpleName() + "::convertDTO] found property: "
                                    + targetpds[i].getName());
                            log.debug("[" + getClass().getSimpleName() + "::convertDTO] source type: "
                                    + sourcepd.getPropertyType().getName() + ", dest type: "
                                    + targetpds[i].getPropertyType().getName());
                        }
                        Method readMethod = sourcepd.getReadMethod();
                        Object valueObject = null;
                        if (!BeanUtils.isSimpleProperty(targetpds[i].getPropertyType())) {
                            if (sourcepd.getPropertyType().isArray()) {
                                valueObject = convertDTOArray(
                                        (Object[]) readMethod.invoke(sourceDTO, new Object[] {}),
                                        targetpds[i].getPropertyType().getComponentType());
                            } else if (sourcepd.getPropertyType().equals(java.util.Calendar.class)
                                    && targetpds[i].getPropertyType().equals(java.util.Date.class)) {
                                // if java.util.Calendar => convert to java.util.Date
                                valueObject = readMethod.invoke(sourceDTO, new Object[0]);
                                if (valueObject != null) {
                                    valueObject = ((Calendar) valueObject).getTime();
                                }
                            } else if (sourcepd.getPropertyType().equals(java.util.Date.class)
                                    && targetpds[i].getPropertyType().equals(java.util.Calendar.class)) {
                                // if java.util.Date => convert to java.util.Calendar
                                Calendar calendar = Calendar.getInstance();
                                valueObject = readMethod.invoke(sourceDTO, new Object[0]);
                                if (valueObject != null) {
                                    calendar.setTime((Date) valueObject);
                                    valueObject = calendar;
                                }
                            } else {
                                valueObject = convertDTO(readMethod.invoke(sourceDTO, new Object[0]),
                                        targetpds[i].getPropertyType());
                            }
                        } else {
                            valueObject = readMethod.invoke(sourceDTO, new Object[0]);
                        }
                        if (log.isDebugEnabled()) {
                            log.debug("[" + getClass().getSimpleName() + "::convertDTO] writing value: "
                                    + valueObject);
                        }
                        writeMethod.invoke(destDTO, new Object[] { valueObject });
                    } else {
                        if (log.isDebugEnabled()) {
                            log.debug("[" + getClass().getSimpleName() + "::convertDTO] skipping property: "
                                    + targetpds[i].getName());
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("[" + getClass().getSimpleName() + "::convertDTO] ERROR", e);
        throw e;
    } finally {
        if (log.isDebugEnabled()) {
            log.debug("[" + getClass().getSimpleName() + "::convertDTO] END");
        }
    }
    return destDTO;
}

From source file:jp.co.ctc_g.jfw.core.util.Beans.java

/**
 * ???????????????//from   w w  w.  j a  v  a  2 s .  co m
 * @param clazz 
 * @param propertyName ??
 * @return 
 */
public static PropertyDescriptor findPropertyDescriptorFor(Class<?> clazz, String propertyName) {
    Args.checkNotNull(clazz);
    return BeanUtils.getPropertyDescriptor(clazz, propertyName);
}

From source file:ome.services.graphs.GraphPathBean.java

/**
 * If the given property of the given class is actually declared by an interface that it implements,
 * find the name of the interface that first declares the property.
 * @param className the name of an {@link IObject} class
 * @param propertyName the name of a property of the class
 * @return the interface declaring the property, or {@code null} if none
 */// w w  w  . j  a  va 2s  .co m
private Class<? extends IObject> getInterfaceForProperty(String className, String propertyName) {
    Class<? extends IObject> interfaceForProperty = null;
    Set<Class<? extends IObject>> interfacesFrom, interfacesTo;
    try {
        interfacesFrom = ImmutableSet
                .<Class<? extends IObject>>of(Class.forName(className).asSubclass(IObject.class));
    } catch (ClassNotFoundException e) {
        log.error("could not load " + IObject.class.getName() + " subclass " + className);
        return null;
    }
    while (!interfacesFrom.isEmpty()) {
        interfacesTo = new HashSet<Class<? extends IObject>>();
        for (final Class<? extends IObject> interfaceFrom : interfacesFrom) {
            if (interfaceFrom.isInterface()
                    && BeanUtils.getPropertyDescriptor(interfaceFrom, propertyName) != null) {
                interfaceForProperty = interfaceFrom;
            }
            for (final Class<?> newInterface : interfaceFrom.getInterfaces()) {
                if (newInterface != IObject.class && IObject.class.isAssignableFrom(newInterface)) {
                    interfacesTo.add(newInterface.asSubclass(IObject.class));
                    classesBySimpleName.put(newInterface.getSimpleName(),
                            newInterface.asSubclass(IObject.class));
                }
            }
        }
        interfacesFrom = interfacesTo;
    }
    return interfaceForProperty == null ? null : interfaceForProperty;
}

From source file:org.apache.syncope.client.console.bulk.BulkContent.java

private Object getTargetId(final Object target, final String idFieldName)
        throws IllegalAccessException, InvocationTargetException {

    return BeanUtils.getPropertyDescriptor(target.getClass(), idFieldName).getReadMethod().invoke(target,
            new Object[0]);
}

From source file:org.beangle.ems.security.restrict.service.IdentifierDataResolver.java

@SuppressWarnings("unchecked")
public <T> List<T> unmarshal(RestrictField field, String text) {
    if (null == field.getType()) {
        return (List<T>) CollectUtils.newArrayList(StringUtils.split(text, ","));
    } else {//from ww w .  j a  v a  2 s .  co m
        Class<?> clazz = null;
        try {
            clazz = Class.forName(field.getType());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        EntityType myType = Model.getEntityType(clazz);
        OqlBuilder<T> builder = OqlBuilder.from(myType.getEntityName(), "restrictField");

        String[] ids = StringUtils.split(text, ",");
        PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, field.getKeyName());
        Class<?> propertyType = pd.getReadMethod().getReturnType();
        List<Object> realIds = CollectUtils.newArrayList(ids.length);
        for (String id : ids) {
            Object realId = ConvertUtils.convert(id, propertyType);
            realIds.add(realId);
        }
        builder.where("restrictField." + field.getKeyName() + " in (:ids)", realIds).cacheable();
        return entityDao.search(builder);
    }
}

From source file:org.codehaus.groovy.grails.commons.GrailsClassUtils.java

/**
 * Returns the type of the given property contained within the specified class
 *
 * @param clazz The class which contains the property
 * @param propertyName The name of the property
 *
 * @return The property type or null if none exists
 *//*ww w . ja va  2s. c o  m*/
public static Class<?> getPropertyType(Class<?> clazz, String propertyName) {
    if (clazz == null || StringUtils.isBlank(propertyName)) {
        return null;
    }

    try {
        PropertyDescriptor desc = BeanUtils.getPropertyDescriptor(clazz, propertyName);
        if (desc != null) {
            return desc.getPropertyType();
        }
        return null;
    } catch (Exception e) {
        // if there are any errors in instantiating just return null for the moment
        return null;
    }
}

From source file:org.codehaus.groovy.grails.commons.GrailsClassUtils.java

/**
 * Checks whether the specified property is inherited from a super class
 *
 * @param clz The class to check/*from  w w  w .j a  v a 2s . co  m*/
 * @param propertyName The property name
 * @return true if the property is inherited
 */
@SuppressWarnings("rawtypes")
public static boolean isPropertyInherited(Class clz, String propertyName) {
    if (clz == null)
        return false;
    Assert.isTrue(!StringUtils.isBlank(propertyName), "Argument [propertyName] cannot be null or blank");

    Class<?> superClass = clz.getSuperclass();

    PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(superClass, propertyName);
    if (pd != null && pd.getReadMethod() != null) {
        return true;
    }
    return false;
}