commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils DbUtils.java
Date Wed, 15 Oct 2003 04:39:27 GMT
dgraham     2003/10/14 21:39:27

  Modified:    dbutils/src/java/org/apache/commons/dbutils DbUtils.java
  Log:
  Refactored setting bean properties into new callSetter() method.
  
  Revision  Changes    Path
  1.32      +58 -33    jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbUtils.java
  
  Index: DbUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbUtils.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- DbUtils.java	15 Oct 2003 04:17:28 -0000	1.31
  +++ DbUtils.java	15 Oct 2003 04:39:27 -0000	1.32
  @@ -66,6 +66,8 @@
   import java.beans.Introspector;
   import java.beans.PropertyDescriptor;
   import java.io.PrintWriter;
  +import java.lang.reflect.InvocationTargetException;
  +import java.lang.reflect.Method;
   import java.sql.Connection;
   import java.sql.PreparedStatement;
   import java.sql.ResultSet;
  @@ -319,15 +321,20 @@
           try {
               Class.forName(name).newInstance();
               return true;
  +            
           } catch (ClassNotFoundException cnfe) {
               cnfe.printStackTrace();
               return false;
  +            
           } catch (IllegalAccessException iae) {
               iae.printStackTrace();
  -            return true; //constructor is privite, ok for DriverManager contract 
  +            // Constructor is private, OK for DriverManager contract
  +            return true;  
  +            
           } catch (InstantiationException ie) {
               ie.printStackTrace();
               return false;
  +            
           }catch(Throwable t){
               return false;
           }
  @@ -361,32 +368,56 @@
           PropertyDescriptor[] pd = propertyDescriptors(obj.getClass());
   
           ResultSetMetaData rsmd = rs.getMetaData();
  -        int cnt = rsmd.getColumnCount();
  -        LOOP : for (int i = 1; i <= cnt; i++) {
  -            String name = rsmd.getColumnName(i);
  +        int cols = rsmd.getColumnCount();
  +        LOOP : for (int i = 1; i <= cols; i++) {
  +            String columnName = rsmd.getColumnName(i);
               for (int j = 0; j < pd.length; j++) {
  -                if (name.equals(pd[j].getName())) {
  +                if (columnName.equals(pd[j].getName())) {
                       Object value = rs.getObject(i);
                       if (rs.wasNull() && pd[j].getPropertyType().isPrimitive())
{
                           value = DEFAULTS.get(pd[j].getPropertyType());
                       }
  -                    try {
  -
  -                        pd[j].getWriteMethod().invoke(obj, new Object[] { value });
  -                        continue LOOP;
  -
  -                    } catch (Exception e) {
  -                        throw new DbException("can not set " + name, e);
  -                    }
   
  +                    callSetter(pd[j], obj, value);
  +                    continue LOOP;
                   }
               }
  +            
               throw new SQLException(
  -                name + " not found in " + obj.getClass().getName());
  +                columnName + " not found in " + obj.getClass().getName());
           }
   
           return obj;
       }
  +    
  +    /**
  +     * Calls the setter method on the target object for the given property.
  +     * If no setter method exists for the property, this method does nothing.
  +     * @param pd The property to set.
  +     * @param target The object to set the property on.
  +     * @param value The value to pass into the setter.
  +     * @throws DbException if an error occurs setting the property.
  +     */
  +    private static void callSetter(
  +        PropertyDescriptor pd,
  +        Object target,
  +        Object value) {
  +
  +        try {
  +            Method setter = pd.getWriteMethod();
  +
  +            if (setter != null) {
  +                setter.invoke(target, new Object[] { value });
  +            }
  +
  +        } catch (IllegalArgumentException e) {
  +            throw new DbException("Cannot set " + pd.getName(), e);
  +        } catch (IllegalAccessException e) {
  +            throw new DbException("Cannot set " + pd.getName(), e);
  +        } catch (InvocationTargetException e) {
  +            throw new DbException("Cannot set " + pd.getName(), e);
  +        }
  +    }
   
       /**
        * Returns a PropertyDescriptor[] for the given Class.
  @@ -419,40 +450,34 @@
   
           PropertyDescriptor[] pd = propertyDescriptors(cls);
           ResultSetMetaData rsmd = rs.getMetaData();
  -        int cnt = rsmd.getColumnCount();
  -        int nameToIndex[] = new int[cnt + 1];
  +        int cols = rsmd.getColumnCount();
  +        int nameToIndex[] = new int[cols + 1];
   
  -        LOOP : for (int i = 1; i <= cnt; i++) {
  -            String name = rsmd.getColumnName(i);
  +        LOOP : for (int i = 1; i <= cols; i++) {
  +            String columnName = rsmd.getColumnName(i);
               for (int j = 0; j < pd.length; j++) {
  -                if (name.equals(pd[j].getName())) {
  +                if (columnName.equals(pd[j].getName())) {
                       nameToIndex[i] = j;
                       continue LOOP;
                   }
               }
  -            throw new SQLException(" index not found for " + name);
  +            throw new SQLException(" index not found for " + columnName);
           }
   
           do {
               Object obj = newInstance(cls);
   
  -            for (int i = 1; i <= cnt; i++) {
  +            for (int i = 1; i <= cols; i++) {
                   Object value = rs.getObject(i);
  -                int index = nameToIndex[i];
                   if (rs.wasNull()) {
                       continue;
                   }
  -                
  -                try {
  -                    pd[index].getWriteMethod().invoke(obj, new Object[] { value });
   
  -                } catch (Exception e) {
  -                    throw new DbException("can not set " + pd[index].getName(), e);
  -                }
  +                callSetter(pd[nameToIndex[i]], obj, value);
               }
  -            
  +
               results.add(obj);
  -            
  +
           } while (rs.next());
   
           return results;
  
  
  

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


Mime
View raw message