This adds default resizing of array or java.util.List properties
Btw. I don't know why ClassCastException is catched if the type is explicitly checked using instanceof


Index: DynaActionForm.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-struts/src/share/org/apache/struts/action/DynaActionForm.java,v
retrieving revision 1.15
diff -u -r1.15 DynaActionForm.java
--- DynaActionForm.java 24 Apr 2004 06:37:00 -0000      1.15
+++ DynaActionForm.java 8 Jun 2004 04:06:27 -0000
@@ -447,25 +447,39 @@
      *  exists, but is not indexed
      * @exception IndexOutOfBoundsException if the specified index
      *  is outside the range of the underlying property
+     * @exception UnsupportedOperationException if the specified property
+     *  does not support setting a value to the specified index
      */
     public void set(String name, int index, Object value) {
 
-        Object prop = dynaValues.get(name);
-        if (prop == null) {
-            throw new NullPointerException
-                ("No indexed value for '" + name + "[" + index + "]'");
-        } else if (prop.getClass().isArray()) {
-            Array.set(prop, index, value);
-        } else if (prop instanceof List) {
-            try {
-                ((List) prop).set(index, value);
-            } catch (ClassCastException e) {
-                throw new ConversionException(e.getMessage());
-            }
-        } else {
-            throw new IllegalArgumentException
-                ("Non-indexed property for '" + name + "[" + index + "]'");
-        }
+               Object prop = dynaValues.get(name);
+               if (prop == null) {
+                       throw new NullPointerException
+                               ("No indexed value for '" + name + "[" + index + "]'");
+               } else if (prop.getClass().isArray()) {
+                       int length = Array.getLength(prop);
+                       if (length <= index) {
+                               Object tmp = 
Array.newInstance(prop.getClass().getComponentType(), index + 1);
+                               if (length > 0)
+                                       System.arraycopy(prop, 0, tmp, 0, length);
+                               prop = tmp;
+                               dynaValues.put(name, prop);
+                       }
+                       Array.set(prop, index, value);
+               } else if (prop instanceof List) {
+                       try {
+                         List list = (List) prop;
+                         for (int i = list.size(); i <= index; i++) {
+                                 list.add(null);
+                         }
+                         list.set(index, value);
+                       } catch (ClassCastException e) {
+                               throw new ConversionException(e.getMessage());
+                       }
+               } else {
+                       throw new IllegalArgumentException
+                               ("Non-indexed property for '" + name + "[" + index + 
"]'");
+               }
 
     }
 

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

Reply via email to