Example usage for java.beans PropertyDescriptor getWriteMethod

List of usage examples for java.beans PropertyDescriptor getWriteMethod

Introduction

In this page you can find the example usage for java.beans PropertyDescriptor getWriteMethod.

Prototype

public synchronized Method getWriteMethod() 

Source Link

Document

Gets the method that should be used to write the property value.

Usage

From source file:org.apache.james.container.spring.lifecycle.osgi.AbstractOSGIAnnotationBeanPostProcessor.java

private A hasAnnotatedProperty(PropertyDescriptor propertyDescriptor) {
    Method setter = propertyDescriptor.getWriteMethod();
    return setter != null ? AnnotationUtils.getAnnotation(setter, getAnnotation()) : null;
}

From source file:ch.flashcard.HibernateDetachUtility.java

private static void nullOutFieldsByAccessors(Object value, Map<Integer, Object> checkedObjects,
        Map<Integer, List<Object>> checkedObjectCollisionMap, int depth, SerializationType serializationType)
        throws Exception {
    // Null out any collections that aren't loaded
    BeanInfo bi = Introspector.getBeanInfo(value.getClass(), Object.class);

    PropertyDescriptor[] pds = bi.getPropertyDescriptors();
    for (PropertyDescriptor pd : pds) {
        Object propertyValue = null;
        try {//from  w  ww. j  av  a 2 s  .com
            propertyValue = pd.getReadMethod().invoke(value);
        } catch (Throwable lie) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Couldn't load: " + pd.getName() + " off of " + value.getClass().getSimpleName(),
                        lie);
            }
        }

        if (!Hibernate.isInitialized(propertyValue)) {
            try {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Nulling out: " + pd.getName() + " off of " + value.getClass().getSimpleName());
                }

                Method writeMethod = pd.getWriteMethod();
                if ((writeMethod != null) && (writeMethod.getAnnotation(XmlTransient.class) == null)) {
                    pd.getWriteMethod().invoke(value, new Object[] { null });
                } else {
                    nullOutField(value, pd.getName());
                }
            } catch (Exception lie) {
                LOG.debug("Couldn't null out: " + pd.getName() + " off of " + value.getClass().getSimpleName()
                        + " trying field access", lie);
                nullOutField(value, pd.getName());
            }
        } else {
            if ((propertyValue instanceof Collection) || ((propertyValue != null)
                    && propertyValue.getClass().getName().startsWith("org.rhq.core.domain"))) {
                nullOutUninitializedFields(propertyValue, checkedObjects, checkedObjectCollisionMap, depth + 1,
                        serializationType);
            }
        }
    }
}

From source file:org.carewebframework.ui.xml.ZK2XML.java

/**
 * Adds the root component to the XML document at the current level along with all bean
 * properties that return String or primitive types. Then, recurses over all of the root
 * component's children./*from  w  w w.jav  a 2s  . c  o m*/
 * 
 * @param root The root component.
 * @param parent The parent XML node.
 */
private void toXML(Component root, Node parent) {
    TreeMap<String, String> properties = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    Class<?> clazz = root.getClass();
    ComponentDefinition def = root.getDefinition();
    String cmpname = def.getName();

    if (def.getImplementationClass() != clazz) {
        properties.put("use", clazz.getName());
    }

    if (def.getApply() != null) {
        properties.put("apply", def.getApply());
    }

    Node child = doc.createElement(cmpname);
    parent.appendChild(child);

    for (PropertyDescriptor propDx : PropertyUtils.getPropertyDescriptors(root)) {
        Method getter = propDx.getReadMethod();
        Method setter = propDx.getWriteMethod();
        String name = propDx.getName();

        if (getter != null && setter != null && !isExcluded(name, cmpname, null)
                && !setter.isAnnotationPresent(Deprecated.class)
                && (getter.getReturnType() == String.class || getter.getReturnType().isPrimitive())) {
            try {
                Object raw = getter.invoke(root);
                String value = raw == null ? null : raw.toString();

                if (StringUtils.isEmpty(value) || ("id".equals(name) && value.startsWith("z_"))
                        || isExcluded(name, cmpname, value)) {
                    continue;
                }

                properties.put(name, value.toString());
            } catch (Exception e) {
            }
        }
    }

    for (Entry<String, String> entry : properties.entrySet()) {
        Attr attr = doc.createAttribute(entry.getKey());
        child.getAttributes().setNamedItem(attr);
        attr.setValue(entry.getValue());
    }

    properties = null;

    for (Component cmp : root.getChildren()) {
        toXML(cmp, child);
    }
}

From source file:org.springframework.faces.mvc.bind.ReverseDataBinder.java

/**
 * Determine if a property contains both read and write methods.
 * @param descriptor The property descriptor
 * @return <tt>true</tt> if the property is mutable
 *///from w ww. ja  v a 2s  .com
private boolean isMutableProperty(PropertyDescriptor descriptor) {
    return descriptor.getReadMethod() != null && descriptor.getWriteMethod() != null;
}

From source file:com.dexcoder.dal.spring.mapper.JdbcRowMapper.java

/**
 * Initialize the mapping metadata for the given class.
 * @param mappedClass the mapped class//from   w w w .  j  av  a 2s  . c o m
 */
protected void initialize(Class<T> mappedClass) {
    this.mappedClass = mappedClass;
    this.mappedFields = new HashMap<String, PropertyDescriptor>();
    this.mappedProperties = new HashSet<String>();
    PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
    for (PropertyDescriptor pd : pds) {
        if (pd.getWriteMethod() != null) {

            Method readMethod = pd.getReadMethod();
            if (readMethod != null) {
                Column aColumn = readMethod.getAnnotation(Column.class);
                if (aColumn != null) {
                    String name = NameUtils.getLegalName(aColumn.value());
                    this.mappedFields.put(lowerCaseName(name), pd);
                }
            }

            this.mappedFields.put(lowerCaseName(pd.getName()), pd);
            String underscoredName = underscoreName(pd.getName());
            if (!lowerCaseName(pd.getName()).equals(underscoredName)) {
                this.mappedFields.put(underscoredName, pd);
            }
            this.mappedProperties.add(pd.getName());
        }
    }
}

From source file:com.impetus.kundera.metadata.processor.AbstractEntityFieldProcessor.java

/**
* Populates @Id accesser methods like, getId and setId of clazz to
* metadata.//  w w  w .  j ava  2  s.co m
* 
* @param metadata
*            the metadata
* @param clazz
*            the clazz
* @param f
*            the f
*/
protected final void populateIdAccessorMethods(EntityMetadata metadata, Class<?> clazz, Field f) {
    try {
        BeanInfo info = Introspector.getBeanInfo(clazz);

        for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) {
            if (descriptor.getName().equals(f.getName())) {
                metadata.setReadIdentifierMethod(descriptor.getReadMethod());
                metadata.setWriteIdentifierMethod(descriptor.getWriteMethod());
                return;
            }
        }
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }
}

From source file:springfox.documentation.spring.web.readers.parameter.ModelAttributeParameterExpander.java

private Set<String> getBeanPropertyNames(final Class<?> clazz) {

    try {/*from   ww w  .  j a  va 2s. c o  m*/
        Set<String> beanProps = new HashSet<String>();
        PropertyDescriptor[] propDescriptors = getBeanInfo(clazz).getPropertyDescriptors();

        for (PropertyDescriptor propDescriptor : propDescriptors) {

            if (propDescriptor.getReadMethod() != null && propDescriptor.getWriteMethod() != null) {
                beanProps.add(propDescriptor.getName());
            }
        }

        return beanProps;

    } catch (IntrospectionException e) {
        LOG.warn(String.format("Failed to get bean properties on (%s)", clazz), e);
    }
    return newHashSet();
}

From source file:org.lambdamatic.internal.elasticsearch.codec.DocumentCodec.java

/**
 * Sets the given {@code documentId} value to the given {@code domainObject} using the setter for
 * the property annotated with the {@link DocumentIdField} annotation.
 * //from w w w.  jav  a2 s.co  m
 * @param domainObject the instance of DomainType on which to set the {@code id} property
 * @param documentId the value of the document id.
 */
public void setDomainObjectId(final T domainObject, final String documentId) {
    final Field idField = getIdField(domainObject.getClass());
    final PropertyDescriptor idPropertyDescriptor = getIdPropertyDescriptor(domainObject, idField);
    try {
        idPropertyDescriptor.getWriteMethod().invoke(domainObject, convertValue(documentId, idField.getType()));
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new CodecException("Failed to set value '" + documentId + "' (" + documentId.getClass().getName()
                + ") using method '" + idPropertyDescriptor.getWriteMethod().toString() + "'");
    }
}

From source file:net.sourceforge.vulcan.spring.SpringBeanXmlEncoder.java

void encodeBean(Element root, String beanName, Object bean) {
    final BeanWrapper bw = new BeanWrapperImpl(bean);

    final PropertyDescriptor[] pds = bw.getPropertyDescriptors();
    final Element beanNode = new Element("bean");

    root.addContent(beanNode);//from   w ww  .j  a v  a2  s  . c  om

    if (beanName != null) {
        beanNode.setAttribute("name", beanName);
    }

    if (factoryExpert.needsFactory(bean)) {
        encodeBeanByFactory(beanNode, bean);
    } else {
        beanNode.setAttribute("class", bean.getClass().getName());
    }

    for (PropertyDescriptor pd : pds) {
        if (pd.getWriteMethod() == null)
            continue;

        final Method readMethod = pd.getReadMethod();

        if (readMethod == null)
            continue;

        if (readMethod.getAnnotation(Transient.class) != null) {
            continue;
        }
        final String name = pd.getName();

        final Object value = bw.getPropertyValue(name);
        if (value == null)
            continue;

        final Element propertyNode = new Element("property");
        propertyNode.setAttribute("name", name);
        beanNode.addContent(propertyNode);

        encodeObject(propertyNode, value);
    }
}

From source file:com.duowan.common.spring.jdbc.BeanPropertyRowMapper.java

/**
 * Initialize the mapping metadata for the given class.
 * @param mappedClass the mapped class.//w w  w .ja v a2s.  c  o  m
 */
protected void initialize(Class<T> mappedClass) {
    this.mappedClass = mappedClass;
    this.mappedFields = new HashMap<String, PropertyDescriptor>();
    this.mappedProperties = new HashSet<String>();
    PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
    for (PropertyDescriptor pd : pds) {
        if (pd.getWriteMethod() != null) {
            this.mappedFields.put(pd.getName().toLowerCase(), pd);
            String underscoredName = underscoreName(pd.getName());
            if (!pd.getName().toLowerCase().equals(underscoredName)) {
                this.mappedFields.put(underscoredName, pd);
            }
            this.mappedProperties.add(pd.getName());
        }
    }
}