@Test public void instantiatesClassWithZeroArgumentConstructor() { MyInterface instance = ClassFactories.newInstance(MyClass.class.getName(), MyInterface.class); assertThat(instance, is(instanceOf(MyClass.class))); }
public static <T> T newInstance(String className, Class<T> type) { try { Object instance = classForName(className).newInstance(); return type.cast(instance); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(format("No such class '%s'", className)); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } }
ClassFactories { public static <T> T newInstance(String className, Class<T> type) { try { Object instance = classForName(className).newInstance(); return type.cast(instance); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(format("No such class '%s'", className)); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } }
ClassFactories { public static <T> T newInstance(String className, Class<T> type) { try { Object instance = classForName(className).newInstance(); return type.cast(instance); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(format("No such class '%s'", className)); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } private  ClassFactories(); }
ClassFactories { public static <T> T newInstance(String className, Class<T> type) { try { Object instance = classForName(className).newInstance(); return type.cast(instance); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(format("No such class '%s'", className)); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } private  ClassFactories(); static T newInstance(String className, Class<T> type); }
ClassFactories { public static <T> T newInstance(String className, Class<T> type) { try { Object instance = classForName(className).newInstance(); return type.cast(instance); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(format("No such class '%s'", className)); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } private  ClassFactories(); static T newInstance(String className, Class<T> type);  }