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 10:51:48 -0500 (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, Class[] argTypes) { 041 if(null == type || null == argTypes) { 042 throw new NullPointerException(); 043 } 044 Constructor ctor = null; 045 try { 046 ctor = type.getConstructor(argTypes); 047 } catch(Exception e) { 048 ctor = null; 049 } 050 if(null == ctor) { 051 // no directly declared matching constructor, 052 // look for something that will work 053 // XXX this should really be more careful to 054 // adhere to the jls mechanism for late binding 055 Constructor[] ctors = type.getConstructors(); 056 for(int i=0;i<ctors.length;i++) { 057 Class[] paramtypes = ctors[i].getParameterTypes(); 058 if(paramtypes.length == argTypes.length) { 059 boolean canuse = true; 060 for(int j=0;j<paramtypes.length;j++) { 061 if(paramtypes[j].isAssignableFrom(argTypes[j])) { 062 continue; 063 } else { 064 canuse = false; 065 break; 066 } 067 } 068 if(canuse == true) { 069 ctor = ctors[i]; 070 break; 071 } 072 } 073 } 074 } 075 return ctor; 076 } 077 078 /** 079 * Creates a new instance of the specified <tt><i>type</i></tt> 080 * using a {@link Constructor} described by the given parameter types 081 * and values. 082 * 083 * @param type the type of {@link Object} to be created 084 * @param argTypes a non-<tt>null</tt> array of types describing the parameters to the {@link Constructor}. 085 * @param argValues a non-<tt>null</tt> array containing the values of the parameters to the {@link Constructor}. 086 * @return a new instance of the specified <tt><i>type</i></tt> 087 * using a {@link Constructor} described by the given parameter types 088 * and values. 089 * @exception InstantiationException 090 * @exception IllegalAccessException 091 * @exception InvocationTargetException 092 */ 093 public static Object invokeConstructor(Class type, Class[] argTypes, Object[] argValues) throws InstantiationException, IllegalAccessException, InvocationTargetException { 094 return ConstructorUtil.getConstructor(type,argTypes).newInstance(argValues); 095 } 096 } 097 098