001 package org.apache.ojb.broker.util;
002
003 /* Copyright 2003-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import java.lang.reflect.Constructor;
019
020 import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
021
022 /**
023 * This class helps us to construct new instances.
024 * We don't want to rely on public default constructors and
025 * have to try hard to also use private or protected constructors.
026 *
027 * @author Thomas Mahler
028 * @author Lance Eason
029 *
030 * @version $Id: ConstructorHelper.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $
031 */
032 public class ConstructorHelper
033 {
034 /**
035 * represents a zero sized parameter array
036 */
037 private static final Object[] NO_ARGS = {};
038
039 /**
040 * no public constructor, please use the static method only.
041 */
042 private ConstructorHelper()
043 {
044 }
045
046 /**
047 * create a new instance of class clazz.
048 * first use the public default constructor.
049 * If this fails also try to use protected an private constructors.
050 * @param clazz the class to instantiate
051 * @return the fresh instance of class clazz
052 * @throws InstantiationException
053 */
054 public static Object instantiate(Class clazz) throws InstantiationException
055 {
056 Object result = null;
057 try
058 {
059 result = ClassHelper.newInstance(clazz);
060 }
061 catch(IllegalAccessException e)
062 {
063 try
064 {
065 result = ClassHelper.newInstance(clazz, true);
066 }
067 catch(Exception e1)
068 {
069 throw new ClassNotPersistenceCapableException("Can't instantiate class '"
070 + (clazz != null ? clazz.getName() : "null")
071 + "', message was: " + e1.getMessage() + ")", e1);
072 }
073 }
074 return result;
075 }
076
077 /**
078 * create a new instance of the class represented by the no-argument constructor provided
079 * @param constructor the zero argument constructor for the class
080 * @return a new instance of the class
081 * @throws InstantiationException
082 * @throws ClassNotPersistenceCapableException if the constructor is null or there is an
083 * exception while trying to create a new instance
084 */
085 public static Object instantiate(Constructor constructor) throws InstantiationException
086 {
087 if(constructor == null)
088 {
089 throw new ClassNotPersistenceCapableException(
090 "A zero argument constructor was not provided!");
091 }
092
093 Object result = null;
094 try
095 {
096 result = constructor.newInstance(NO_ARGS);
097 }
098 catch(InstantiationException e)
099 {
100 throw e;
101 }
102 catch(Exception e)
103 {
104 throw new ClassNotPersistenceCapableException("Can't instantiate class '"
105 + (constructor != null ? constructor.getDeclaringClass().getName() : "null")
106 + "' with given constructor: " + e.getMessage(), e);
107 }
108 return result;
109 }
110 }