| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ConstructorUtil |
|
| 6.0;6 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. 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 | package org.apache.commons.jocl; | |
| 19 | ||
| 20 | import java.lang.reflect.Constructor; | |
| 21 | import java.lang.reflect.InvocationTargetException; | |
| 22 | ||
| 23 | /** | |
| 24 | * Miscellaneous {@link Constructor} related utility functions. | |
| 25 | * | |
| 26 | * @author Rodney Waldhoff | |
| 27 | * @version $Revision: 1023401 $ $Date: 2010-10-16 21:54:24 -0400 (Sat, 16 Oct 2010) $ | |
| 28 | */ | |
| 29 | 0 | public class ConstructorUtil { |
| 30 | /** | |
| 31 | * Returns a {@link Constructor} for the given method signature, or <tt>null</tt> | |
| 32 | * if no such <tt>Constructor</tt> can be found. | |
| 33 | * | |
| 34 | * @param type the (non-<tt>null</tt>) type of {@link Object} the returned {@link Constructor} should create | |
| 35 | * @param argTypes a non-<tt>null</tt> array of types describing the parameters to the {@link Constructor}. | |
| 36 | * @return a {@link Constructor} for the given method signature, or <tt>null</tt> | |
| 37 | * if no such <tt>Constructor</tt> can be found. | |
| 38 | * @see #invokeConstructor | |
| 39 | */ | |
| 40 | public static Constructor getConstructor(Class type, Class[] argTypes) { | |
| 41 | 42 | if(null == type || null == argTypes) { |
| 42 | 0 | throw new NullPointerException(); |
| 43 | } | |
| 44 | 42 | Constructor ctor = null; |
| 45 | try { | |
| 46 | 42 | ctor = type.getConstructor(argTypes); |
| 47 | 12 | } catch(Exception e) { |
| 48 | 12 | ctor = null; |
| 49 | 30 | } |
| 50 | 42 | if(null == ctor) { |
| 51 | // no directly declared matching constructor, | |
| 52 | // look for something that will work | |
| 53 | // XXX this should really be more careful to | |
| 54 | // adhere to the jls mechanism for late binding | |
| 55 | 12 | Constructor[] ctors = type.getConstructors(); |
| 56 | 18 | for(int i=0;i<ctors.length;i++) { |
| 57 | 18 | Class[] paramtypes = ctors[i].getParameterTypes(); |
| 58 | 18 | if(paramtypes.length == argTypes.length) { |
| 59 | 12 | boolean canuse = true; |
| 60 | 72 | for(int j=0;j<paramtypes.length;j++) { |
| 61 | 60 | if(paramtypes[j].isAssignableFrom(argTypes[j])) { |
| 62 | 60 | continue; |
| 63 | } else { | |
| 64 | 0 | canuse = false; |
| 65 | 0 | break; |
| 66 | } | |
| 67 | } | |
| 68 | 12 | if(canuse == true) { |
| 69 | 12 | ctor = ctors[i]; |
| 70 | 12 | break; |
| 71 | } | |
| 72 | } | |
| 73 | } | |
| 74 | } | |
| 75 | 42 | return ctor; |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Creates a new instance of the specified <tt><i>type</i></tt> | |
| 80 | * using a {@link Constructor} described by the given parameter types | |
| 81 | * and values. | |
| 82 | * | |
| 83 | * @param type the type of {@link Object} to be created | |
| 84 | * @param argTypes a non-<tt>null</tt> array of types describing the parameters to the {@link Constructor}. | |
| 85 | * @param argValues a non-<tt>null</tt> array containing the values of the parameters to the {@link Constructor}. | |
| 86 | * @return a new instance of the specified <tt><i>type</i></tt> | |
| 87 | * using a {@link Constructor} described by the given parameter types | |
| 88 | * and values. | |
| 89 | * @exception InstantiationException | |
| 90 | * @exception IllegalAccessException | |
| 91 | * @exception InvocationTargetException | |
| 92 | */ | |
| 93 | public static Object invokeConstructor(Class type, Class[] argTypes, Object[] argValues) throws InstantiationException, IllegalAccessException, InvocationTargetException { | |
| 94 | 42 | return ConstructorUtil.getConstructor(type,argTypes).newInstance(argValues); |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 |