You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Ken X Horn <ho...@jpmorgan.com> on 2000/10/18 12:23:35 UTC

PATCH: Nested bean setters

I appologise for the hacky nature of the change (I just commented out the
NoSuchMethodException in setSimpleProperty --
should be thrown and caught in populate? Or is this too slow?) -- wasn't sure
how much code references this method.

Ken

PS The last patch I submitted, for setting thrown exceptions in tags as a
request attribute, never got incorporated (though it
was on the todo list for Struts 1.0) -- is this still planned? If it does go in
it means we can switch to a normal build of struts again.

----------------------------------------------------

diff -wc cvssrc/BeanUtils.java ./BeanUtils.java
*** cvssrc/BeanUtils.java     Wed Oct 18 10:46:26 2000
--- ./BeanUtils.java     Tue Oct 17 15:19:44 2000
***************
*** 637,642 ****
--- 637,643 ----
            stripped.substring(0, stripped.length() - suffix.length());
         }
         properties.put(stripped, request.getParameterValues(name));
+
     }

     // Set the corresponding properties of our bean
***************
*** 679,685 ****
       *  throws an exception
       */
      public static void populate(Object bean, Hashtable properties)
!    throws IllegalAccessException, InvocationTargetException {

     if ((bean == null) || (properties == null))
         return;
--- 680,686 ----
       *  throws an exception
       */
      public static void populate(Object bean, Hashtable properties)
!    throws IllegalAccessException,
InvocationTargetException,NoSuchMethodException {

     if ((bean == null) || (properties == null))
         return;
***************
*** 700,749 ****
         if (value == null)
          continue;

!        // Identify the relevant setter method (if there is one)
!        Method setter = null;
!        Class parameterTypes[] = null;
!        for (int i = 0; i < descriptors.length; i++) {
!         if (!name.equals(descriptors[i].getName()))
!             continue;
!         setter = descriptors[i].getWriteMethod();
!         if (setter == null)
!             continue;
!         parameterTypes = setter.getParameterTypes();
!         if (parameterTypes.length != 1) {
!             setter = null;
!             continue;
!         }
!         break;
!        }
!        if (setter == null)
!         continue;

!        // Convert the parameter value as required for this setter method
!        Object parameters[] = new Object[1];
!        if (parameterTypes[0].isArray()) {
!         if (value instanceof String) {
!             String values[] = new String[1];
!             values[0] = (String) value;
!             parameters[0] = convert((String[]) values,
!                            parameterTypes[0]);
!         } else {
!             parameters[0] = convert((String[]) value,
!                            parameterTypes[0]);
!         }
!        } else {
!         if (value instanceof String) {
!             parameters[0] = convert((String) value,
!                            parameterTypes[0]);
!         } else {
!             parameters[0] = convert( ((String[]) value)[0],
!                             parameterTypes[0]);
!         }
!        }

-        // Invoke the setter method
-        setter.invoke(bean, parameters);
-
     }

      }
--- 701,753 ----
         if (value == null)
          continue;

!         PropertyUtils.setProperty(bean, name, value);

! //        // Identify the relevant setter method (if there is one)
! //        Method setter = null;
! //        Class parameterTypes[] = null;
! //        for (int i = 0; i < descriptors.length; i++) {
! //
! //        if (!name.equals(descriptors[i].getName()))
! //            continue;
! //        setter = descriptors[i].getWriteMethod();
! //        if (setter == null)
! //            continue;
! //        parameterTypes = setter.getParameterTypes();
! //        if (parameterTypes.length != 1) {
! //            setter = null;
! //            continue;
! //        }
! //        break;
! //        }
! //        if (setter == null)
! //        continue;
! //
! //        // Convert the parameter value as required for this setter method
! //        Object parameters[] = new Object[1];
! //        if (parameterTypes[0].isArray()) {
! //        if (value instanceof String) {
! //            String values[] = new String[1];
! //            values[0] = (String) value;
! //            parameters[0] = convert((String[]) values,
! //                        parameterTypes[0]);
! //        } else {
! //            parameters[0] = convert((String[]) value,
! //                        parameterTypes[0]);
! //        }
! //        } else {
! //        if (value instanceof String) {
! //            parameters[0] = convert((String) value,
! //                        parameterTypes[0]);
! //        } else {
! //            parameters[0] = convert( ((String[]) value)[0],
! //                         parameterTypes[0]);
! //        }
! //        }
! //
! //        // Invoke the setter method
! //        setter.invoke(bean, parameters);

     }

      }
***************
*** 868,871 ****
      }


! }
--- 872,875 ----
      }


! }
diff -wc cvssrc/PropertyUtils.java ./PropertyUtils.java
*** cvssrc/PropertyUtils.java Wed Oct 18 10:45:54 2000
--- ./PropertyUtils.java Tue Oct 17 15:19:10 2000
***************
*** 705,714 ****
--- 705,716 ----
         if (delim < 0)
          break;
         String next = name.substring(0, delim);
+
         if (next.indexOf(INDEXED_DELIM) >= 0)
          bean = getIndexedProperty(bean, next);
         else
          bean = getSimpleProperty(bean, next);
+
          if (bean == null)
          throw new IllegalArgumentException
              ("Null property value for '" +
***************
*** 773,792 ****
     // Retrieve the property setter method for the specified property
     PropertyDescriptor descriptor =
         getPropertyDescriptor(bean, name);
     if (descriptor == null)
!        throw new NoSuchMethodException("Unknown property '" +
!                            name + "'");
     Method writeMethod = descriptor.getWriteMethod();
     if (writeMethod == null)
!        throw new NoSuchMethodException("Property '" + name +
!                            "' has no setter method");

-    // Call the property setter method
-    Object values[] = new Object[1];
-    values[0] = value;
-    writeMethod.invoke(bean, values);

      }


! }
--- 775,832 ----
      // Retrieve the property setter method for the specified property
     PropertyDescriptor descriptor =
         getPropertyDescriptor(bean, name);
+
          if (descriptor == null)
!     {
!         return;
! //        throw new NoSuchMethodException("Unknown property '" +
! //                        name + "'");
!     }
!
      Method writeMethod = descriptor.getWriteMethod();
     if (writeMethod == null)
!     {
!         return;
! //        throw new NoSuchMethodException("Property '" + name +
! //                        "' has no setter method");
!     }


+     Class[] parameterTypes = writeMethod.getParameterTypes();
+     if (parameterTypes.length != 1) {
+         return;
      }

+     // Convert the parameter value as required for this setter method
+     Object parameters[] = new Object[1];
+     if (parameterTypes[0].isArray()) {
+         if (value instanceof String) {
+             String values[] = new String[1];
+             values[0] = (String) value;
+             parameters[0] = BeanUtils.convert((String[]) values,
+                         parameterTypes[0]);
+         } else {
+             parameters[0] = BeanUtils.convert((String[]) value,
+                         parameterTypes[0]);
+         }
+     } else {
+         if (value instanceof String) {
+             parameters[0] = BeanUtils.convert((String) value,
+                         parameterTypes[0]);
+         } else {
+             parameters[0] = BeanUtils.convert( ((String[]) value)[0],
+                          parameterTypes[0]);
+         }
+     }
+
+     // Invoke the setter method
+     writeMethod.invoke(bean, parameters);
+
+     // Call the property setter method
+ //    Object values[] = new Object[1];
+ //    values[0] = value;
+ //    writeMethod.invoke(bean, values);
+     }
+

! }



This communication is for informational purposes only.  It is not intended as
an offer or solicitation for the purchase or sale of any financial instrument
or as an official confirmation of any transaction. All market prices, data
and other information are not warranted as to completeness or accuracy and
are subject to change without notice. Any comments or statements made herein
do not necessarily reflect those of J.P. Morgan & Co. Incorporated, its
subsidiaries and affiliates.