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:org.jdal.swing.ListTableModel.java

/**
 * Get a primary key of entity in the list
 * @param row row of model//www . j  a va  2s  .  co m
 * @return the primary key of model, if any
 */
private Object getPrimaryKey(Object row) {
    if (BeanUtils.getPropertyDescriptor(modelClass, id) == null)
        return row;

    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(row);
    return wrapper.getPropertyValue(id);
}

From source file:ch.ralscha.extdirectspring.util.ParametersResolver.java

private Map<String, Object> fillReadRequestFromMap(ExtDirectStoreReadRequest to, Map<String, Object> from) {
    Set<String> foundParameters = new HashSet<String>();

    for (Entry<String, Object> entry : from.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();

        if (key.equals("filter")) {
            List<Filter> filters = new ArrayList<Filter>();

            if (value instanceof String) {
                List<Map<String, Object>> rawFilters = this.jsonHandler.readValue((String) value,
                        new TypeReference<List<Map<String, Object>>>() {/* empty */
                        });//  ww w  .  ja v  a2s. c  om

                for (Map<String, Object> rawFilter : rawFilters) {
                    Filter filter = Filter.createFilter(rawFilter, this.conversionService);
                    if (filter != null) {
                        filters.add(filter);
                    }
                }
            } else if (value instanceof List) {
                @SuppressWarnings("unchecked")
                List<Map<String, Object>> filterList = (List<Map<String, Object>>) value;
                for (Map<String, Object> rawFilter : filterList) {
                    Filter filter = Filter.createFilter(rawFilter, this.conversionService);
                    if (filter != null) {
                        filters.add(filter);
                    }
                }
            }
            to.setFilters(filters);
            foundParameters.add(key);
        } else if (key.equals("sort") && value != null && value instanceof List) {

            List<SortInfo> sorters = new ArrayList<SortInfo>();
            @SuppressWarnings("unchecked")
            List<Map<String, Object>> rawSorters = (List<Map<String, Object>>) value;

            for (Map<String, Object> aRawSorter : rawSorters) {
                sorters.add(SortInfo.create(aRawSorter));
            }

            to.setSorters(sorters);
            foundParameters.add(key);
        } else if (key.equals("group") && value != null && value instanceof List) {
            List<GroupInfo> groups = new ArrayList<GroupInfo>();
            @SuppressWarnings("unchecked")
            List<Map<String, Object>> rawGroups = (List<Map<String, Object>>) value;

            for (Map<String, Object> aRawGroupInfo : rawGroups) {
                groups.add(GroupInfo.create(aRawGroupInfo));
            }

            to.setGroups(groups);
            foundParameters.add(key);
        } else {

            PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(to.getClass(), key);
            if (descriptor != null && descriptor.getWriteMethod() != null) {
                try {

                    descriptor.getWriteMethod().invoke(to,
                            this.conversionService.convert(value, descriptor.getPropertyType()));

                    foundParameters.add(key);
                } catch (IllegalArgumentException e) {
                    log.error("fillObjectFromMap", e);
                } catch (IllegalAccessException e) {
                    log.error("fillObjectFromMap", e);
                } catch (InvocationTargetException e) {
                    log.error("fillObjectFromMap", e);
                }
            }
        }
    }

    if (to.getLimit() != null) {
        // this test is no longer needed with extjs 4.0.7 and extjs 4.1.0
        // these two libraries always send page, start and limit
        if (to.getPage() != null && to.getStart() == null) {
            to.setStart(to.getLimit() * (to.getPage() - 1));
            // the else if is still valid for extjs 3 code
        } else if (to.getPage() == null && to.getStart() != null) {
            to.setPage(to.getStart() / to.getLimit() + 1);
        }
    }

    if (to.getSort() != null && to.getDir() != null) {
        List<SortInfo> sorters = new ArrayList<SortInfo>();
        sorters.add(new SortInfo(to.getSort(), SortDirection.fromString(to.getDir())));
        to.setSorters(sorters);
    }

    if (to.getGroupBy() != null && to.getGroupDir() != null) {
        List<GroupInfo> groups = new ArrayList<GroupInfo>();
        groups.add(new GroupInfo(to.getGroupBy(), SortDirection.fromString(to.getGroupDir())));
        to.setGroups(groups);
    }

    Map<String, Object> remainingParameters = new HashMap<String, Object>();
    for (Entry<String, Object> entry : from.entrySet()) {
        if (!foundParameters.contains(entry.getKey())) {
            remainingParameters.put(entry.getKey(), entry.getValue());
        }
    }
    to.setParams(remainingParameters);

    return remainingParameters;
}

From source file:org.codehaus.griffon.commons.GriffonClassUtils.java

/**
 * Checks whether the specified property is inherited from a super class
 *
 * @param clz The class to check/*  w w  w  . j a v a2 s.  com*/
 * @param propertyName The property name
 * @return True if the property is inherited
 */
public static boolean isPropertyInherited(Class clz, String propertyName) {
    if (clz == null)
        return false;
    if (StringUtils.isBlank(propertyName))
        throw new IllegalArgumentException("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:ch.rasc.extclassgenerator.ModelGenerator.java

private static void createModelBean(ModelBean model, AccessibleObject accessibleObject,
        OutputConfig outputConfig) {//from   w  w w  .jav a 2 s  .c o m
    Class<?> javaType = null;
    String name = null;
    Class<?> declaringClass = null;

    if (accessibleObject instanceof Field) {
        Field field = (Field) accessibleObject;
        javaType = field.getType();
        name = field.getName();
        declaringClass = field.getDeclaringClass();
    } else if (accessibleObject instanceof Method) {
        Method method = (Method) accessibleObject;

        javaType = method.getReturnType();
        if (javaType.equals(Void.TYPE)) {
            return;
        }

        if (method.getName().startsWith("get")) {
            name = StringUtils.uncapitalize(method.getName().substring(3));
        } else if (method.getName().startsWith("is")) {
            name = StringUtils.uncapitalize(method.getName().substring(2));
        } else {
            name = method.getName();
        }

        declaringClass = method.getDeclaringClass();
    }

    ModelType modelType = null;
    if (model.isAutodetectTypes()) {
        for (ModelType mt : ModelType.values()) {
            if (mt.supports(javaType)) {
                modelType = mt;
                break;
            }
        }
    } else {
        modelType = ModelType.AUTO;
    }

    ModelFieldBean modelFieldBean = null;

    ModelField modelFieldAnnotation = accessibleObject.getAnnotation(ModelField.class);
    if (modelFieldAnnotation != null) {

        if (StringUtils.hasText(modelFieldAnnotation.value())) {
            name = modelFieldAnnotation.value();
        }

        if (StringUtils.hasText(modelFieldAnnotation.customType())) {
            modelFieldBean = new ModelFieldBean(name, modelFieldAnnotation.customType());
        } else {
            ModelType type = null;
            if (modelFieldAnnotation.type() != ModelType.NOT_SPECIFIED) {
                type = modelFieldAnnotation.type();
            } else {
                type = modelType;
            }

            modelFieldBean = new ModelFieldBean(name, type);
        }

        updateModelFieldBean(modelFieldBean, modelFieldAnnotation);
        model.addField(modelFieldBean);
    } else {
        if (modelType != null) {
            modelFieldBean = new ModelFieldBean(name, modelType);
            model.addField(modelFieldBean);
        }
    }

    ModelId modelIdAnnotation = accessibleObject.getAnnotation(ModelId.class);
    if (modelIdAnnotation != null) {
        model.setIdProperty(name);
    }

    ModelClientId modelClientId = accessibleObject.getAnnotation(ModelClientId.class);
    if (modelClientId != null) {
        model.setClientIdProperty(name);
        model.setClientIdPropertyAddToWriter(modelClientId.configureWriter());
    }

    ModelVersion modelVersion = accessibleObject.getAnnotation(ModelVersion.class);
    if (modelVersion != null) {
        model.setVersionProperty(name);
    }

    ModelAssociation modelAssociationAnnotation = accessibleObject.getAnnotation(ModelAssociation.class);
    if (modelAssociationAnnotation != null) {
        model.addAssociation(AbstractAssociation.createAssociation(modelAssociationAnnotation, model, javaType,
                declaringClass, name));
    }

    if (modelFieldBean != null && outputConfig.getIncludeValidation() != IncludeValidation.NONE) {

        Set<ModelValidation> modelValidationAnnotations = AnnotationUtils
                .getRepeatableAnnotation(accessibleObject, ModelValidations.class, ModelValidation.class);
        if (!modelValidationAnnotations.isEmpty()) {
            for (ModelValidation modelValidationAnnotation : modelValidationAnnotations) {
                AbstractValidation modelValidation = AbstractValidation.createValidation(name,
                        modelValidationAnnotation, outputConfig.getIncludeValidation());
                if (modelValidation != null) {
                    model.addValidation(modelValidation);
                }
            }
        } else {
            Annotation[] fieldAnnotations = accessibleObject.getAnnotations();

            for (Annotation fieldAnnotation : fieldAnnotations) {
                AbstractValidation.addValidationToModel(model, modelFieldBean, fieldAnnotation,
                        outputConfig.getIncludeValidation());
            }

            if (accessibleObject instanceof Field) {
                PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(declaringClass, name);
                if (pd != null && pd.getReadMethod() != null) {
                    for (Annotation readMethodAnnotation : pd.getReadMethod().getAnnotations()) {
                        AbstractValidation.addValidationToModel(model, modelFieldBean, readMethodAnnotation,
                                outputConfig.getIncludeValidation());
                    }
                }
            }
        }
    }

}

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

private String getToolTipTextFromAnnotation() {
    Class propertyContainerType = getBeanPropertyType().getContainerType();
    String propertyIdRelativeToContainerType = getBeanPropertyType().getId();
    PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(propertyContainerType,
            propertyIdRelativeToContainerType);
    Method method = descriptor.getReadMethod();
    ToolTip toolTipAnnotation = method.getAnnotation(ToolTip.class);
    if (toolTipAnnotation == null) {
        return null;
    } else {// ww w.j ava2s  .com
        return toolTipAnnotation.value();
    }
}

From source file:org.hellojavaer.poi.excel.utils.ExcelUtils.java

private static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName) {
    return BeanUtils.getPropertyDescriptor(clazz, propertyName);
}

From source file:com.nortal.petit.beanmapper.BeanMappingFactoryImpl.java

@SuppressWarnings("unchecked")
private <B> void initProperty(Map<String, Property<B, Object>> props, List<Property<B, Object>> idProps,
        String name, Class<B> type) {
    PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(type, name);

    if (!isPropertyReadableAndWritable(pd)) {
        return;//from   w w w. j  a v a2 s  .co m
    }

    List<Annotation> ans = BeanMappingReflectionUtils.readAnnotations(type, pd.getName());

    if (BeanMappingReflectionUtils.getAnnotation(ans, Transient.class) != null) {
        return;
    }

    Column column = BeanMappingReflectionUtils.getAnnotation(ans, Column.class);

    ReflectionProperty<B, Object> prop = new ReflectionProperty<B, Object>(name,
            (Class<Object>) pd.getPropertyType(), inferColumn(name, column), pd.getWriteMethod(),
            pd.getReadMethod());

    if (column != null) {
        prop.readOnly(!column.insertable());
    }

    if (BeanMappingReflectionUtils.getAnnotation(ans, Id.class) != null) {
        idProps.add(prop);
    }

    if (useAdditionalConfiguration()) {
        prop.getConfiguration().setAnnotations(ans);
        if (Collection.class.isAssignableFrom(pd.getPropertyType())) {
            prop.getConfiguration().setCollectionTypeArguments(
                    ((ParameterizedType) pd.getReadMethod().getGenericReturnType()).getActualTypeArguments());
        }
    }

    if (BeanMappingReflectionUtils.getAnnotation(ans, Embedded.class) != null) {
        props.putAll(getCompositeProperties(prop, ans));
    } else {
        props.put(prop.name(), prop);
    }
}

From source file:com.xyz.util.ReflectionUtil.java

/**
 * ?clazzproperty.// w  w w  . j  a  v a  2 s .  c  o m
 * 
 * @param value ?
 * @param clazz ???Class
 * @param propertyName ???Class.
 */
public static Object convertValue(Object value, Class<?> clazz, String propertyName) {
    try {
        Class<?> toType = BeanUtils.getPropertyDescriptor(clazz, propertyName).getPropertyType();
        DateConverter dc = new DateConverter();
        dc.setUseLocaleFormat(true);
        dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
        ConvertUtils.register(dc, Date.class);
        //String???''
        if (toType.equals(String.class))
            value = "'" + value + "'";
        return ConvertUtils.convert(value.toString(), toType);

    } catch (Exception e) {
        throw convertToUncheckedException(e);
    }
}

From source file:grails.util.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
 *///from   www .jav  a2s  .  c om
public static Class<?> getPropertyType(Class<?> clazz, String propertyName) {
    if (clazz == null || !StringUtils.hasText(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;
    }
}