You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ni...@apache.org on 2005/02/18 22:05:20 UTC

svn commit: r154349 - in jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils: BeanUtilsBean.java PropertyUtilsBean.java

Author: niallp
Date: Fri Feb 18 13:05:19 2005
New Revision: 154349

URL: http://svn.apache.org/viewcvs?view=rev&rev=154349
Log:
Fix for Bug 28358 - Problems on indexed property with JDK 1.4

Modified:
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java?view=diff&r1=154348&r2=154349
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java Fri Feb 18 13:05:19 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@
  * @author Chris Audley
  * @author Rey François
  * @author Gregor Raýman
- * @version $Revision: 1.17 $ $Date: 2004/10/17 14:01:04 $
+ * @version $Revision$ $Date$
  * @see BeanUtils
  * @since 1.7
  */
@@ -967,7 +967,7 @@
                 }
                 type = ((MappedPropertyDescriptor) descriptor).
                     getMappedPropertyType();
-            } else if (descriptor instanceof IndexedPropertyDescriptor) {
+            } else if (index >= 0 && descriptor instanceof IndexedPropertyDescriptor) {
                 if (((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod() == null) {
                     if (log.isDebugEnabled()) {
                         log.debug("Skipping read-only property");

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java?view=diff&r1=154348&r2=154349
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java Fri Feb 18 13:05:19 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -88,7 +88,7 @@
  * @author Gregor Raýman
  * @author Jan Sorensen
  * @author Scott Sanders
- * @version $Revision: 1.15 $ $Date: 2004/08/02 19:49:21 $
+ * @version $Revision$ $Date$
  * @see PropertyUtils
  * @since 1.7
  */
@@ -109,6 +109,8 @@
      */
     private FastHashMap descriptorsCache = null;
     private FastHashMap mappedDescriptorsCache = null;
+    private static final Class[] indexedListGetterArgs = new Class[0];
+    private static final Class[] indexedListSetterArgs = new Class[] {java.util.List.class};
     
     /** Log instance */
     private Log log = LogFactory.getLog(PropertyUtils.class);
@@ -901,6 +903,79 @@
         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,
+                                                            indexedListGetterArgs);
+                    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,
+                                                            indexedListSetterArgs);
+                    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);
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org