Example usage for java.beans PropertyDescriptor getReadMethod

List of usage examples for java.beans PropertyDescriptor getReadMethod

Introduction

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

Prototype

public synchronized Method getReadMethod() 

Source Link

Document

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

Usage

From source file:name.livitski.tools.persista.AbstractDAO.java

private Method accessor(String property) {
    Map<String, PropertyDescriptor> props = introspectProperties();
    PropertyDescriptor prop = props.get(property);
    if (null == prop)
        throw new IllegalArgumentException("Property \"" + property + "\" does not exist in " + entityClass);
    Method getter = prop.getReadMethod();
    if (null == getter)
        throw new IllegalArgumentException("Property \"" + property + "\" is not writable in " + entityClass);
    return getter;
}

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

static Object getProperty(PropertyDescriptor pd, Object source)
        throws IllegalAccessException, InvocationTargetException, TransformException {
    if (pd != null && pd.getReadMethod() != null) {
        Method m = pd.getReadMethod();
        if (!m.isAccessible())
            m.setAccessible(true);//from  w w w  .ja  va  2 s .co  m
        Object value = m.invoke(source, new Object[] {});
        if (log.isDebugEnabled())
            log.debug("Get property '" + pd.getName() + '=' + value + "' on object '"
                    + source.getClass().getName() + '\'');
        return value;
    } else {
        TransformException me = new TransformException(TransformException.NO_GETTER, null,
                pd == null ? "???" : pd.getName(), source.getClass().getName());
        log.error(me.getLocalizedMessage());
        throw me;
    }
}

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 va2  s .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:com.ettrema.httpclient.calsync.parse.CalDavBeanPropertyMapper.java

public void toBean(Object bean, String icalText) {
    ByteArrayInputStream fin = null;
    try {/*from  www.  ja v a 2s.  c  o  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);
    }
}

From source file:org.hopen.framework.rewrite.CachedIntrospectionResults.java

private PropertyDescriptor buildGenericTypeAwarePropertyDescriptor(Class beanClass, PropertyDescriptor pd) {
    try {/*from   www.j  a  v a2  s  .c  om*/
        return new GenericTypeAwarePropertyDescriptor(beanClass, pd.getName(), pd.getReadMethod(),
                pd.getWriteMethod(), pd.getPropertyEditorClass());
    } catch (IntrospectionException ex) {
        throw new FatalBeanException("Failed to re-introspect class [" + beanClass.getName() + "]", ex);
    }
}

From source file:org.apache.activemq.artemis.tests.integration.jms.connection.ConnectionFactorySerializationTest.java

private void populate(StringBuilder sb, BeanUtilsBean bean, ActiveMQConnectionFactory factory)
        throws IllegalAccessException, InvocationTargetException {
    PropertyDescriptor[] descriptors = bean.getPropertyUtils().getPropertyDescriptors(factory);
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getWriteMethod() != null && descriptor.getReadMethod() != null) {
            if (descriptor.getPropertyType() == String.class) {
                String value = RandomUtil.randomString();
                bean.setProperty(factory, descriptor.getName(), value);
                sb.append("&").append(descriptor.getName()).append("=").append(value);
            } else if (descriptor.getPropertyType() == int.class) {
                int value = RandomUtil.randomPositiveInt();
                bean.setProperty(factory, descriptor.getName(), value);
                sb.append("&").append(descriptor.getName()).append("=").append(value);
            } else if (descriptor.getPropertyType() == long.class) {
                long value = RandomUtil.randomPositiveLong();
                bean.setProperty(factory, descriptor.getName(), value);
                sb.append("&").append(descriptor.getName()).append("=").append(value);
            } else if (descriptor.getPropertyType() == double.class) {
                double value = RandomUtil.randomDouble();
                bean.setProperty(factory, descriptor.getName(), value);
                sb.append("&").append(descriptor.getName()).append("=").append(value);
            }/*from w ww  .java2s .c  om*/
        }
    }
}

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

/**
 * Initialize the mapping metadata for the given class.
 * @param mappedClass the mapped class//from  ww w  . j  a v  a 2s  .c  om
 */
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.datatorrent.stram.appdata.AppDataPushAgent.java

private JSONObject extractFields(Object o) {
    List<Field> fields;
    Map<String, Method> methods;

    if (cacheFields.containsKey(o.getClass())) {
        fields = cacheFields.get(o.getClass());
    } else {/*from  w ww . ja v  a2s  .  com*/
        fields = new ArrayList<Field>();

        for (Class<?> c = o.getClass(); c != Object.class; c = c.getSuperclass()) {
            Field[] declaredFields = c.getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                AutoMetric rfa = field.getAnnotation(AutoMetric.class);
                if (rfa != null) {
                    field.setAccessible(true);
                    try {
                        fields.add(field);
                    } catch (Exception ex) {
                        LOG.debug("Error extracting fields for app data: {}. Ignoring.", ex.getMessage());
                    }
                }
            }
        }
        cacheFields.put(o.getClass(), fields);
    }
    JSONObject result = new JSONObject();
    for (Field field : fields) {
        try {
            result.put(field.getName(), field.get(o));
        } catch (Exception ex) {
            // ignore
        }
    }
    if (cacheGetMethods.containsKey(o.getClass())) {
        methods = cacheGetMethods.get(o.getClass());
    } else {
        methods = new HashMap<String, Method>();
        try {
            BeanInfo info = Introspector.getBeanInfo(o.getClass());
            for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
                Method method = pd.getReadMethod();
                if (pd.getReadMethod() != null) {
                    AutoMetric rfa = method.getAnnotation(AutoMetric.class);
                    if (rfa != null) {
                        methods.put(pd.getName(), method);
                    }
                }
            }
        } catch (IntrospectionException ex) {
            // ignore
        }
        cacheGetMethods.put(o.getClass(), methods);
    }
    for (Map.Entry<String, Method> entry : methods.entrySet()) {
        try {
            result.put(entry.getKey(), entry.getValue().invoke(o));
        } catch (Exception ex) {
            // ignore
        }
    }
    return result;
}

From source file:com.twinsoft.convertigo.beans.core.MySimpleBeanInfo.java

protected PropertyDescriptor getPropertyDescriptor(String name) throws IntrospectionException {
    checkAdditionalProperties();/* w  ww. j a  va  2s.  c  o  m*/
    for (int i = 0; i < properties.length; i++) {
        PropertyDescriptor property = properties[i];
        if (name.equals(property.getName())) {
            PropertyDescriptor clone = new PropertyDescriptor(name, property.getReadMethod(),
                    property.getWriteMethod());
            clone.setDisplayName(property.getDisplayName());
            clone.setShortDescription(property.getShortDescription());
            clone.setPropertyEditorClass(property.getPropertyEditorClass());
            clone.setBound(property.isBound());
            clone.setConstrained(property.isConstrained());
            clone.setExpert(property.isExpert());
            clone.setHidden(property.isHidden());
            clone.setPreferred(property.isPreferred());
            for (String attributeName : Collections.list(property.attributeNames())) {
                clone.setValue(attributeName, property.getValue(attributeName));
            }
            return properties[i] = clone;
        }
    }
    return null;
}

From source file:edu.harvard.med.screensaver.model.AbstractEntity.java

@SuppressWarnings("unchecked")
public <P> P getPropertyValue(String propertyName, Class<P> propertyType) {
    try {//from  ww w .  j  a  v a  2s.com
        PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(this, propertyName);
        return (P) propertyDescriptor.getReadMethod().invoke(this);
    } catch (Exception e) {
        log.error(e);
        return null;
    }
}