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:com.gzj.tulip.jade.rowmapper.BeanPropertyRowMapper.java

/**
 * Initialize the mapping metadata for the given class.
 * /*from  w  w  w.  jav  a  2s .c  o  m*/
 * @param mappedClass the mapped class.
 */
protected void initialize() {
    this.mappedFields = new HashMap<String, PropertyDescriptor>();
    PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
    if (checkProperties) {
        mappedProperties = new HashSet<String>();
    }
    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor pd = pds[i];
        if (pd.getWriteMethod() != null) {
            if (checkProperties) {
                this.mappedProperties.add(pd.getName());
            }
            this.mappedFields.put(pd.getName().toLowerCase(), pd);
            for (String underscoredName : underscoreName(pd.getName())) {
                if (underscoredName != null && !pd.getName().toLowerCase().equals(underscoredName)) {
                    this.mappedFields.put(underscoredName, pd);
                }
            }
        }
    }
}

From source file:com.github.erchu.beancp.PropertyBindingSide.java

/**
 *
 * Creates binding to property from property information.
 *
 * @param propertyDescriptor property information used to create binding
 *///from www  . j  a  va2  s.  c  o m
public PropertyBindingSide(final PropertyDescriptor propertyDescriptor) {
    this._valueClass = propertyDescriptor.getPropertyType();
    this._readMethod = propertyDescriptor.getReadMethod();
    this._writeMethod = propertyDescriptor.getWriteMethod();
    this._name = propertyDescriptor.getName();
}

From source file:com.nortal.petit.orm.relation.RelationMapper.java

public RelationMapper(Class<T> target, Class<R> relation, String targetProperty, String relationProperty,
        String targetMapping, WherePart where) {
    Assert.notNull(target, "RelationMapper.construct: target is mandatory");
    Assert.notNull(relation, "RelationMapper.construct: relation is mandatory");
    Assert.isTrue(StringUtils.isNotEmpty(targetProperty) || StringUtils.isNotEmpty(relationProperty),
            "RelationMapper.construct: targetProperty or relationProperty is mandatory");

    this.relationClass = relation;

    this.targetMapper = BeanMappings.get(target);
    this.relationMapper = BeanMappings.get(relation);

    // Init target mapping property
    if (StringUtils.isEmpty(targetProperty)) {
        this.targetProperty = targetMapper.id();
    } else {/* w  w  w .j  av  a 2  s .com*/
        this.targetProperty = targetMapper.props().get(targetProperty);
    }
    Assert.notNull(this.targetProperty, "RelationMapper.construct: targetProperty is mandatory");

    targetId = new PropertyFunction<T, Object>(this.targetProperty);

    // Init target mapping property
    if (StringUtils.isEmpty(relationProperty)) {
        this.relationProperty = relationMapper.id();
    } else {
        this.relationProperty = relationMapper.props().get(relationProperty);
    }
    Assert.notNull(this.relationProperty, "RelationMapper.construct: relationProperty is mandatory");

    relationId = new PropertyFunction<R, Object>(this.relationProperty);

    if (StringUtils.isNotEmpty(targetMapping)) {
        PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(target, targetMapping);
        this.associateMethod = pd.getWriteMethod();
    }

    // Mapping conditions
    this.where = where;
}

From source file:at.molindo.notify.model.BeanParams.java

private PropertyDescriptor getDescriptor(String propertyName) {
    try {//from ww  w .ja  v a2  s  . c om
        PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(_bean, propertyName);
        return pd != null && pd.getWriteMethod() != null && pd.getReadMethod() != null ? pd : null;
    } catch (IllegalAccessException e) {
        throw new NotifyRuntimeException(
                "failed to get PropertyDescriptor for property " + propertyName + " from bean " + _bean, e);
    } catch (InvocationTargetException e) {
        throw new NotifyRuntimeException(
                "failed to get PropertyDescriptor for property " + propertyName + " from bean " + _bean, e);
    } catch (NoSuchMethodException e) {
        // ignore
        return null;
    }
}

From source file:com.clican.pluto.common.support.spring.BeanPropertyRowMapper.java

/**
 * Initialize the mapping metadata for the given class.
 * //w w  w .j  ava 2 s  .  c  o m
 * @param mappedClass
 *            the mapped class.
 */
protected void initialize(Class<?> mappedClass) {
    this.mappedClass = mappedClass;
    this.mappedFields = new HashMap<String, PropertyDescriptor>();
    this.mappedProperties = new HashSet<String>();
    PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor pd = pds[i];
        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());
        }
    }
}

From source file:com.ettrema.httpclient.calsync.parse.BeanPropertyMapper.java

public String toVCard(Object bean) {
    VCard card = new VCardImpl();
    card.setBegin(new BeginType());
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean);
    for (PropertyDescriptor pd : pds) {
        if (pd.getReadMethod() != null && pd.getWriteMethod() != null) {
            Method read = pd.getReadMethod();
            Annotation[] annotations = read.getAnnotations();
            for (Annotation anno : annotations) {
                Mapper mapper = mapOfMappers.get(anno.annotationType());
                if (mapper != null) {
                    mapper.mapToCard(card, bean, pd);
                }//from   w w w  . j  a  v  a  2s.com
            }
        }
    }
    FormattedNameFeature fname = card.getFormattedName();
    if (fname == null) {
        NameFeature nameFeature = card.getName();
        if (nameFeature != null) {
            String formattedName = nameFeature.getGivenName() + " " + nameFeature.getFamilyName();
            fname = new FormattedNameType(formattedName);
            card.setFormattedName(fname);
        }
    }

    card.setEnd(new EndType());

    VCardWriter writer = new VCardWriter();
    writer.setVCard(card);
    String text = writer.buildVCardString();
    return text;
}

From source file:org.vmguys.reflect.BeanSearchUtil.java

/** Returns set method name.
* @param name name of property./*from  www  .  j  ava 2s .c o m*/
* @param caseInsensitiveSearch if <code>true</code>, do a case-insenstive search
*                        for name.
* @return set method  or <code>null</code> if not found.
*/
public Method getWriteMethod(String name, boolean caseInsensitiveSearch) {
    if (name.startsWith("set") && name.length() > 3)
        name = name.substring(3);
    PropertyDescriptor p = getPropertyDescriptor(name, caseInsensitiveSearch);
    return (p == null ? null : p.getWriteMethod());
}

From source file:org.onebusaway.siri.core.filters.ElementPathModuleDeliveryFilter.java

private void applyFilter(Object parent, PropertyDescriptor propertyDescriptor) {

    Method readMethod = propertyDescriptor.getReadMethod();
    Method writeMethod = propertyDescriptor.getWriteMethod();

    if (writeMethod != null) {

        Class<?>[] parameterTypes = writeMethod.getParameterTypes();
        Class<?> parameterType = parameterTypes[0];
        Object value = convertValue(_value, parameterType);
        PropertyConverterSupport.setTargetPropertyValue(parent, writeMethod, value);
    } else if (Collection.class.isAssignableFrom(propertyDescriptor.getPropertyType())
            && (_value == null || _value instanceof Collection)) {
        Collection<?> values = (Collection<?>) _value;
        if (values == null)
            values = Collections.emptyList();
        PropertyConverterSupport.setTargetPropertyValues(parent, readMethod, values);
    } else {/*from   www. jav a2 s .  co m*/
        throw new SiriException(
                "no write method for property \"" + propertyDescriptor.getName() + " on " + parent);
    }
}

From source file:com.netspective.medigy.model.data.EntitySeedDataPopulator.java

protected void populateEntity(final Session session, final Class entityClass, final String[] propertyList,
        final Object[][] data) throws HibernateException {
    try {/*from   w  ww .j a va2s.  c  o  m*/
        final Hashtable pdsByName = new Hashtable();
        final BeanInfo beanInfo = Introspector.getBeanInfo(entityClass);
        final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < descriptors.length; i++) {
            final PropertyDescriptor descriptor = descriptors[i];
            if (descriptor.getWriteMethod() != null)
                pdsByName.put(descriptor.getName(), descriptor.getWriteMethod());
        }

        for (int i = 0; i < data.length; i++) {
            final Object entityObject = entityClass.newInstance();
            for (int j = 0; j < propertyList.length; j++) {
                final Method setter = (Method) pdsByName.get(propertyList[j]);
                if (setter != null)
                    setter.invoke(entityObject, new Object[] { data[i][j] });
            }
            session.save(entityObject);
        }
    } catch (Exception e) {
        log.error(e);
        throw new HibernateException(e);
    }
}

From source file:org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImpl.java

private void setValueFor(PropertyDescriptor pd, Object target, Object value) {
    Method m = pd.getWriteMethod();
    Object[] args = { value };/* w ww  .j  ava  2s.  c o  m*/
    if (m != null) {
        try {
            /**
             * MBAIRD: it is safe to call getParameterTypes()[0] because this is
             * the "set" method and it needs to take one parameter only.
             * we need to be able to set values to null. We can only set something to null if
             * the type is not a primitive (assignable from Object).
             */
            if ((value != null) || !m.getParameterTypes()[0].isPrimitive()) {
                m.invoke(ProxyHelper.getRealObject(target), args);
            }
        } catch (Throwable e) {
            logProblem(pd, target, value, "Can't set value on given object.");
            throw new MetadataException(
                    "Error invoking method:" + m.getName() + " in object:" + target.getClass().getName(), e);
        }
    } else {
        throw new MetadataException("Can't get WriteMethod for property:" + pd.getName() + " in object:"
                + target.getClass().getName());
    }
}