ClassUtils { @SuppressWarnings("unchecked") public static <T> T createInstance(String classname, Class<T> interfaceClazz) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = interfaceClazz.getClassLoader(); } try { Class<T> clazz = (Class<T>) Class.forName(classname, true, classLoader); return clazz.newInstance(); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Unknown class: " + classname); } catch (InstantiationException | IllegalAccessException e) { throw new IllegalArgumentException("Could not create an instance of class: " + classname, e); } }  @SuppressWarnings("unchecked") static T createInstance(String classname, Class<T> interfaceClazz);  }
@Test void testCreateInstance() { CharSequence string = ClassUtils.createInstance("java.lang.String", CharSequence.class); assertNotNull(string); assertTrue(string instanceof String); assertEquals("", string); }