Reflections { public static Constructor constructor(Class clazz, Class<?>... params) throws NoSuchMethodException { List<String> paramClassNames = new ArrayList<>(); for (Class<?> c : params) { paramClassNames.add(c.getCanonicalName()); } final String fullName = clazz.getCanonicalName() + "(" + join("+", paramClassNames) + ")"; if (!constructors.containsKey(fullName)) { Constructor constructor = clazz.getConstructor(params); constructors.put(fullName, constructor); } return constructors.get(fullName); }  static Method method(Class clazz, String name, Class<?>... params); static Constructor constructor(Class clazz, Class<?>... params);  }
@Test public void test_constructor_no_params() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { SomeModel byHandModel = new SomeModel(); Constructor constructor = Reflections.constructor(SomeModel.class); SomeModel autoModel = (SomeModel) constructor.newInstance(); assertEquals(byHandModel, autoModel); Constructor constructor2 = Reflections.constructor(SomeModel.class); assertEquals(constructor, constructor2); }
@Test public void test_constructor_with_params() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { SomeModel byHandModel = new SomeModel(1); Constructor constructor = Reflections.constructor(SomeModel.class, int.class); SomeModel autoModel = (SomeModel) constructor.newInstance(1); assertEquals(byHandModel, autoModel); Reflections.constructor(SomeModel.class); Constructor constructor2 = Reflections.constructor(SomeModel.class, int.class); assertEquals(constructor, constructor2); }