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:org.eclipse.jubula.rc.common.util.PropertyUtil.java

/**
 * Returns a sorted map consisting of the bean properties of a component
 * //from w w w.j  av  a  2  s  .c  om
 * @param currComp 
 *              the component
 * @return the sorted map of properties
 */
public static Map getMapOfComponentProperties(final Object currComp) {
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(currComp);
    Map componentProperties = new TreeMap();
    for (int i = 0; i < propertyDescriptors.length; i++) {
        PropertyDescriptor pd = propertyDescriptors[i];
        try {
            Method readMethod = pd.getReadMethod();
            if (readMethod != null) {
                Object obj = readMethod.invoke(currComp, new Object[] {});
                String value = String.valueOf(obj);
                if (value.length() > 200) {
                    value = StringUtils.substring(value, 0, 200);
                }
                if (obj instanceof Character) {
                    Character c = (Character) obj;
                    if (c.charValue() == 0) {
                        value = ""; //$NON-NLS-1$
                    }
                }
                componentProperties.put(pd.getName(), value);
            } else {
                componentProperties.put(pd.getName(), "This property is not readable"); //$NON-NLS-1$
            }
        } catch (IllegalArgumentException e) {
            componentProperties.put(pd.getName(), "Error"); //$NON-NLS-1$
        } catch (IllegalAccessException e) {
            componentProperties.put(pd.getName(), "Error accessing this property"); //$NON-NLS-1$
        } catch (InvocationTargetException e) {
            componentProperties.put(pd.getName(), "Error reading this property"); //$NON-NLS-1$
        }
    }
    return componentProperties;
}

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

public static int countPropertyFilled(Object bean) {
    BeanWrapper wrap = new BeanWrapperImpl(bean);
    int count = 0;
    for (PropertyDescriptor descriptor : wrap.getPropertyDescriptors()) {
        if (descriptor.getReadMethod() == null || descriptor.getWriteMethod() == null)
            continue;
        Object value = wrap.getPropertyValue(descriptor.getName());
        if (value instanceof String) {
            if (StringUtils.hasText((String) value))
                count++;//from   w ww . jav  a 2  s.  com
        } else if (value != null)
            count++;
    }
    return count;
}

From source file:org.atlasapi.output.rdf.RdfIntrospector.java

/**
 * Currently a property is considered a relation if it is a persistence 
 * relation.  This may need to be updated, but is convenient for now.
 *///from   w  w  w.j a  v  a2s.com
public static RdfProperty getProperty(PropertyDescriptor property) {
    Method readMethod = property.getReadMethod();

    return readMethod.getAnnotation(RdfProperty.class);
}

From source file:org.jdal.text.FormatUtils.java

public static <A extends Annotation> A getAnnotation(PropertyDescriptor pd, Class<A> annotationType) {
    A annotation = AnnotationUtils.getAnnotation(pd.getReadMethod(), annotationType);
    if (annotation != null)
        return annotation;

    Field field = getDeclaredField(pd);

    if (field != null)
        annotation = AnnotationUtils.getAnnotation(field, annotationType);

    return annotation;
}

From source file:com.evolveum.midpoint.provisioning.ucf.api.UcfUtil.java

public static boolean hasAnnotation(PropertyDescriptor prop, Class<? extends Annotation> annotationClass) {
    Method readMethod = prop.getReadMethod();
    if (readMethod != null && readMethod.getAnnotation(annotationClass) != null) {
        return true;
    }//w  w  w.j  a  v  a  2  s  .  c  o m
    Method writeMethod = prop.getWriteMethod();
    if (writeMethod != null && writeMethod.getAnnotation(annotationClass) != null) {
        return true;
    }
    Class<?> propertyType = prop.getPropertyType();
    if (propertyType.isAnnotationPresent(annotationClass)) {
        return true;
    }
    return false;
}

From source file:org.apache.activemq.artemis.utils.uri.BeanSupport.java

public static String getData(List<String> ignored, Object... beans) throws Exception {
    StringBuilder sb = new StringBuilder();
    boolean empty = true;
    synchronized (beanUtils) {
        for (Object bean : beans) {
            if (bean != null) {
                PropertyDescriptor[] descriptors = beanUtils.getPropertyUtils().getPropertyDescriptors(bean);
                for (PropertyDescriptor descriptor : descriptors) {
                    if (descriptor.getReadMethod() != null && isWriteable(descriptor, ignored)) {
                        String value = beanUtils.getProperty(bean, descriptor.getName());
                        if (value != null) {
                            if (!empty) {
                                sb.append("&");
                            }/*ww w. java 2s  .c o  m*/
                            empty = false;
                            sb.append(descriptor.getName()).append("=").append(encodeURI(value));
                        }
                    }
                }
            }
        }
    }
    return sb.toString();
}

From source file:com.doculibre.constellio.utils.connector.ConnectorPropertyInheritanceResolver.java

private static String getStringPropertyValue(Object bean, String propertyName) {
    try {/*  www .j av  a 2s .  com*/
        PropertyDescriptor propertyDescriptor = BeanUtils.getPropertyDescriptor(bean.getClass(), propertyName);
        return (String) propertyDescriptor.getReadMethod().invoke(bean);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.nortal.petit.beanmapper.BeanMappingUtils.java

private static boolean isPropertyReadableAndWritable(PropertyDescriptor pd) {
    if (pd == null || pd.getReadMethod() == null || pd.getWriteMethod() == null) {
        return false;
    }//from  w  ww. j a va  2 s  .  c  om

    return true;
}

From source file:com.amazonaws.services.dynamodbv2.datamodeling.DynamoDbPropertyMarshaller.java

public static AttributeValue getAttributeValue(final Object propertyValue,
        final PropertyDescriptor propertyDescriptor) {
    final Method readMethod = propertyDescriptor.getReadMethod();
    if (propertyValue == null) {
        return null;
    }/* w ww  .  j a va  2  s .  co m*/
    if (propertyValue instanceof Collection && ((Collection<?>) propertyValue).isEmpty()) {
        return null;
    }
    final DynamoDBReflectorUtil reflector = new DynamoDBReflectorUtil();
    try {
        final ArgumentMarshaller marshaller = reflector.getArgumentMarshaller(readMethod);
        return marshaller.marshall(propertyValue);
    } catch (final DynamoDBMappingException e) {
        try {
            final StringWriter output = new StringWriter();
            final JsonGenerator jsonGenerator = jsonFactory.createGenerator(output);
            jsonGenerator.writeObject(propertyValue);
            return new AttributeValue(output.toString());
        } catch (final IOException ioException) {
            throw new IllegalStateException(ioException);
        }
    }
}

From source file:com.nortal.petit.beanmapper.BeanMappingReflectionUtils.java

private static void readAnnotations(List<Annotation> l, Class<?> type, String name) {
    Column ao = getAttributeOverride(type, name);
    if (ao != null) {
        l.add(ao);/*from ww  w .j av a 2 s  .c om*/
    }
    Field field = FieldUtils.getDeclaredField(type, name, true);
    if (field != null) {
        addAll(l, field.getAnnotations());
    }
    PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(type, name);
    if (pd != null) {
        if (pd.getReadMethod() != null) {
            addAll(l, pd.getReadMethod().getAnnotations());
        }
    }
    if (type.getSuperclass() != null) {
        readAnnotations(l, type.getSuperclass(), name);
    }
}