DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=17501>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=17501

Add dynamic discovery of mapped properties to PropertyUtils

           Summary: Add dynamic discovery of mapped properties to
                    PropertyUtils
           Product: Commons
           Version: 1.6 Final
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Bean Utilities
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]
                CC: [EMAIL PROTECTED]


I had thought that the getMappedPropertyDescriptors method would dynamically
discover the mapped properties of a given bean (as the getPropertyDescriptors
method does for simple and indexed properties), but it looks like all it does is
return a map of cached descriptors. It also looks like the only place entries
are added to that cache is in the getPropertyDescriptor method. Which means you
have to know the name of the mapped property ahead of time. It would be nice for
PropertyUtils to provide a method that returns an array of
MappedPropertyDescriptor for a given bean. As an example, here's a couple of
utility methods I'm currently using:

    public static MappedPropertyDescriptor[] getMappedPropertyDescriptors(Object
bean) {
        if (bean == null) {
            throw new NullPointerException("No bean specified");
        }
        return (getMappedPropertyDescriptors(bean.getClass()));
    }

    public static MappedPropertyDescriptor[] getMappedPropertyDescriptors(Class
beanClass) {
        if (beanClass == null) {
            throw new NullPointerException("No bean class specified");
        }

        Method[] allMethods = beanClass.getMethods();
        Map descriptorMap = new HashMap();
        for (int i = 0, n = allMethods.length; i < n; i++) {
            String methodName = allMethods[i].getName();
            if ((methodName.startsWith("get")) || (methodName.startsWith("set"))) {
                String propName =
Introspector.decapitalize(methodName.substring(3));
                if ((propName.length() > 0) && (descriptorMap.get(propName) ==
null)) {
                    try {
                        MappedPropertyDescriptor descriptor = new
MappedPropertyDescriptor(propName, beanClass);
                        descriptorMap.put(propName, descriptor);
                    } catch (IntrospectionException ignore) {}
                }
            }
        }

        MappedPropertyDescriptor[] descriptors = null;

        int numDescriptors = descriptorMap.size();
        if (numDescriptors > 0) {
            descriptors = new MappedPropertyDescriptor[numDescriptors];
            Iterator iter = descriptorMap.values().iterator();
            int i = 0;
            while (iter.hasNext()) {
                descriptors[i++] = (MappedPropertyDescriptor)iter.next();
            }
        }

        if (descriptors == null) {
            descriptors = new MappedPropertyDescriptor[0];
        }

        return descriptors;
    }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to