| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ConstructorHelper |
|
| 4.666666666666667;4.667 |
| 1 | package org.apache.ojb.broker.util; | |
| 2 | ||
| 3 | /* Copyright 2003-2005 The Apache Software Foundation | |
| 4 | * | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 6 | * you may not use this file except in compliance with the License. | |
| 7 | * You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | ||
| 18 | import java.lang.reflect.Constructor; | |
| 19 | ||
| 20 | import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException; | |
| 21 | ||
| 22 | /** | |
| 23 | * This class helps us to construct new instances. | |
| 24 | * We don't want to rely on public default constructors and | |
| 25 | * have to try hard to also use private or protected constructors. | |
| 26 | * | |
| 27 | * @author Thomas Mahler | |
| 28 | * @author Lance Eason | |
| 29 | * | |
| 30 | * @version $Id: ConstructorHelper.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $ | |
| 31 | */ | |
| 32 | public class ConstructorHelper | |
| 33 | { | |
| 34 | /** | |
| 35 | * represents a zero sized parameter array | |
| 36 | */ | |
| 37 | private static final Object[] NO_ARGS = {}; | |
| 38 | ||
| 39 | /** | |
| 40 | * no public constructor, please use the static method only. | |
| 41 | */ | |
| 42 | private ConstructorHelper() | |
| 43 | { | |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * create a new instance of class clazz. | |
| 48 | * first use the public default constructor. | |
| 49 | * If this fails also try to use protected an private constructors. | |
| 50 | * @param clazz the class to instantiate | |
| 51 | * @return the fresh instance of class clazz | |
| 52 | * @throws InstantiationException | |
| 53 | */ | |
| 54 | public static Object instantiate(Class clazz) throws InstantiationException | |
| 55 | { | |
| 56 | Object result = null; | |
| 57 | try | |
| 58 | { | |
| 59 | result = ClassHelper.newInstance(clazz); | |
| 60 | } | |
| 61 | catch(IllegalAccessException e) | |
| 62 | { | |
| 63 | try | |
| 64 | { | |
| 65 | result = ClassHelper.newInstance(clazz, true); | |
| 66 | } | |
| 67 | catch(Exception e1) | |
| 68 | { | |
| 69 | throw new ClassNotPersistenceCapableException("Can't instantiate class '" | |
| 70 | + (clazz != null ? clazz.getName() : "null") | |
| 71 | + "', message was: " + e1.getMessage() + ")", e1); | |
| 72 | } | |
| 73 | } | |
| 74 | return result; | |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * create a new instance of the class represented by the no-argument constructor provided | |
| 79 | * @param constructor the zero argument constructor for the class | |
| 80 | * @return a new instance of the class | |
| 81 | * @throws InstantiationException | |
| 82 | * @throws ClassNotPersistenceCapableException if the constructor is null or there is an | |
| 83 | * exception while trying to create a new instance | |
| 84 | */ | |
| 85 | public static Object instantiate(Constructor constructor) throws InstantiationException | |
| 86 | { | |
| 87 | if(constructor == null) | |
| 88 | { | |
| 89 | throw new ClassNotPersistenceCapableException( | |
| 90 | "A zero argument constructor was not provided!"); | |
| 91 | } | |
| 92 | ||
| 93 | Object result = null; | |
| 94 | try | |
| 95 | { | |
| 96 | result = constructor.newInstance(NO_ARGS); | |
| 97 | } | |
| 98 | catch(InstantiationException e) | |
| 99 | { | |
| 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 | } |