Here you can find the source of getConstructor(final Class
public static <T> Constructor<T> getConstructor(final Class<T> clazz, final Object... constructorArgs)
//package com.java2s; //License from project: Apache License import java.lang.reflect.Constructor; import java.util.Arrays; public class Main { public static <T> Constructor<T> getConstructor(final Class<T> clazz, final Object... constructorArgs) { final Constructor<T> constructor = getStrictConstructor(clazz, constructorArgs); return constructor == null ? findCompatibleConstructor(clazz, constructorArgs) : constructor; }// w ww . ja va 2 s . com public static <T> Constructor<T> getStrictConstructor(final Class<T> clazz, final Object... constructorArgs) { final Class<?>[] argsTypes = objsToClasses(constructorArgs); try { return clazz.getConstructor(argsTypes); } catch (final NoSuchMethodException e) { e.printStackTrace(); } catch (final SecurityException e) { e.printStackTrace(); } return null; } public static <T> Constructor<T> findCompatibleConstructor(final Class<T> clazz, final Object... constructorArgs) { @SuppressWarnings("unchecked") final Constructor<T>[] constructors = (Constructor<T>[]) clazz.getConstructors(); final Class<?>[] argsTypes = objsToClasses(constructorArgs); Constructor<T> c = null; for (final Constructor<T> constructor : constructors) { final Class<?>[] parametersTypes = constructor.getParameterTypes(); if (parametersTypes.length == argsTypes.length) { if (Arrays.equals(parametersTypes, argsTypes)) { return constructor; } if (checkAllAssignable(parametersTypes, constructorArgs)) { c = constructor; } } } return c; } public static Class<?>[] objsToClasses(final Object... objs) { final Class<?>[] types = new Class<?>[objs.length]; int index = 0; for (final Object o : objs) { types[index] = o.getClass(); ++index; } return types; } public static boolean checkAllAssignable(final Class<?>[] superClasses, final Class<?>... subClasses) { if (superClasses.length != subClasses.length) { return false; } int index = 0; for (final Class<?> c : superClasses) { if (!c.isAssignableFrom(subClasses[index])) { return false; } ++index; } return true; } public static boolean checkAllAssignable(final Class<?>[] superClasses, final Object... objects) { return checkAllAssignable(superClasses, objsToClasses(objects)); } public static Class<?> getClass(final String className) { try { return Class.forName(className); } catch (final ClassNotFoundException e) { e.printStackTrace(); } return null; } }