[ http://issues.apache.org/jira/browse/BEANUTILS-177?page=all ]
Niall Pemberton updated BEANUTILS-177:
--------------------------------------
Bugzilla Id: (was: 17501)
Component/s: Bean / Property Utils
> [beanutils] Add dynamic discovery of mapped properties to PropertyUtils
> -----------------------------------------------------------------------
>
> Key: BEANUTILS-177
> URL: http://issues.apache.org/jira/browse/BEANUTILS-177
> Project: Commons BeanUtils
> Issue Type: Improvement
> Components: Bean / Property Utils
> Affects Versions: 1.6
> Environment: Operating System: All
> Platform: All
> Reporter: Kris Schneider
> Priority: Minor
>
> 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;
> }
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]