Java Reflection Constructor Get getConstructor(Class clazz, Class[] paramTypes)

Here you can find the source of getConstructor(Class clazz, Class[] paramTypes)

Description

get Constructor

License

Open Source License

Declaration

private static Constructor getConstructor(Class clazz, Class[] paramTypes) throws NoSuchMethodException 

Method Source Code


//package com.java2s;
/*/* w  w w  . ja v a 2 s .co  m*/
 *  Copyright (C) 2013, Peter Decsi.
 * 
 *  This library is free software: you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public 
 *  License as published by the Free Software Foundation, either 
 *  version 3 of the License, or (at your option) any later version.
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.Constructor;

public class Main {
    private static Constructor getConstructor(Class clazz, Class[] paramTypes) throws NoSuchMethodException {
        for (Constructor c : clazz.getConstructors()) {
            if (isAssignable(c.getParameterTypes(), paramTypes))
                return c;
        }
        return clazz.getConstructor(paramTypes);
    }

    private static boolean isAssignable(Class[] constructor, Class[] parameters) {
        int size = parameters.length;
        if (size != constructor.length)
            return false;
        for (int i = 0; i < size; i++)
            if (!constructor[i].isAssignableFrom(parameters[i]))
                return false;
        return true;
    }
}

Related

  1. getConstructor(Class clazz, Class... argTypes)
  2. getConstructor(Class clazz, Class[] argTypes)
  3. getConstructor(Class clazz, Class[] parametersTypes)
  4. getConstructor(Class clazz, Class[] paramTypes)
  5. getConstructor(Class cls, Class[] args)
  6. getConstructor(Class clz, Class expectedTypes[])
  7. getConstructor(Class type, Class[] argTypes)