Home » commons-dbcp-1.2.2-src » org.apache.commons » jocl » [javadoc | source]
    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: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
   28    */
   29   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           if(null == type || null == argTypes) {
   42               throw new NullPointerException();
   43           }
   44           Constructor ctor = null;
   45           try {
   46               ctor = type.getConstructor(argTypes);
   47           } catch(Exception e) {
   48               ctor = null;
   49           }
   50           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               Constructor[] ctors = type.getConstructors();
   56               for(int i=0;i<ctors.length;i++) {
   57                   Class[] paramtypes = ctors[i].getParameterTypes();
   58                   if(paramtypes.length == argTypes.length) {
   59                       boolean canuse = true;
   60                       for(int j=0;j<paramtypes.length;j++) {
   61                           if(paramtypes[j].isAssignableFrom(argTypes[j])) {
   62                               continue;
   63                           } else {
   64                               canuse = false;
   65                               break;
   66                           }
   67                       }
   68                       if(canuse == true) {
   69                           ctor = ctors[i];
   70                           break;
   71                       }
   72                   }
   73               }
   74           }
   75           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           return ConstructorUtil.getConstructor(type,argTypes).newInstance(argValues);
   95       }
   96   }
   97   
   98   

Save This Page
Home » commons-dbcp-1.2.2-src » org.apache.commons » jocl » [javadoc | source]