ClassLoaderUtil { public static <T> T newInstance(String className) throws ClassNotFoundException, ClassInstantiationException { Class<T> clazz = loadClass(className); return newInstance(clazz); }  static ClassLoader getContextClassLoader(); static String getClasspath(); static Class<T> loadClass(String className); static Class<T> loadClass(String className, Class<?> referrer); static Class<T> loadClass(String className, ClassLoader classLoader); static Class<T> loadServiceClass(String serviceId); static Class<T> loadServiceClass(String serviceId, Class<?> referrer); static Class<T> loadServiceClass(String serviceId, ClassLoader classLoader); static Class<T> loadServiceClass(String className, String serviceId); static Class<T> loadServiceClass(String className, String serviceId, Class<T> referrer); static Class<T> loadServiceClass(String className, String serviceId, ClassLoader classLoader); static T newInstance(String className); static Object newInstance(String className, Class<?> referrer); static Object newInstance(String className, ClassLoader classLoader); static T newInstance(Class<T> clazz); static T newServiceInstance(String serviceId); static T newServiceInstance(String serviceId, Class<T> referrer); static T newServiceInstance(String serviceId, ClassLoader classLoader); static T newServiceInstance(String className, String serviceId); static T newServiceInstance(String className, String serviceId, Class<T> referrer); static T newServiceInstance(String className, String serviceId, ClassLoader classLoader); static URL[] getResources(String resourceName); static URL[] getResources(String resourceName, Class<?> referrer); static URL[] getResources(String resourceName, ClassLoader classLoader); static URL getResource(String resourceName); static URL getResource(String resourceName, Class<?> referrer); static URL getResource(String resourceName, ClassLoader classLoader); static InputStream getResourceAsStream(String resourceName); static InputStream getResourceAsStream(String resourceName, Class<?> referrer); static InputStream getResourceAsStream(String resourceName, ClassLoader classLoader); static URL[] whichClasses(String className); static URL[] whichClasses(String className, Class<T> referrer); static URL[] whichClasses(String className, ClassLoader classLoader); static URL whichClass(String className); static URL whichClass(String className, Class<T> referrer); static URL whichClass(String className, ClassLoader classLoader);  }
@Test public void newInstance() throws Exception { assertNull(ClassLoaderUtil.newInstance((String) null)); assertNull(ClassLoaderUtil.newInstance((Class<?>) null)); assertInstance(String.class, ClassLoaderUtil.newInstance("java.lang.String")); try { ClassLoaderUtil.newInstance("com.baidu.unbiz.common.sample.MyErrorService"); fail("Expected ClassInstantiationException"); } catch (ClassInstantiationException e) { } assertNull(ClassLoaderUtil.newInstance(null, getClass())); assertNull(ClassLoaderUtil.newInstance(null, (Class<?>) null)); assertInstance(String.class, ClassLoaderUtil.newInstance("java.lang.String", getClass())); assertInstance(String.class, ClassLoaderUtil.newInstance("java.lang.String", (Class<?>) null)); try { ClassLoaderUtil.newInstance("com.baidu.unbiz.common.sample.MyErrorService", getClass()); fail("Expected ClassInstantiationException"); } catch (ClassInstantiationException e) { } assertNull(ClassLoaderUtil.newInstance(null, getClass().getClassLoader())); assertNull(ClassLoaderUtil.newInstance(null, (ClassLoader) null)); assertInstance(String.class, ClassLoaderUtil.newInstance("java.lang.String", getClass().getClassLoader())); assertInstance(String.class, ClassLoaderUtil.newInstance("java.lang.String", (ClassLoader) null)); try { ClassLoaderUtil.newInstance("com.baidu.unbiz.common.sample.MyErrorService", getClass().getClassLoader()); fail("Expected ClassInstantiationException"); } catch (ClassInstantiationException e) { } }