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:edu.northwestern.bioinformatics.studycalendar.web.admin.AdministerUserCommand.java

private void copyBoundProperties(User src, User dst) {
    BeanWrapper srcW = new BeanWrapperImpl(src);
    BeanWrapper dstW = new BeanWrapperImpl(dst);

    for (PropertyDescriptor srcProp : srcW.getPropertyDescriptors()) {
        if (srcProp.getReadMethod() == null || srcProp.getWriteMethod() == null) {
            continue;
        }// w  w  w  .j a  v  a2  s . c o  m
        Object srcValue = srcW.getPropertyValue(srcProp.getName());
        if (srcValue != null) {
            dstW.setPropertyValue(srcProp.getName(), srcValue);
        }
    }
}

From source file:org.jaffa.soa.dataaccess.TransformerUtils.java

/**
 * Display the properties of this JavaBean in XML format.
 *
 * @param source Javabean who's contents should be printed
 * @return XML formatted string of this beans properties and their values
 *//*from w w w.  j a v a2  s.  c o  m*/
public static String printXMLGraph(Object source) {

    StringBuffer out = new StringBuffer();
    out.append("<" + source.getClass().getSimpleName() + ">");

    try {
        BeanInfo sInfo = Introspector.getBeanInfo(source.getClass());
        PropertyDescriptor[] sDescriptors = sInfo.getPropertyDescriptors();
        if (sDescriptors != null && sDescriptors.length != 0) {
            for (int i = 0; i < sDescriptors.length; i++) {
                PropertyDescriptor sDesc = sDescriptors[i];
                Method sm = sDesc.getReadMethod();
                if (sm != null && sDesc.getWriteMethod() != null) {
                    if (!sm.isAccessible())
                        sm.setAccessible(true);
                    Object sValue = sm.invoke(source, (Object[]) null);

                    out.append("<" + sDesc.getName() + ">");

                    if (sValue != null && !sm.getReturnType().isArray()
                            && !GraphDataObject.class.isAssignableFrom(sValue.getClass())) {
                        out.append(sValue.toString().trim());
                    }
                    out.append("</" + sDesc.getName() + ">");
                }
            }
        }
    } catch (IllegalAccessException e) {
        TransformException me = new TransformException(TransformException.ACCESS_ERROR, "???", e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        //throw me;
    } catch (InvocationTargetException e) {
        TransformException me = new TransformException(TransformException.INVOCATION_ERROR, "???", e);
        log.error(me.getLocalizedMessage(), me.getCause());
        //throw me;
    } catch (IntrospectionException e) {
        TransformException me = new TransformException(TransformException.INTROSPECT_ERROR, "???",
                e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        //throw me;
    }
    out.append("</" + source.getClass().getSimpleName() + ">");
    return out.toString();
}

From source file:org.springframework.flex.core.io.SpringPropertyProxy.java

private boolean isWriteIgnored(Object instance, String propertyName) {
    PropertyAccessor accessor = PropertyProxyUtils.getPropertyAccessor(this.conversionService,
            this.useDirectFieldAccess, instance);
    if (!accessor.isWritableProperty(propertyName)) {
        return true;
    }/*from w w w  .j a v a  2 s.  c  om*/
    if (this.useDirectFieldAccess) {
        AmfIgnoreField ignoreField = accessor.getPropertyTypeDescriptor(propertyName)
                .getAnnotation(AmfIgnoreField.class);
        return ignoreField != null && ignoreField.onDeserialization();
    } else {
        PropertyDescriptor pd = ((BeanWrapper) accessor).getPropertyDescriptor(propertyName);
        return pd.getWriteMethod().getAnnotation(AmfIgnore.class) != null;
    }
}

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

protected void populateCachedReferenceEntities(final Class entityClass,
        final CachedReferenceEntity[] cachedEntities, final String[] propertyList, final Object[][] data)
        throws HibernateException {
    try {//from w w  w.  j av a  2 s. 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());
        }

        Map entityMapByCache = new HashMap<CachedReferenceEntity, Object>();
        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 && data[i][j] != null) {
                    setter.invoke(entityObject, new Object[] { data[i][j] });
                }
            }
            entityMapByCache.put(cachedEntities[i], entityObject);
            if (!useEjb)
                session.save(entityObject);
            else
                entityManager.persist(entityObject);
            cachedEntities[i].setEntity((ReferenceEntity) entityObject);
        }
    } catch (Exception e) {
        log.error(e);
        throw new HibernateException(e);
    }
}

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

protected void populateCachedCustomReferenceEntities(final Class entityClass,
        final CachedCustomReferenceEntity[] cachedEntities, final String[] propertyList, final Object[][] data)
        throws HibernateException {
    try {//from  w  ww . j  a  va  2 s .  c om
        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());
        }

        Map entityMapByCache = new HashMap<CachedCustomReferenceEntity, Object>();
        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 && data[i][j] != null) {
                    if (data[i][j] instanceof CachedCustomHierarchyReferenceEntity) {
                        setter.invoke(entityObject, new Object[] { entityMapByCache.get(data[i][j]) });
                    } else
                        setter.invoke(entityObject, new Object[] { data[i][j] });
                }
            }
            entityMapByCache.put(cachedEntities[i], entityObject);
            if (!useEjb)
                session.save(entityObject);
            else
                entityManager.persist(entityObject);
            cachedEntities[i].setEntity((CustomReferenceEntity) entityObject);
        }
    } catch (Exception e) {
        log.error(ExceptionUtils.getStackTrace(e));
        throw new HibernateException(e);
    }
}

From source file:org.jaffa.soa.dataaccess.TransformerUtils.java

static void setProperty(PropertyDescriptor pd, Object value, Object source)
        throws IllegalAccessException, InvocationTargetException, TransformException {
    if (pd != null && pd.getWriteMethod() != null) {
        Method m = pd.getWriteMethod();
        if (!m.isAccessible())
            m.setAccessible(true);/*from   w ww .  j  a v  a  2s . c o m*/
        Class tClass = m.getParameterTypes()[0];
        if (value == null || tClass.isAssignableFrom(value.getClass())) {
            m.invoke(source, new Object[] { value });
            if (log.isDebugEnabled())
                log.debug("Set property '" + pd.getName() + '=' + value + "' on object '"
                        + source.getClass().getName() + '\'');
        } else if (DataTypeMapper.instance().isMappable(value.getClass(), tClass)) {
            // See if there is a datatype mapper for these classes
            value = DataTypeMapper.instance().map(value, tClass);
            m.invoke(source, new Object[] { value });
            if (log.isDebugEnabled())
                log.debug("Translate+Set property '" + pd.getName() + '=' + value + "' on object '"
                        + source.getClass().getName() + '\'');
        } else {
            // Data type mismatch
            throw new TransformException(TransformException.DATATYPE_MISMATCH,
                    source.getClass().getName() + '.' + m.getName(), tClass.getName(),
                    value.getClass().getName());
        }
    } else {
        TransformException me = new TransformException(TransformException.NO_SETTER, null,
                pd == null ? "???" : pd.getName(), source.getClass().getName());
        log.error(me.getLocalizedMessage());
        throw me;
    }
}

From source file:org.mule.module.netsuite.api.model.expression.filter.FilterExpressionBuilder.java

private SearchRecord createOrGetAttributeGroup(String joinName) throws Exception {
    PropertyDescriptor d = newDescriptor(joinName + "Join", target);
    SearchRecord attributeGroup = (SearchRecord) d.getReadMethod().invoke(target);
    if (attributeGroup == null) {
        attributeGroup = (SearchRecord) d.getPropertyType().newInstance();
        d.getWriteMethod().invoke(target, attributeGroup);
    }//from ww w .  j a  v  a  2s  .c  o m
    return attributeGroup;
}

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

public String toVCard(Object bean) {
    net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();
    calendar.getProperties().add(new ProdId("-//spliffy.org//iCal4j 1.0//EN"));
    calendar.getProperties().add(Version.VERSION_2_0);
    VEvent vevent = new VEvent();
    calendar.getComponents().add(vevent);

    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(calendar, bean, pd);
                }//from w w w  .  j  a  va  2s.c  o m
            }
        }
    }

    CalendarOutputter outputter = new CalendarOutputter();
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    try {
        outputter.output(calendar, bout);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (ValidationException ex) {
        throw new RuntimeException(ex);
    }
    return bout.toString();
}

From source file:org.androidtransfuse.processor.Merger.java

private <T extends Mergeable> T mergeMergeable(Class<? extends T> targetClass, T target, T source)
        throws MergerException {

    try {/*from w  w w . ja va  2 s  .  c  om*/

        BeanInfo beanInfo = Introspector.getBeanInfo(targetClass);

        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            Method getter = propertyDescriptor.getReadMethod();
            Method setter = propertyDescriptor.getWriteMethod();

            String propertyName = propertyDescriptor.getDisplayName();

            if (PropertyUtils.isWriteable(target, propertyName)) {

                //check for mergeCollection
                MergeCollection mergeCollection = findAnnotation(MergeCollection.class, getter, setter);
                if (Collection.class.isAssignableFrom(propertyDescriptor.getPropertyType())) {
                    PropertyUtils.setProperty(target, propertyName,
                            mergeList(mergeCollection, propertyName, target, source));
                }

                //check for merge
                Merge mergeAnnotation = findAnnotation(Merge.class, getter, setter);
                PropertyUtils.setProperty(target, propertyName,
                        mergeProperties(mergeAnnotation, propertyName, target, source));
            }
        }
    } catch (NoSuchMethodException e) {
        throw new MergerException("NoSuchMethodException while trying to merge", e);
    } catch (IntrospectionException e) {
        throw new MergerException("IntrospectionException while trying to merge", e);
    } catch (IllegalAccessException e) {
        throw new MergerException("IllegalAccessException while trying to merge", e);
    } catch (InvocationTargetException e) {
        throw new MergerException("InvocationTargetException while trying to merge", e);
    }

    return target;
}

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

public void toBean(Object bean, String icalText) {
    ByteArrayInputStream fin = null;
    try {/*from   ww w .ja v  a  2 s.co  m*/
        fin = new ByteArrayInputStream(icalText.getBytes("UTF-8"));
        CalendarBuilder builder = new CalendarBuilder();
        net.fortuna.ical4j.model.Calendar cal4jCalendar;
        try {
            cal4jCalendar = builder.build(fin);
        } catch (IOException ex) {
            throw new RuntimeException(icalText, ex);
        } catch (ParserException ex) {
            throw new RuntimeException(icalText, ex);
        }
        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.mapToBean(cal4jCalendar, bean, pd);
                    }
                }
            }
        }
    } catch (UnsupportedEncodingException ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeQuietly(fin);
    }
}