Example usage for org.springframework.beans BeanWrapper isReadableProperty

List of usage examples for org.springframework.beans BeanWrapper isReadableProperty

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper isReadableProperty.

Prototype

boolean isReadableProperty(String propertyName);

Source Link

Document

Determine whether the specified property is readable.

Usage

From source file:hu.petabyte.redflags.engine.util.MappingUtils.java

public static synchronized Object getDeepPropertyIfExists(BeanWrapper wrapper, String property) {
    if (wrapper.isReadableProperty(property)) {
        return wrapper.getPropertyValue(property);
    } else {/* w  ww . j a  v  a2s . c  om*/
        return null;
    }
}

From source file:org.jdal.vaadin.ui.FormUtils.java

/**
 * Add a link on primary and dependent ComboBoxes by property name. 
 * When selection changes on primary use propertyName to get a Collection and fill dependent ComboBox with it
 * @param primary ComboBox when selection changes
 * @param dependent ComboBox that are filled with collection   
 * @param propertyName the property name for get the collection from primary selected item
 * @param addNull if true, add a null as first combobox item
 *///from   w w  w.j  a v a2 s. c o  m
@SuppressWarnings("rawtypes")
public static void link(final ComboBox primary, final ComboBox dependent, final String propertyName,
        final boolean addNull) {

    primary.addValueChangeListener(new ValueChangeListener() {

        public void valueChange(ValueChangeEvent event) {
            Object selected = event.getProperty().getValue();
            if (selected != null) {
                BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(selected);
                if (wrapper.isReadableProperty(propertyName)) {
                    Collection items = (Collection) wrapper.getPropertyValue(propertyName);
                    dependent.removeAllItems();

                    if (addNull)
                        dependent.addItem(null);

                    for (Object item : items)
                        dependent.addItem(item);
                } else {
                    log.error("Can't write on propety '" + propertyName + "' of class: '" + selected.getClass()
                            + "'");
                }
            }

        }
    });
}

From source file:com.aw.support.beans.BeanUtils.java

public static Object copyProperties(Object source, Object target, String[] propertyNamesToIgnore,
        boolean ignoreProxy, boolean ignoreCollections) {
    List<String> propertyNamesToIgnoreList = propertyNamesToIgnore == null ? Collections.EMPTY_LIST
            : Arrays.asList(propertyNamesToIgnore);
    BeanWrapper sourceWrap = new BeanWrapperImpl(source);
    BeanWrapper targetWrap = new BeanWrapperImpl(target);
    for (PropertyDescriptor propDescriptor : sourceWrap.getPropertyDescriptors()) {
        String propName = propDescriptor.getName();
        //chequear que no esta en la lista a ignorar
        if (propertyNamesToIgnoreList.contains(propName))
            continue;
        //chequear que se puede leer
        if (!sourceWrap.isReadableProperty(propName))
            continue;
        //chequear que se puede escribir
        if (!targetWrap.isWritableProperty(propName))
            continue;

        Object sourceValue = sourceWrap.getPropertyValue(propName);

        //chequear que objeto no es un proxy
        if (ignoreProxy && sourceValue != null && Proxy.isProxyClass(sourceValue.getClass()))
            continue;

        //chequear que objeto no una collection
        if (ignoreCollections && sourceValue instanceof Collection)
            continue;

        targetWrap.setPropertyValue(propName, sourceValue);
    }/*from   w  ww .j a va2s .c o  m*/
    return target;
}

From source file:com.aw.support.collection.ListUtils.java

private static Object getPropertyValue(BeanWrapper wrap, String propertyNameValue,
        boolean assumeNullValueOnError) {
    Object value;/*from   w w w  .  j a  v a2  s.co m*/
    if (assumeNullValueOnError)
        value = wrap.isReadableProperty(propertyNameValue) ? wrap.getPropertyValue(propertyNameValue) : null;
    else
        value = wrap.getPropertyValue(propertyNameValue);
    return value;
}

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

/**
 * <p>Looks for a property of the reference instance with a given name.</p>
 * <p>If found its value is returned. We follow the Java bean conventions with augmentation for groovy support
 * and static fields/properties. We will therefore match, in this order:
 * </p>/*from   w w w .  j  a v  a2  s.com*/
 * <ol>
 * <li>Standard public bean property (with getter or just public field, using normal introspection)
 * <li>Public static property with getter method
 * <li>Public static field
 * </ol>
 *
 * @return property value or null if no property found
 */
public static Object getPropertyOrStaticPropertyOrFieldValue(Object obj, String name) //throws BeansException
{
    BeanWrapper ref = new BeanWrapperImpl(obj);
    if (ref.isReadableProperty(name)) {
        return ref.getPropertyValue(name);
    } else {
        // Look for public fields
        if (isPublicField(obj, name)) {
            return getFieldValue(obj, name);
        }

        // Look for statics
        Class clazz = obj.getClass();
        if (isStaticProperty(clazz, name)) {
            return getStaticPropertyValue(clazz, name);
        } else {
            return null;
        }
    }
}

From source file:org.codehaus.griffon.commons.GriffonClassUtils.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  w w  w . j  a  v a 2 s .  co m*/
public static Class getPropertyType(Class clazz, String propertyName) {
    if (clazz == null || StringUtils.isBlank(propertyName))
        return null;

    try {
        BeanWrapper wrapper = new BeanWrapperImpl(clazz);
        if (wrapper.isReadableProperty(propertyName)) {
            return wrapper.getPropertyType(propertyName);
        } else {
            return null;
        }
    } catch (Exception e) {
        // if there are any errors in instantiating just return null for the moment
        return null;
    }
}

From source file:org.jdto.spring.BeanWrapperBeanModifier.java

/**
 * Read a property value using the property path by invoking a spring {@link BeanWrapper}
 * @param propertyPath// www.  ja v a2s.c  o m
 * @param instance
 * @return the property value found on the property path applied to the provided instance.
 */
@Override
public Object doReadPropertyValue(String propertyPath, Object instance) {
    BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(instance);

    //check if it's safe to write the property.
    if (!beanWrapper.isReadableProperty(propertyPath)) {
        return null;
    }

    return beanWrapper.getPropertyValue(propertyPath);
}

From source file:org.springjutsu.validation.rules.ValidationRulesContainer.java

/**
 * Read from exclude annotations to further 
 * populate exclude paths already parsed from XML.
 *///from w w w .  j  a v  a2  s  .c  om
@SuppressWarnings({ "unchecked", "rawtypes" })
private void initExcludePaths() {
    for (ValidationEntity entity : validationEntityMap.values()) {
        // no paths to check on an interface.
        if (entity.getValidationClass().isInterface()) {
            continue;
        }
        BeanWrapper entityWrapper = new BeanWrapperImpl(entity.getValidationClass());
        for (PropertyDescriptor descriptor : entityWrapper.getPropertyDescriptors()) {
            if (!entityWrapper.isReadableProperty(descriptor.getName())
                    || !entityWrapper.isWritableProperty(descriptor.getName())) {
                continue;
            }
            for (Class excludeAnnotation : excludeAnnotations) {
                try {
                    if (ReflectionUtils.findField(entity.getValidationClass(), descriptor.getName())
                            .getAnnotation(excludeAnnotation) != null) {
                        entity.getExcludedPaths().add(descriptor.getName());
                    }
                } catch (SecurityException se) {
                    throw new IllegalStateException("Unexpected error while checking for excluded properties",
                            se);
                }
            }
        }
    }
}

From source file:org.springjutsu.validation.rules.ValidationRulesContainer.java

/**
 * Read from include annotations to further 
 * populate include paths already parsed from XML.
 */// w w w  .  j a  v a2 s . co m
@SuppressWarnings({ "unchecked", "rawtypes" })
private void initIncludePaths() {
    for (ValidationEntity entity : validationEntityMap.values()) {
        // no paths to check on an interface.
        if (entity.getValidationClass().isInterface()) {
            continue;
        }
        BeanWrapper entityWrapper = new BeanWrapperImpl(entity.getValidationClass());
        for (PropertyDescriptor descriptor : entityWrapper.getPropertyDescriptors()) {
            if (!entityWrapper.isReadableProperty(descriptor.getName())
                    || !entityWrapper.isWritableProperty(descriptor.getName())) {
                continue;
            }
            for (Class includeAnnotation : includeAnnotations) {
                try {
                    if (ReflectionUtils.findField(entity.getValidationClass(), descriptor.getName())
                            .getAnnotation(includeAnnotation) != null) {
                        entity.getIncludedPaths().add(descriptor.getName());
                    }
                } catch (SecurityException se) {
                    throw new IllegalStateException("Unexpected error while checking for included properties",
                            se);
                }
            }
        }
    }
}

From source file:com.alibaba.citrus.service.form.impl.GroupImpl.java

/**
 * fields//from  www .  ja v  a2 s .  co m
 * <p>
 * <code>isValidated()</code><code>true</code>group
 * </p>
 */
public void mapTo(Object object) {
    if (isValidated() || object == null) {
        return;
    }

    if (log.isDebugEnabled()) {
        log.debug("Mapping properties to fields: group=\"{}\", object={}", getName(),
                ObjectUtil.identityToString(object));
    }

    BeanWrapper bean = new BeanWrapperImpl(object);
    getForm().getFormConfig().getPropertyEditorRegistrar().registerCustomEditors(bean);

    for (Field field : getFields()) {
        String propertyName = field.getFieldConfig().getPropertyName();

        if (bean.isReadableProperty(propertyName)) {
            Object propertyValue = bean.getPropertyValue(propertyName);
            Class<?> propertyType = bean.getPropertyType(propertyName);
            PropertyEditor editor = bean.findCustomEditor(propertyType, propertyName);

            if (editor == null) {
                editor = BeanUtils.findEditorByConvention(propertyType);
            }

            if (editor == null) {
                if (propertyType.isArray() || CollectionFactory.isApproximableCollectionType(propertyType)) {
                    field.setValues((String[]) bean.convertIfNecessary(propertyValue, String[].class));
                } else {
                    field.setValue(bean.convertIfNecessary(propertyValue, String.class));
                }
            } else {
                editor.setValue(propertyValue);
                field.setValue(editor.getAsText());
            }
        } else {
            log.debug("No readable property \"{}\" found in type {}", propertyName,
                    object.getClass().getName());
        }
    }
}