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.7.2.1 2005/12/21 22:27:47 tomdz Exp $
031: */
032: public class ConstructorHelper {
033: /**
034: * represents a zero sized parameter array
035: */
036: private static final Object[] NO_ARGS = {};
037:
038: /**
039: * no public constructor, please use the static method only.
040: */
041: private ConstructorHelper() {
042: }
043:
044: /**
045: * create a new instance of class clazz.
046: * first use the public default constructor.
047: * If this fails also try to use protected an private constructors.
048: * @param clazz the class to instantiate
049: * @return the fresh instance of class clazz
050: * @throws InstantiationException
051: */
052: public static Object instantiate(Class clazz)
053: throws InstantiationException {
054: Object result = null;
055: try {
056: result = ClassHelper.newInstance(clazz);
057: } catch (IllegalAccessException e) {
058: try {
059: result = ClassHelper.newInstance(clazz, true);
060: } catch (Exception e1) {
061: throw new ClassNotPersistenceCapableException(
062: "Can't instantiate class '"
063: + (clazz != null ? clazz.getName()
064: : "null") + "', message was: "
065: + e1.getMessage() + ")", e1);
066: }
067: }
068: return result;
069: }
070:
071: /**
072: * create a new instance of the class represented by the no-argument constructor provided
073: * @param constructor the zero argument constructor for the class
074: * @return a new instance of the class
075: * @throws InstantiationException
076: * @throws ClassNotPersistenceCapableException if the constructor is null or there is an
077: * exception while trying to create a new instance
078: */
079: public static Object instantiate(Constructor constructor)
080: throws InstantiationException {
081: if (constructor == null) {
082: throw new ClassNotPersistenceCapableException(
083: "A zero argument constructor was not provided!");
084: }
085:
086: Object result = null;
087: try {
088: result = constructor.newInstance(NO_ARGS);
089: } catch (InstantiationException e) {
090: throw e;
091: } catch (Exception e) {
092: throw new ClassNotPersistenceCapableException(
093: "Can't instantiate class '"
094: + (constructor != null ? constructor
095: .getDeclaringClass().getName()
096: : "null")
097: + "' with given constructor: "
098: + e.getMessage(), e);
099: }
100: return result;
101: }
102: }
|