001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: package org.apache.commons.jocl;
019:
020: import java.lang.reflect.Constructor;
021: import java.lang.reflect.InvocationTargetException;
022:
023: /**
024: * Miscellaneous {@link Constructor} related utility functions.
025: *
026: * @author Rodney Waldhoff
027: * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
028: */
029: public class ConstructorUtil {
030: /**
031: * Returns a {@link Constructor} for the given method signature, or <tt>null</tt>
032: * if no such <tt>Constructor</tt> can be found.
033: *
034: * @param type the (non-<tt>null</tt>) type of {@link Object} the returned {@link Constructor} should create
035: * @param argTypes a non-<tt>null</tt> array of types describing the parameters to the {@link Constructor}.
036: * @return a {@link Constructor} for the given method signature, or <tt>null</tt>
037: * if no such <tt>Constructor</tt> can be found.
038: * @see #invokeConstructor
039: */
040: public static Constructor getConstructor(Class type,
041: Class[] argTypes) {
042: if (null == type || null == argTypes) {
043: throw new NullPointerException();
044: }
045: Constructor ctor = null;
046: try {
047: ctor = type.getConstructor(argTypes);
048: } catch (Exception e) {
049: ctor = null;
050: }
051: if (null == ctor) {
052: // no directly declared matching constructor,
053: // look for something that will work
054: // XXX this should really be more careful to
055: // adhere to the jls mechanism for late binding
056: Constructor[] ctors = type.getConstructors();
057: for (int i = 0; i < ctors.length; i++) {
058: Class[] paramtypes = ctors[i].getParameterTypes();
059: if (paramtypes.length == argTypes.length) {
060: boolean canuse = true;
061: for (int j = 0; j < paramtypes.length; j++) {
062: if (paramtypes[j].isAssignableFrom(argTypes[j])) {
063: continue;
064: } else {
065: canuse = false;
066: break;
067: }
068: }
069: if (canuse == true) {
070: ctor = ctors[i];
071: break;
072: }
073: }
074: }
075: }
076: return ctor;
077: }
078:
079: /**
080: * Creates a new instance of the specified <tt><i>type</i></tt>
081: * using a {@link Constructor} described by the given parameter types
082: * and values.
083: *
084: * @param type the type of {@link Object} to be created
085: * @param argTypes a non-<tt>null</tt> array of types describing the parameters to the {@link Constructor}.
086: * @param argValues a non-<tt>null</tt> array containing the values of the parameters to the {@link Constructor}.
087: * @return a new instance of the specified <tt><i>type</i></tt>
088: * using a {@link Constructor} described by the given parameter types
089: * and values.
090: * @exception InstantiationException
091: * @exception IllegalAccessException
092: * @exception InvocationTargetException
093: */
094: public static Object invokeConstructor(Class type,
095: Class[] argTypes, Object[] argValues)
096: throws InstantiationException, IllegalAccessException,
097: InvocationTargetException {
098: return ConstructorUtil.getConstructor(type, argTypes)
099: .newInstance(argValues);
100: }
101: }
|