public static <T> Constructor<T> getMatchingConstructor(final Class<T> cls, final Class<?>... parameterTypes) { // see if we can find the constructor directly // most of the time this works and it's much faster try { final Constructor<T> ctor = cls.getConstructor(parameterTypes); return ctor; } catch (final NoSuchMethodException e) { } // return best match: Constructor<T> result = null; final Constructor<?>[] ctors = cls.getConstructors(); for (Constructor<?> ctor : ctors) { // compare parameters if (isAssignable(parameterTypes, ctor.getParameterTypes(), true)) { if (result == null || compareParameterTypes(ctor.getParameterTypes(), result.getParameterTypes(), parameterTypes) < 0) result = (Constructor<T>) ctor; } } return result; }