ClassUtil { public static <T> T newInstance(String className, Class[] argTyps, Object[] args) { try { Class clazz = ClassUtil.class.getClassLoader().loadClass(className); Constructor constructor = clazz.getConstructor(argTyps); return (T) constructor.newInstance(args); } catch (Throwable e) { throw new IllegalStateException("new an instance of " + className + " err!", e); } } private  ClassUtil(); static T newInstance(String className, Class[] argTyps, Object[] args);  }
@Test public void testNewInstance() { ArrayList list = ClassUtil.newInstance(ArrayList.class.getName(), null, null); Assert.assertNotNull(list); Integer integer = ClassUtil.newInstance(Integer.class.getName(), new Class[] { int.class }, new Object[] { 5 }); Assert.assertEquals("5", integer.toString()); }
@Test(expected = IllegalStateException.class) public void testNewInstanceException() { ClassUtil.newInstance(ArrayList.class.getName() + "x", null, null); }