/** * Get the constructor of the concrete, dynamic wrapper class of the given abstract class * with matching argument types. * @param the argTypes of the constructor to look for. null signify default constructor. */ public static <T> Constructor<T> getConcreteConstructor(Class<T> abstractClass, Class<?>... argTypes) throws ClassNotFoundException { Class<? extends T> cls = makeConcrete(abstractClass); Constructor<?>[] constructors = cls.getConstructors(); int args = argTypes == null ? 0 : argTypes.length; for (Constructor<?> cons : constructors) { Class<?>[] params = cons.getParameterTypes(); if (params.length != args) continue; boolean match = false; for (int i = 0; i < params.length; i++) { match = params[i].isAssignableFrom(argTypes[i]); if (!match) break; } if (match) { return (Constructor<T>)cons; } } throw new RuntimeException(abstractClass + " has no constructor with " + (args == 0 ? "void" : Arrays.toString(argTypes))); }