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.echocat.jemoni.jmx.reflection.ReflectionBasedAttributeDefinition.java

@Nonnull
protected AccessMode getAccessModeBy(@Nonnull PropertyDescriptor descriptor) {
    final Method readMethod = descriptor.getReadMethod();
    final Method writeMethod = descriptor.getWriteMethod();
    final AccessMode accessMode;
    if (readMethod != null && writeMethod != null) {
        accessMode = AccessMode.readWrite;
    } else if (readMethod != null) {
        accessMode = AccessMode.readOnly;
    } else if (writeMethod != null) {
        accessMode = AccessMode.writeOnly;
    } else {/*from  ww w .  jav  a  2  s .c o  m*/
        throw new IllegalStateException("There is no read and no write method?");
    }
    return accessMode;
}

From source file:com.manydesigns.elements.reflection.JavaPropertyAccessor.java

public JavaPropertyAccessor(PropertyDescriptor propertyDescriptor) {
    this.propertyDescriptor = propertyDescriptor;
    getter = propertyDescriptor.getReadMethod();
    setter = propertyDescriptor.getWriteMethod();
    if (setter == null) {
        logger.debug("Setter not available for: {}", propertyDescriptor.getName());
    }/*from   ww  w.j ava2s  . c o  m*/
}

From source file:grails.util.GrailsClassUtils.java

/**
 * Retrieves a PropertyDescriptor for the specified instance and property value
 *
 * @param instance The instance/*from  w w w .  j a va  2s  . c  o m*/
 * @param propertyValue The value of the property
 * @return The PropertyDescriptor
 */
public static PropertyDescriptor getPropertyDescriptorForValue(Object instance, Object propertyValue) {
    if (instance == null || propertyValue == null) {
        return null;
    }

    PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(instance.getClass());
    for (PropertyDescriptor pd : descriptors) {
        if (isAssignableOrConvertibleFrom(pd.getPropertyType(), propertyValue.getClass())) {
            Object value;
            try {
                ReflectionUtils.makeAccessible(pd.getReadMethod());
                value = pd.getReadMethod().invoke(instance);
            } catch (Exception e) {
                throw new FatalBeanException("Problem calling readMethod of " + pd, e);
            }
            if (propertyValue.equals(value)) {
                return pd;
            }
        }
    }
    return null;
}

From source file:jp.co.ctc_g.jfw.core.util.Beans.java

private static Object readPropertyValueNamed0(String propertyName, Object bean) {
    PropertyDescriptor pd = findPropertyDescriptorFor(bean.getClass(), propertyName);
    if (pd == null) {
        if (L.isDebugEnabled()) {
            Map<String, Object> replace = new HashMap<String, Object>(2);
            replace.put("class", bean.getClass().getName());
            replace.put("property", propertyName);
            L.debug(Strings.substitute(R.getString("D-UTIL#0015"), replace));
        }/*w  w  w.  ja  v  a  2 s  .  c  o  m*/
        return null;
    }
    Method reader = pd.getReadMethod();
    if (reader == null) {
        if (L.isDebugEnabled()) {
            Map<String, Object> replace = new HashMap<String, Object>(2);
            replace.put("class", bean.getClass().getName());
            replace.put("property", propertyName);
            L.debug(Strings.substitute(R.getString("D-UTIL#0016"), replace));
        }
        return null;
    }
    return Reflects.invoke(reader, bean);
}

From source file:org.force66.beantester.tests.ValuePropertyTest.java

private boolean testReadValue(Object bean, PropertyDescriptor descriptor, Object value)
        throws IllegalAccessException, InvocationTargetException {
    boolean answer = true;
    if (descriptor.getReadMethod() != null) {
        if (value == null) {
            answer = descriptor.getReadMethod().invoke(bean) == null;
        } else {//from   ww w  .ja v  a2s .  c  o m
            answer = value.equals(descriptor.getReadMethod().invoke(bean));
        }

    }
    return answer;
}

From source file:com.mindquarry.persistence.jcr.model.DefaultProperty.java

public boolean isAccessible() {
    PropertyDescriptor descriptor = getPropertyDescriptor();
    if (descriptor != null) {
        Method readMethod = descriptor.getReadMethod();
        if ((readMethod == null) && (descriptor instanceof IndexedPropertyDescriptor)) {
            readMethod = getIndexedReadMethod(descriptor);
        }/*ww w  .  j  a va 2  s .c o  m*/
        return readMethod != null;
    } else {
        return false;
    }
}

From source file:org.grails.datastore.mapping.keyvalue.mapping.config.AnnotationKeyValueMappingFactory.java

@Override
public KeyValue createMappedForm(PersistentProperty mpp) {
    final Class javaClass = mpp.getOwner().getJavaClass();
    final ClassPropertyFetcher cpf = ClassPropertyFetcher.forClass(javaClass);

    final PropertyDescriptor pd = cpf.getPropertyDescriptor(mpp.getName());
    final KeyValue kv = super.createMappedForm(mpp);
    Index index = AnnotationUtils.getAnnotation(pd.getReadMethod(), Index.class);

    if (index == null) {
        final Field field = ReflectionUtils.findField(javaClass, mpp.getName());
        if (field != null) {
            ReflectionUtils.makeAccessible(field);
            index = field.getAnnotation(Index.class);
        }//from   www . j  a  v  a  2 s  .co  m
    }
    if (index != null) {
        kv.setIndex(true);
    }

    return kv;
}

From source file:org.constretto.internal.store.ObjectConfigurationStore.java

private TaggedPropertySet createPropertySetForObject(Object configurationObject) {
    String tag = ConfigurationValue.DEFAULT_TAG;
    String basePath = "";
    Map<String, String> properties = new HashMap<String, String>();
    if (configurationObject.getClass().isAnnotationPresent(ConfigurationSource.class)) {
        ConfigurationSource configurationAnnotation = configurationObject.getClass()
                .getAnnotation(ConfigurationSource.class);
        tag = configurationAnnotation.tag();
        if (tag.equals("")) {
            tag = ConfigurationValue.DEFAULT_TAG;
        }//from w  ww .j a  va 2  s.co  m
        basePath = configurationAnnotation.basePath();
    }

    for (PropertyDescriptor propertyDescriptor : PropertyUtils.getPropertyDescriptors(configurationObject)) {
        boolean canRead = propertyDescriptor.getReadMethod() != null;
        boolean isString = propertyDescriptor.getPropertyType().isAssignableFrom(String.class);
        if (canRead && isString) {
            String path = propertyDescriptor.getName();
            try {
                String value = (String) PropertyUtils.getProperty(configurationObject, path);
                if (!ConstrettoUtils.isEmpty(basePath)) {
                    path = basePath + "." + path;
                }
                properties.put(path, value);
            } catch (Exception e) {
                throw new ConstrettoException("Could not access data in field", e);
            }
        }

    }

    return new TaggedPropertySet(tag, properties, getClass());
}

From source file:org.dspace.app.rest.link.DSpaceResourceHalLinkFactory.java

protected void addLinks(DSpaceResource halResource, Pageable page, LinkedList<Link> list) throws Exception {
    RestAddressableModel data = halResource.getContent();

    try {// ww  w.  j  a v  a  2  s  .c  o m
        for (PropertyDescriptor pd : Introspector.getBeanInfo(data.getClass()).getPropertyDescriptors()) {
            Method readMethod = pd.getReadMethod();
            String name = pd.getName();
            if (readMethod != null && !"class".equals(name)) {
                LinkRest linkAnnotation = readMethod.getAnnotation(LinkRest.class);

                if (linkAnnotation != null) {
                    if (StringUtils.isNotBlank(linkAnnotation.name())) {
                        name = linkAnnotation.name();
                    }

                    Link linkToSubResource = utils.linkToSubResource(data, name);
                    // no method is specified to retrieve the linked object(s) so check if it is already here
                    if (StringUtils.isBlank(linkAnnotation.method())) {
                        Object linkedObject = readMethod.invoke(data);

                        if (linkedObject instanceof RestAddressableModel
                                && linkAnnotation.linkClass().isAssignableFrom(linkedObject.getClass())) {
                            linkToSubResource = utils.linkToSingleResource((RestAddressableModel) linkedObject,
                                    name);
                        }

                        if (linkedObject != null || !linkAnnotation.optional()) {
                            halResource.add(linkToSubResource);
                        }
                    }

                } else if (RestModel.class.isAssignableFrom(readMethod.getReturnType())) {
                    Link linkToSubResource = utils.linkToSubResource(data, name);
                    halResource.add(linkToSubResource);
                }
            }
        }
    } catch (IntrospectionException e) {
        e.printStackTrace();
    }

    halResource.add(utils.linkToSingleResource(data, Link.REL_SELF));
}

From source file:org.opentele.server.core.util.CustomGroovyBeanJSONMarshaller.java

public void marshalObject(Object o, JSON json) throws ConverterException {
    JSONWriter writer = json.getWriter();
    try {//from   ww w .  j av  a  2 s .  c  o  m
        writer.object();
        for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
            String name = property.getName();
            Method readMethod = property.getReadMethod();
            if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) {
                Object value = readMethod.invoke(o, (Object[]) null);
                writer.key(name);
                json.convertAnother(value);
            }
        }
        for (Field field : o.getClass().getDeclaredFields()) {
            int modifiers = field.getModifiers();
            if (Modifier.isPublic(modifiers)
                    && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
                writer.key(field.getName());
                json.convertAnother(field.get(o));
            }
        }
        writer.endObject();
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Bean with class " + o.getClass().getName(), e);
    }
}