Example usage for java.beans IndexedPropertyDescriptor setReadMethod

List of usage examples for java.beans IndexedPropertyDescriptor setReadMethod

Introduction

In this page you can find the example usage for java.beans IndexedPropertyDescriptor setReadMethod.

Prototype

public synchronized void setReadMethod(Method readMethod) throws IntrospectionException 

Source Link

Document

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

Usage

From source file:eu.qualityontime.commons.DefaultBeanIntrospector.java

/**
 * This method fixes an issue where IndexedPropertyDescriptor behaves
 * differently in different versions of the JDK for 'indexed' properties
 * which use java.util.List (rather than an array). It implements a
 * workaround for Bug 28358. If you have a Bean with the following
 * getters/setters for an indexed property:
 *
 * <pre>//from  w  ww  . j  a  va  2 s .  com
 * public List getFoo()
 * public Object getFoo(int index)
 * public void setFoo(List foo)
 * public void setFoo(int index, Object foo)
 * </pre>
 *
 * then the IndexedPropertyDescriptor's getReadMethod() and getWriteMethod()
 * behave as follows:
 * <ul>
 * <li>JDK 1.3.1_04: returns valid Method objects from these methods.</li>
 * <li>JDK 1.4.2_05: returns null from these methods.</li>
 * </ul>
 *
 * @param beanClass
 *            the current class to be inspected
 * @param descriptors
 *            the array with property descriptors
 */
private void handleIndexedPropertyDescriptors(final Class<?> beanClass,
        final PropertyDescriptor[] descriptors) {
    for (final PropertyDescriptor pd : descriptors) {
        if (pd instanceof IndexedPropertyDescriptor) {
            final IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) pd;
            final String propName = descriptor.getName().substring(0, 1).toUpperCase()
                    + descriptor.getName().substring(1);

            if (descriptor.getReadMethod() == null) {
                final String methodName = descriptor.getIndexedReadMethod() != null
                        ? descriptor.getIndexedReadMethod().getName()
                        : "get" + propName;
                final Method readMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName,
                        EMPTY_CLASS_PARAMETERS);
                if (readMethod != null) {
                    try {
                        descriptor.setReadMethod(readMethod);
                    } catch (final Exception e) {
                        log.error("Error setting indexed property read method", e);
                    }
                }
            }
            if (descriptor.getWriteMethod() == null) {
                final String methodName = descriptor.getIndexedWriteMethod() != null
                        ? descriptor.getIndexedWriteMethod().getName()
                        : "set" + propName;
                Method writeMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName,
                        LIST_CLASS_PARAMETER);
                if (writeMethod == null) {
                    for (final Method m : beanClass.getMethods()) {
                        if (m.getName().equals(methodName)) {
                            final Class<?>[] parameterTypes = m.getParameterTypes();
                            if (parameterTypes.length == 1 && List.class.isAssignableFrom(parameterTypes[0])) {
                                writeMethod = m;
                                break;
                            }
                        }
                    }
                }
                if (writeMethod != null) {
                    try {
                        descriptor.setWriteMethod(writeMethod);
                    } catch (final Exception e) {
                        log.error("Error setting indexed property write method", e);
                    }
                }
            }
        }
    }
}

From source file:org.evergreen.web.utils.beanutils.PropertyUtilsBean.java

/**
 * <p>Retrieve the property descriptors for the specified class,
 * introspecting and caching them the first time a particular bean class
 * is encountered.</p>//from ww w . ja v a2 s .com
 *
 * <p><strong>FIXME</strong> - Does not work with DynaBeans.</p>
 *
 * @param beanClass Bean class for which property descriptors are requested
 * @return the property descriptors
 *
 * @exception IllegalArgumentException if <code>beanClass</code> is null
 */
public PropertyDescriptor[] getPropertyDescriptors(Class beanClass) {

    if (beanClass == null) {
        throw new IllegalArgumentException("No bean class specified");
    }

    // Look up any cached descriptors for this bean class
    PropertyDescriptor[] descriptors = null;
    descriptors = (PropertyDescriptor[]) descriptorsCache.get(beanClass);
    if (descriptors != null) {
        return (descriptors);
    }

    // Introspect the bean and cache the generated descriptors
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(beanClass);
    } catch (IntrospectionException e) {
        return (new PropertyDescriptor[0]);
    }
    descriptors = beanInfo.getPropertyDescriptors();
    if (descriptors == null) {
        descriptors = new PropertyDescriptor[0];
    }

    // ----------------- Workaround for Bug 28358 --------- START ------------------
    //
    // The following code fixes an issue where IndexedPropertyDescriptor behaves
    // Differently in different versions of the JDK for 'indexed' properties which
    // use java.util.List (rather than an array).
    //
    // If you have a Bean with the following getters/setters for an indexed property:
    //
    //     public List getFoo()
    //     public Object getFoo(int index)
    //     public void setFoo(List foo)
    //     public void setFoo(int index, Object foo)
    //
    // then the IndexedPropertyDescriptor's getReadMethod() and getWriteMethod()
    // behave as follows:
    //
    //     JDK 1.3.1_04: returns valid Method objects from these methods.
    //     JDK 1.4.2_05: returns null from these methods.
    //
    for (int i = 0; i < descriptors.length; i++) {
        if (descriptors[i] instanceof IndexedPropertyDescriptor) {
            IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) descriptors[i];
            String propName = descriptor.getName().substring(0, 1).toUpperCase()
                    + descriptor.getName().substring(1);

            if (descriptor.getReadMethod() == null) {
                String methodName = descriptor.getIndexedReadMethod() != null
                        ? descriptor.getIndexedReadMethod().getName()
                        : "get" + propName;
                Method readMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName,
                        EMPTY_CLASS_PARAMETERS);
                if (readMethod != null) {
                    try {
                        descriptor.setReadMethod(readMethod);
                    } catch (Exception e) {
                        log.error("Error setting indexed property read method", e);
                    }
                }
            }
            if (descriptor.getWriteMethod() == null) {
                String methodName = descriptor.getIndexedWriteMethod() != null
                        ? descriptor.getIndexedWriteMethod().getName()
                        : "set" + propName;
                Method writeMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName,
                        LIST_CLASS_PARAMETER);
                if (writeMethod == null) {
                    Method[] methods = beanClass.getMethods();
                    for (int j = 0; j < methods.length; j++) {
                        if (methods[j].getName().equals(methodName)) {
                            Class[] parameterTypes = methods[j].getParameterTypes();
                            if (parameterTypes.length == 1 && List.class.isAssignableFrom(parameterTypes[0])) {
                                writeMethod = methods[j];
                                break;
                            }
                        }
                    }
                }
                if (writeMethod != null) {
                    try {
                        descriptor.setWriteMethod(writeMethod);
                    } catch (Exception e) {
                        log.error("Error setting indexed property write method", e);
                    }
                }
            }
        }
    }
    // ----------------- Workaround for Bug 28358 ---------- END -------------------

    descriptorsCache.put(beanClass, descriptors);
    return (descriptors);

}

From source file:org.mypsycho.beans.PropertyUtilsBean.java

/**
 * <p>/*from  w ww  .  j a  v a  2 s .  c  om*/
 * Retrieve the property descriptors for the specified class, introspecting
 * and caching them the first time a particular bean class is encountered.
 * </p>
 *
 * @param beanClass Bean class for which property descriptors are requested
 * @return the property descriptors
 * @exception IllegalArgumentException if <code>beanClass</code> is null
 */
public PropertyDescriptor[] createDescriptorsCache(Class<?> beanClass) throws IntrospectionException {

    // Introspect the bean and cache the generated descriptors
    BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);

    PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
    if (descriptors == null) {
        return new PropertyDescriptor[0];
    }

    // ----------------- Workaround for Bug 28358 --------- START ------------------
    //
    // The following code fixes an issue where IndexedPropertyDescriptor
    // behaves
    // Differently in different versions of the JDK for 'indexed' properties
    // which use java.util.List (rather than an array).
    //
    // If you have a Bean with the following getters/setters for an indexed
    // property:
    //
    // public List getFoo()
    // public Object getFoo(int index)
    // public void setFoo(List foo)
    // public void setFoo(int index, Object foo)
    //
    // then the IndexedPropertyDescriptor's getReadMethod() and
    // getWriteMethod()
    // behave as follows:
    //
    // JDK 1.3.1_04: returns valid Method objects from these methods.
    // JDK 1.4.2_05: returns null from these methods.
    //
    for (PropertyDescriptor descriptor2 : descriptors) {
        if (!(descriptor2 instanceof IndexedPropertyDescriptor)) {
            continue;
        }
        IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) descriptor2;
        String propName = descriptor.getName().substring(0, 1).toUpperCase()
                + descriptor.getName().substring(1);

        if (descriptor.getReadMethod() == null) {
            String methodName = (descriptor.getIndexedReadMethod() != null)
                    ? descriptor.getIndexedReadMethod().getName()
                    : "get" + propName;
            Method readMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName,
                    EMPTY_CLASS_PARAMETERS);
            if (readMethod != null) {
                try {
                    descriptor.setReadMethod(readMethod);
                } catch (Exception e) {
                    notify("copy", "Fail to set indexed property" + propName, e);
                }
            }
        }
        if (descriptor.getWriteMethod() == null) {
            Method indexedMethod = descriptor.getIndexedWriteMethod();
            String methodName = indexedMethod != null ? indexedMethod.getName() : "set" + propName;
            Method writeMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName,
                    LIST_CLASS_PARAMETER);
            if (writeMethod == null) {
                Method[] methods = beanClass.getMethods();
                for (Method method : methods) {
                    if (method.getName().equals(methodName)) {
                        Class<?>[] parameterTypes = method.getParameterTypes();
                        if (parameterTypes.length == 1 && List.class.isAssignableFrom(parameterTypes[0])) {
                            writeMethod = method;
                            break;
                        }
                    }
                }
            }
            if (writeMethod != null) {
                try {
                    descriptor.setWriteMethod(writeMethod);
                } catch (Exception e) {
                    notify("copy", "Fail to set indexed property" + propName, e);
                }
            }
        }

    }
    // ----------------- Workaround for Bug 28358 ---------- END -------------------

    return descriptors;
}