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:11:50 GMT
dgraham     2003/10/14 21:11:50

  Modified:    dbutils/src/java/org/apache/commons/dbutils DbUtils.java
  Log:
  Refactored PropertyDescriptor retrieval into new propertyDescriptors()
  method.
  
  Revision  Changes    Path
  1.30      +57 -46    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.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- DbUtils.java	15 Oct 2003 03:51:05 -0000	1.29
  +++ DbUtils.java	15 Oct 2003 04:11:50 -0000	1.30
  @@ -81,6 +81,14 @@
   import java.util.List;
   import java.util.Map;
   
  +/**
  + * A collection of JDBC helper methods.
  + * 
  + * @author Henri Yandell
  + * @author Juozas Baliuka
  + * @author Steven Caswell
  + * @author David Graham
  + */
   public final class DbUtils {
       
       private static Map DEFAULTS = new HashMap();
  @@ -344,48 +352,58 @@
       }
       
       /**
  -     * Create an Bean from a ResultSet.
  +     * Create a Bean from a ResultSet.
        * It is assumed that next() has already been called on the ResultSet.
        */
  -    
  -    public static Object resultSetToBean(ResultSet rs, Object obj) throws SQLException
{
  -        
  -        try{
  -            
  -            java.beans.BeanInfo bInfo = java.beans.Introspector.getBeanInfo(
  -            obj.getClass());
  -            java.beans.PropertyDescriptor pd[]  = bInfo.getPropertyDescriptors();
  -            
  -            ResultSetMetaData rsmd = rs.getMetaData();
  -            int cnt = rsmd.getColumnCount();
  -            LOOP:
  -                for( int i = 1; i <= cnt; i++ ){
  -                    String name = rsmd.getColumnName(i);
  -                    for( int j = 0; j < pd.length; j++ ){
  -                        if(name.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);
  -                            }
  -                            
  -                        }
  +    public static Object resultSetToBean(ResultSet rs, Object obj)
  +        throws SQLException {
  +
  +        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);
  +            for (int j = 0; j < pd.length; j++) {
  +                if (name.equals(pd[j].getName())) {
  +                    Object value = rs.getObject(i);
  +                    if (rs.wasNull() && pd[j].getPropertyType().isPrimitive())
{
  +                        value = DEFAULTS.get(pd[j].getPropertyType());
                       }
  -                    throw new SQLException(name + " not found in " + obj.getClass().getName()
);
  +                    try {
  +
  +                        pd[j].getWriteMethod().invoke(obj, new Object[] { value });
  +                        continue LOOP;
  +
  +                    } catch (Exception e) {
  +                        throw new DbException("can not set " + name, e);
  +                    }
  +
                   }
  -        }catch(java.beans.IntrospectionException ie){
  -            throw new DbException(ie);
  +            }
  +            throw new SQLException(
  +                name + " not found in " + obj.getClass().getName());
           }
  -        
  +
           return obj;
       }
  +
  +    /**
  +     * Returns a PropertyDescriptor[] for the given Class.
  +     * @param c The Class to retrieve PropertyDescriptors for.
  +     * @return A PropertyDescriptor[] describing the Class.
  +     * @throws DbException if introspection failed.
  +     */
  +    private static PropertyDescriptor[] propertyDescriptors(Class c) {
  +        BeanInfo beanInfo = null;
  +        try {
  +            beanInfo = Introspector.getBeanInfo(c);
  +        } catch (IntrospectionException e) {
  +            throw new DbException(e);
  +        }
  +        
  +        return beanInfo.getPropertyDescriptors();
  +    }
       
       /**
        * Optimized for collections of beans.
  @@ -399,14 +417,7 @@
               return results;
           }
   
  -        BeanInfo beanInfo = null;
  -        try {
  -            beanInfo = Introspector.getBeanInfo(cls);
  -        } catch (IntrospectionException ie) {
  -            throw new DbException(ie);
  -        }
  -
  -        PropertyDescriptor pd[] = beanInfo.getPropertyDescriptors();
  +        PropertyDescriptor[] pd = propertyDescriptors(cls);
           ResultSetMetaData rsmd = rs.getMetaData();
           int cnt = rsmd.getColumnCount();
           int nameToIndex[] = new int[cnt + 1];
  
  
  

---------------------------------------------------------------------
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