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.atlasapi.output.rdf.RdfIntrospector.java

public static String getRdfIdProperty(Class<?> entityType) throws Exception {
    Map<String, PropertyDescriptor> metadata = BeanIntrospector.getPropertyDescriptors(entityType);

    for (Map.Entry<String, PropertyDescriptor> entry : metadata.entrySet()) {
        PropertyDescriptor pd = entry.getValue();
        Method readMethod = pd.getReadMethod();
        RdfId ann = readMethod.getAnnotation(RdfId.class);
        if (ann != null) {
            return entry.getKey();
        }/*from ww w. j  a va2s.c  o m*/
    }

    return null;
}

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

public static <T extends Item> AttributeValue getValue(final T item,
        final PropertyDescriptor propertyDescriptor) {
    final Method readMethod = propertyDescriptor.getReadMethod();
    Object propertyValue;//w  w  w . ja va2  s  . c o m
    try {
        propertyValue = readMethod.invoke(item);
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
    if (propertyValue == null) {
        return null;
    }
    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:de.unentscheidbar.validation.internal.Beans.java

public static @Nonnull MethodHandle getterMethod(Class<?> beanClass, String propertyName) {

    PropertyDescriptor pd = property(beanClass, propertyName);
    Method getter = pd == null ? null : pd.getReadMethod();
    if (getter == null) {
        throw new IllegalArgumentException("Could not find a readable property named " + propertyName
                + " in class " + beanClass.getName());
    }//from  w  ww  . j  a va  2 s .co  m
    try {
        return MethodHandles.lookup().unreflect(getter);
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException("I do not have access to property read method " + getter);
    }
}

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

public static String rdfProperty(PropertyDescriptor property, String defaultNS) {
    Method readMethod = property.getReadMethod();
    String propertyName = null;//from  w  ww .  j a va  2s.  c  om

    RdfProperty rdfProperty = (RdfProperty) readMethod.getAnnotation(RdfProperty.class);

    if (rdfProperty != null) {
        if (rdfProperty.uri() != null && rdfProperty.uri().length() > 0) {
            propertyName = rdfProperty.uri();
        } else {
            propertyName = property.getName();
        }

        if (rdfProperty.namespace() != null && rdfProperty.namespace().length() > 0) {
            return rdfProperty.namespace() + propertyName;
        } else {
            return defaultNS + propertyName;
        }
    }

    return null;
}

From source file:de.unentscheidbar.validation.internal.Beans.java

public static boolean hasReadableProperty(Class<?> beanClass, String propertyName, Class<?> returnType) {

    PropertyDescriptor pd = property(beanClass, propertyName);
    if (pd == null)
        return false;
    Method getter = pd.getReadMethod();
    return (getter != null) && ClassUtils.isAssignable(getter.getReturnType(), returnType);
}

From source file:org.eclipse.scada.da.core.VariantBeanHelper.java

/**
 * Extract the property data as string/variant map
 * @param source the source object/*from ww w  .  j ava2 s  .c om*/
 * @return the map with bean data
 * @throws IntrospectionException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static Map<String, Variant> extract(final Object source) throws IntrospectionException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    final Map<String, Variant> result = new HashMap<String, Variant>();

    final BeanInfo bi = Introspector.getBeanInfo(source.getClass());

    for (final PropertyDescriptor pd : bi.getPropertyDescriptors()) {
        final Method m = pd.getReadMethod();
        if (m != null) {
            result.put(pd.getName(), Variant.valueOf(m.invoke(source)));
        }
    }
    return result;
}

From source file:org.apache.ignite.cache.store.cassandra.common.PropertyMappingHelper.java

/**
 * Extracts all property descriptors from a class.
 *
 * @param clazz class which property descriptors should be extracted.
 * @param primitive boolean flag indicating that only property descriptors for primitive properties
 *      should be extracted./*from  www .  jav a  2 s . c om*/
 *
 * @return list of class property descriptors
 */
public static List<PropertyDescriptor> getPojoPropertyDescriptors(Class clazz, boolean primitive) {
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(clazz);

    List<PropertyDescriptor> list = new ArrayList<>(descriptors == null ? 1 : descriptors.length);

    if (descriptors == null || descriptors.length == 0)
        return list;

    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getReadMethod() == null || (primitive && !isPrimitivePropertyDescriptor(descriptor)))
            continue;

        list.add(descriptor);
    }

    return list;
}

From source file:org.eclipse.scada.da.core.VariantBeanHelper.java

public static Map<String, Variant> extractSafe(final Object source) {
    final Map<String, Variant> result = new HashMap<String, Variant>();

    BeanInfo bi;/*  ww w  .  jav  a 2  s .co m*/
    try {
        bi = Introspector.getBeanInfo(source.getClass());
    } catch (final IntrospectionException e) {
        return result;
    }

    for (final PropertyDescriptor pd : bi.getPropertyDescriptors()) {
        final Method m = pd.getReadMethod();
        if (m != null) {
            try {
                result.put(pd.getName(), Variant.valueOf(m.invoke(source)));
            } catch (final Exception e) {
                // ignore
            }
        }
    }
    return result;
}

From source file:arena.utils.ReflectionUtils.java

public static String[] getAttributeNamesUsingGetter(Class<?> entityClass) {
    //        return BeanUtils.getPropertyDescriptors(entityClass);
    Class<?> clsMe = entityClass;
    List<String> voFieldNames = new ArrayList<String>();

    while (clsMe != null) {
        Field[] fields = clsMe.getDeclaredFields();

        for (int n = 0; n < fields.length; n++) {
            int modifiers = fields[n].getModifiers();
            if (!Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)) {
                String fieldName = fields[n].getName();
                try {
                    PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(entityClass, fieldName);
                    if (pd.getReadMethod() != null) {
                        voFieldNames.add(fieldName);
                    }/*ww  w.j a  va2s  . c  o m*/
                } catch (Throwable err) {
                    // skip
                }
            }
        }

        // Loop back to parent class
        clsMe = clsMe.getSuperclass();
    }
    return voFieldNames.toArray(new String[voFieldNames.size()]);
}

From source file:com.cloudera.csd.validation.references.components.ReflectionHelper.java

/**
 * Return the set of getter methods for the class.
 *
 * @param clazz the class./*  w  w  w.  j av  a2s.  c o m*/
 * @return the set of getter methods.
 */
public static Set<Method> getterMethods(Class<?> clazz) {
    Preconditions.checkNotNull(clazz);
    try {
        ImmutableSet.Builder<Method> builder = ImmutableSet.builder();
        BeanInfo info = Introspector.getBeanInfo(clazz);
        for (PropertyDescriptor p : info.getPropertyDescriptors()) {
            Method getter = p.getReadMethod();
            if (getter != null) {
                // Don't want to include any of the methods inherited from object.
                if (!getter.getDeclaringClass().equals(Object.class)) {
                    builder.add(getter);
                }
            }
        }
        return builder.build();
    } catch (IntrospectionException e) {
        throw new IllegalStateException("Could not introspect on " + clazz, e);
    }
}