KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jocl > ConstructorUtil


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.jocl;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21
22 /**
23  * Miscellaneous {@link Constructor} related utility functions.
24  *
25  * @author Rodney Waldhoff
26  * @version $Revision: 1.6 $ $Date: 2004/02/28 12:18:18 $
27  */

28 public class ConstructorUtil {
29     /**
30      * Returns a {@link Constructor} for the given method signature, or <tt>null</tt>
31      * if no such <tt>Constructor</tt> can be found.
32      *
33      * @param type the (non-<tt>null</tt>) type of {@link Object} the returned {@link Constructor} should create
34      * @param argTypes a non-<tt>null</tt> array of types describing the parameters to the {@link Constructor}.
35      * @return a {@link Constructor} for the given method signature, or <tt>null</tt>
36      * if no such <tt>Constructor</tt> can be found.
37      * @see #invokeConstructor
38      */

39     public static Constructor JavaDoc getConstructor(Class JavaDoc type, Class JavaDoc[] argTypes) {
40         if(null == type || null == argTypes) {
41             throw new NullPointerException JavaDoc();
42         }
43         Constructor JavaDoc ctor = null;
44         try {
45             ctor = type.getConstructor(argTypes);
46         } catch(Exception JavaDoc e) {
47             ctor = null;
48         }
49         if(null == ctor) {
50             // no directly declared matching constructor,
51
// look for something that will work
52
// XXX this should really be more careful to
53
// adhere to the jls mechanism for late binding
54
Constructor JavaDoc[] ctors = type.getConstructors();
55             for(int i=0;i<ctors.length;i++) {
56                 Class JavaDoc[] paramtypes = ctors[i].getParameterTypes();
57                 if(paramtypes.length == argTypes.length) {
58                     boolean canuse = true;
59                     for(int j=0;j<paramtypes.length;j++) {
60                         if(paramtypes[j].isAssignableFrom(argTypes[j])) {
61                             continue;
62                         } else {
63                             canuse = false;
64                             break;
65                         }
66                     }
67                     if(canuse == true) {
68                         ctor = ctors[i];
69                         break;
70                     }
71                 }
72             }
73         }
74         return ctor;
75     }
76
77     /**
78      * Creates a new instance of the specified <tt><i>type</i></tt>
79      * using a {@link Constructor} described by the given parameter types
80      * and values.
81      *
82      * @param type the type of {@link Object} to be created
83      * @param argTypes a non-<tt>null</tt> array of types describing the parameters to the {@link Constructor}.
84      * @param argValues a non-<tt>null</tt> array containing the values of the parameters to the {@link Constructor}.
85      * @return a new instance of the specified <tt><i>type</i></tt>
86      * using a {@link Constructor} described by the given parameter types
87      * and values.
88      * @exception InstantiationException
89      * @exception IllegalAccessException
90      * @exception InvocationTargetException
91      */

92     public static Object JavaDoc invokeConstructor(Class JavaDoc type, Class JavaDoc[] argTypes, Object JavaDoc[] argValues) throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
93         return ConstructorUtil.getConstructor(type,argTypes).newInstance(argValues);
94     }
95 }
96
97
98
Popular Tags