You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by th...@apache.org on 2003/03/16 13:04:32 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/util ConstructorHelper.java

thma        2003/03/16 04:04:32

  Modified:    src/java/org/apache/ojb/broker/accesslayer
                        RowReaderDefaultImpl.java PkEnumeration.java
               src/java/org/apache/ojb/broker/util ConstructorHelper.java
  Log:
  performance improvement for no-argument constructor usage
  
  Revision  Changes    Path
  1.17      +15 -9     db-ojb/src/java/org/apache/ojb/broker/accesslayer/RowReaderDefaultImpl.java
  
  Index: RowReaderDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/RowReaderDefaultImpl.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- RowReaderDefaultImpl.java	31 Jan 2003 16:58:06 -0000	1.16
  +++ RowReaderDefaultImpl.java	16 Mar 2003 12:04:32 -0000	1.17
  @@ -56,6 +56,7 @@
   
   import java.lang.reflect.Constructor;
   import java.lang.reflect.InvocationTargetException;
  +import java.lang.reflect.Method;
   import java.sql.ResultSet;
   import java.sql.SQLException;
   import java.util.Map;
  @@ -76,9 +77,6 @@
   {
       private ClassDescriptor cld;
   
  -//    public RowReaderDefaultImpl()
  -//    {
  -//    }
   
       public RowReaderDefaultImpl(ClassDescriptor cld)
       {
  @@ -225,11 +223,10 @@
           Object result;
           FieldDescriptor fmd;
           try
  -        {
  -
  -            // 1. create an empty Object (persistent classes need a public default constructor)
  -            Class c = targetClassDescriptor.getClassOfObject();
  -			result = ConstructorHelper.instantiate(c);
  +        {			
  +			// 1. create an empty Object (persistent classes need a public default constructor)
  +            Constructor con = targetClassDescriptor.getZeroArgumentConstructor();
  +            result = ConstructorHelper.instantiate(con);
   			
               // 2. fill all scalar attributes of the new object
               FieldDescriptor[] fields = targetClassDescriptor.getFieldDescriptions();
  @@ -238,6 +235,15 @@
                   fmd = fields[i];
                   fmd.getPersistentField().set(result, row.get(fmd.getColumnName()));
               }
  +            
  +            // 3. invoke the initialization method for the class if one is provided
  +            Method initializationMethod = targetClassDescriptor.getInitializationMethod();
  +            Object[] noArgs = {};
  +            if (initializationMethod != null)
  +            {
  +                initializationMethod.invoke(result, noArgs);
  +            }
  +             
               return result;
           }
           catch (Exception ex)
  
  
  
  1.10      +4 -3      db-ojb/src/java/org/apache/ojb/broker/accesslayer/PkEnumeration.java
  
  Index: PkEnumeration.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/PkEnumeration.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- PkEnumeration.java	9 Mar 2003 17:10:48 -0000	1.9
  +++ PkEnumeration.java	16 Mar 2003 12:04:32 -0000	1.10
  @@ -145,8 +145,9 @@
           try
           {
               // 1. get an empty instance of the target class
  -            Class c = classDescriptor.getClassOfObject();
  -            Object obj = ConstructorHelper.instantiate(c);
  +            Constructor con = classDescriptor.getZeroArgumentConstructor();
  +            Object obj = ConstructorHelper.instantiate(con);
  +            
               // 2. fill only primary key values from Resultset
               Object colValue;
               FieldDescriptor fld;
  
  
  
  1.2       +40 -1     db-ojb/src/java/org/apache/ojb/broker/util/ConstructorHelper.java
  
  Index: ConstructorHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/ConstructorHelper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ConstructorHelper.java	26 Jan 2003 12:30:10 -0000	1.1
  +++ ConstructorHelper.java	16 Mar 2003 12:04:32 -0000	1.2
  @@ -58,14 +58,19 @@
   import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
   
   /**
  - * This class helps us to constuct new instances.
  + * This class helps us to construct new instances.
    * We don't want to rely on public default constructors and
    * have to try hard to also use private or protected constructors.
    * 
    * @author Thomas Mahler
  + * @author Lance Eason
    */
   public class ConstructorHelper
   {
  +	/**
  +	 * represents a zero sized parameter array
  +	 */
  +	private static final Object[] NO_ARGS = {};
   
       /**
        * no public constructor, please use the static method only.
  @@ -127,5 +132,39 @@
           }
           return result;
       }
  +
  +    /**
  +     * create a new instance of the class represented by the no-argument constructor provided
  +     * @param constructor the zero argument constructor for the class
  +     * @return a new instance of the class
  +     * @throws InstantiationException
  +     * @throws ClassNotPersistenceCapableException if the constructor is null or there is an
  +     *   exception while trying to create a new instance
  +     */
  +    public static Object instantiate(Constructor constructor) throws InstantiationException
  +    {
  +        if (constructor == null)
  +        {
  +            throw new ClassNotPersistenceCapableException(
  +                "A zero argument constructor was not provided!");
  +        }
  +  
  +        Object result = null;
  +  
  +        try
  +        {
  +            result = constructor.newInstance(NO_ARGS);
  +        }
  +        catch (Throwable t)
  +        {
  +            throw new ClassNotPersistenceCapableException(
  +                "Could not instantiate "
  +                + constructor.getDeclaringClass().getName()
  +                + ": " 
  +                + t.getMessage() + ")");
  +        }
  +  
  +       return result;
  +   }
   
   }