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

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

Description

get Constructor

License

LGPL

Declaration

public static <T> Constructor<T> getConstructor(Class<T> clazz, Class[] expectedTypes)
            throws NoSuchMethodException 

Method Source Code

//package com.java2s;
/*/*www  .  j  ava 2 s .  co m*/
 * JFox - The most lightweight Java EE Application Server!
 * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
 *
 * JFox is licenced and re-distributable under GNU LGPL.
 */

import java.lang.reflect.Constructor;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
    private static Map<String, Class> primitiveStringToClass = new HashMap<String, Class>();

    public static <T> Constructor<T> getConstructor(Class<T> clazz, Class[] expectedTypes)
            throws NoSuchMethodException {
        Constructor<T> constructor = null;
        if (expectedTypes == null) {
            expectedTypes = EMPTY_CLASS_ARRAY;
        }
        try {
            constructor = clazz.getConstructor(expectedTypes);
        } catch (NoSuchMethodException e) {
            Constructor[] constructors = clazz.getConstructors();
            for (Constructor constr : constructors) {
                if (isAssignable(expectedTypes, constr.getParameterTypes())) {
                    if (constructor == null) {
                        constructor = (Constructor<T>) constr;
                    } else {
                        if (isAssignable(constr.getParameterTypes(), constructor.getParameterTypes())) {
                            constructor = (Constructor<T>) constr;
                        }
                    }
                }
            }
            if (constructor == null)
                throw e;
        }
        return constructor;
    }

    public static boolean isAssignable(Class clazz, Class toClass) {
        if (clazz == null || toClass == null) {
            return false;
        }
        if (clazz.equals(toClass)) {
            return true;
        }

        Class newClazz = clazz;
        Class newToClass = toClass;
        if (clazz.isPrimitive()) {
            newClazz = primitiveStringToClass.get(clazz.getName());
        }
        if (toClass.isPrimitive()) {
            newToClass = primitiveStringToClass.get(toClass.getName());
        }
        return newToClass.isAssignableFrom(newClazz);
    }

    public static boolean isAssignable(Class[] classArray, Class[] toClassArray) {
        if (classArray == null || toClassArray == null) {
            return false;
        }
        if (classArray.length != toClassArray.length) {
            return false;
        }

        for (int i = 0; i < classArray.length; i++) {
            if (!isAssignable(classArray[i], toClassArray[i])) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. getConstructor(Class type, Class[] givenTypes)
  2. getConstructor(Class aClass)
  3. getConstructor(Class cl, Class... params)
  4. getConstructor(Class clazz)
  5. getConstructor(Class clazz, Class... parameterTypes)
  6. getConstructor(Class cls, Class... parameterClses)
  7. getConstructor(Class cls, Class[] args)
  8. getConstructor(Class cls, Object... args)
  9. getConstructor(Class instanceType)