You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2003/03/04 20:30:22 UTC

cvs commit: jakarta-commons/beanutils/src/java/org/apache/commons/beanutils PropertyUtilsBean.java

rdonkin     2003/03/04 11:30:22

  Modified:    beanutils/src/java/org/apache/commons/beanutils
                        PropertyUtilsBean.java
  Log:
  Improved exception reporting. IllegalArgumentExceptions thrown by method invocation are now caught and rethrown with more information. Suggested by Tim Sawyer.
  
  Revision  Changes    Path
  1.2       +41 -14    jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java
  
  Index: PropertyUtilsBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PropertyUtilsBean.java	3 Mar 2003 22:33:46 -0000	1.1
  +++ PropertyUtilsBean.java	4 Mar 2003 19:30:22 -0000	1.2
  @@ -77,6 +77,8 @@
   import java.util.Map;
   
   import org.apache.commons.collections.FastHashMap;
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
   
   
   /**
  @@ -155,6 +157,9 @@
       private FastHashMap descriptorsCache = null;
       private FastHashMap mappedDescriptorsCache = null;
       
  +    /** Log instance */
  +    private Log log = LogFactory.getLog(PropertyUtils.class);
  +    
       // ---------------------------------------------------------- Constructors
       
       /** Base constructor */
  @@ -444,7 +449,7 @@
                   Object subscript[] = new Object[1];
                   subscript[0] = new Integer(index);
                   try {
  -                    return (readMethod.invoke(bean, subscript));
  +                    return (invokeMethod(readMethod,bean, subscript));
                   } catch (InvocationTargetException e) {
                       if (e.getTargetException() instanceof
                               ArrayIndexOutOfBoundsException) {
  @@ -465,7 +470,7 @@
           }
   
           // Call the property getter and return the value
  -        Object value = readMethod.invoke(bean, new Object[0]);
  +        Object value = invokeMethod(readMethod, bean, new Object[0]);
           if (!value.getClass().isArray()) {
               if (!(value instanceof java.util.List)) {
                   throw new IllegalArgumentException("Property '" + name
  @@ -586,7 +591,7 @@
               if (readMethod != null) {
                   Object keyArray[] = new Object[1];
                   keyArray[0] = key;
  -                result = readMethod.invoke(bean, keyArray);
  +                result = invokeMethod(readMethod, bean, keyArray);
               } else {
                   throw new NoSuchMethodException("Property '" + name +
                           "' has no mapped getter method");
  @@ -595,7 +600,7 @@
             /* means that the result has to be retrieved from a map */
             Method readMethod = descriptor.getReadMethod();
             if (readMethod != null) {
  -            Object invokeResult = readMethod.invoke(bean, new Object[0]);
  +            Object invokeResult = invokeMethod(readMethod, bean, new Object[0]);
               /* test and fetch from the map */
               if (invokeResult instanceof java.util.Map) {
                 result = ((java.util.Map)invokeResult).get(key);
  @@ -1136,7 +1141,7 @@
           }
   
           // Call the property getter and return the value
  -        Object value = readMethod.invoke(bean, new Object[0]);
  +        Object value = invokeMethod(readMethod, bean, new Object[0]);
           return (value);
   
       }
  @@ -1386,7 +1391,7 @@
                   subscript[0] = new Integer(index);
                   subscript[1] = value;
                   try {
  -                    writeMethod.invoke(bean, subscript);
  +                    invokeMethod(writeMethod, bean, subscript);
                   } catch (InvocationTargetException e) {
                       if (e.getTargetException() instanceof
                               ArrayIndexOutOfBoundsException) {
  @@ -1408,7 +1413,7 @@
           }
   
           // Call the property getter to get the array or list
  -        Object array = readMethod.invoke(bean, new Object[0]);
  +        Object array = invokeMethod(readMethod, bean, new Object[0]);
           if (!array.getClass().isArray()) {
               if (array instanceof List) {
                   // Modify the specified value in the List
  @@ -1534,7 +1539,7 @@
                   Object params[] = new Object[2];
                   params[0] = key;
                   params[1] = value;
  -                mappedWriteMethod.invoke(bean, params);
  +                invokeMethod(mappedWriteMethod, bean, params);
               } else {
                   throw new NoSuchMethodException
                           ("Property '" + name +
  @@ -1544,7 +1549,7 @@
             /* means that the result has to be retrieved from a map */
             Method readMethod = descriptor.getReadMethod();
             if (readMethod != null) {
  -            Object invokeResult = readMethod.invoke(bean, new Object[0]);
  +            Object invokeResult = invokeMethod(readMethod, bean, new Object[0]);
               /* test and fetch from the map */
               if (invokeResult instanceof java.util.Map) {
                 ((java.util.Map)invokeResult).put(key, value);
  @@ -1740,7 +1745,29 @@
           // Call the property setter method
           Object values[] = new Object[1];
           values[0] = value;
  -        writeMethod.invoke(bean, values);
  +        invokeMethod(writeMethod, bean, values);
   
  +    }
  +    
  +    /** This just catches and wraps IllegalArgumentException. */
  +    private Object invokeMethod(
  +                        Method method, 
  +                        Object bean, 
  +                        Object[] values) 
  +                            throws
  +                                IllegalAccessException,
  +                                InvocationTargetException {
  +        try {
  +            
  +            return method.invoke(bean, values);
  +        
  +        } catch (IllegalArgumentException e) {
  +            
  +            log.error("Method invocation failed.", e);
  +            throw new IllegalArgumentException(
  +                "Cannot invoke " + method.getDeclaringClass().getName() + "." 
  +                + method.getName() + " - " + e.getMessage());
  +            
  +        }
       }
   }
  
  
  

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