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/01/26 13:30:10 UTC

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

thma        2003/01/26 04:30:10

  Modified:    src/java/org/apache/ojb/broker/accesslayer
                        PkEnumeration.java RowReaderDefaultImpl.java
  Added:       src/java/org/apache/ojb/broker/util ConstructorHelper.java
  Log:
  add support for persistent classes with private and protected constructors only
  
  Revision  Changes    Path
  1.7       +3 -2      jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/PkEnumeration.java
  
  Index: PkEnumeration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/PkEnumeration.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PkEnumeration.java	15 Jan 2003 10:43:28 -0000	1.6
  +++ PkEnumeration.java	26 Jan 2003 12:30:09 -0000	1.7
  @@ -61,6 +61,7 @@
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.util.ConstructorHelper;
   import org.apache.ojb.broker.util.SqlHelper;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   
  @@ -146,7 +147,7 @@
           {
               // 1. get an empty instance of the target class
               Class c = classDescriptor.getClassOfObject();
  -            Object obj = c.newInstance();
  +            Object obj = ConstructorHelper.instantiate(c);
               // 2. fill only primary key values from Resultset
               Object colValue;
               FieldDescriptor fld;
  
  
  
  1.15      +5 -2      jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/RowReaderDefaultImpl.java
  
  Index: RowReaderDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/RowReaderDefaultImpl.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- RowReaderDefaultImpl.java	15 Jan 2003 10:43:28 -0000	1.14
  +++ RowReaderDefaultImpl.java	26 Jan 2003 12:30:09 -0000	1.15
  @@ -58,7 +58,9 @@
   import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
  +import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
  +import org.apache.ojb.broker.util.ConstructorHelper;
   import org.apache.ojb.broker.util.SqlHelper;
   
   import java.lang.reflect.Constructor;
  @@ -228,7 +230,8 @@
   
               // 1. create an empty Object (persistent classes need a public default constructor)
               Class c = targetClassDescriptor.getClassOfObject();
  -            result = c.newInstance();
  +			result = ConstructorHelper.instantiate(c);
  +			
               // 2. fill all scalar attributes of the new object
               FieldDescriptor[] fields = targetClassDescriptor.getFieldDescriptions();
               for (int i = 0; i < fields.length; i++)
  
  
  
  1.1                  jakarta-ojb/src/java/org/apache/ojb/broker/util/ConstructorHelper.java
  
  Index: ConstructorHelper.java
  ===================================================================
  package org.apache.ojb.broker.util;
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache ObjectRelationalBridge" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.lang.reflect.Constructor;
  
  import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
  
  /**
   * This class helps us to constuct 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
   */
  public class ConstructorHelper
  {
  
      /**
       * no public constructor, please use the static method only.
       */
      private ConstructorHelper()
      {
      }
  
  	/**
  	 * create a new instance of class clazz.
  	 * first use the public default constructor.
  	 * If this fails also try to use protected an private constructors.
  	 * @param clazz the class to instantiate
  	 * @return the fresh instance of class clazz
  	 * @throws InstantiationException
  	 */
      public static Object instantiate(Class clazz) throws InstantiationException
      {
          Object result = null;
  
          try
          {
              result = clazz.newInstance();
          }
          catch (IllegalAccessException e)
          {
              // can't access constructor, so let's use reflection
              //1. get private no args constructor
              Class[] params = {};
              Constructor constructor;
              try
              {
                  constructor = clazz.getDeclaredConstructor(params);
              }
              catch (NoSuchMethodException ex)
              {
                  throw new ClassNotPersistenceCapableException(
                      clazz.getName() + " does not provide a zero argument constructor!");
              }
  
              //2. make is accessible
              constructor.setAccessible(true);
  
              //3. use it to build a new instance
              Object[] args = {};
              try
              {
                  result = constructor.newInstance(args);
              }
              catch (Throwable t)
              {
                  throw new ClassNotPersistenceCapableException(
                      "Could not instantiate "
                          + clazz.getName()
                          + ": " 
                          + t.getMessage() + ")");
              }
  
          }
          return result;
      }
  
  }