ClassLoaderUtil { public static <T> Class<T> loadServiceClass(String serviceId) throws ClassNotFoundException { return loadServiceClass(serviceId, getContextClassLoader()); }  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 loadServiceClass() throws Exception { assertNull(ClassLoaderUtil.loadServiceClass(null)); try { ClassLoaderUtil.loadServiceClass("myservice.notexist"); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } try { ClassLoaderUtil.loadServiceClass("myservice.notfound"); fail("expected ClassNotFoundException"); } catch (ClassNotFoundException e) { assertFalse(e instanceof ServiceNotFoundException); } assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("myservice")); assertNull(ClassLoaderUtil.loadServiceClass(null, getClass())); assertNull(ClassLoaderUtil.loadServiceClass(null, (Class<?>) null)); try { ClassLoaderUtil.loadServiceClass("myservice.notexist", getClass()); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } try { ClassLoaderUtil.loadServiceClass("myservice.notfound", getClass()); fail("expected ClassNotFoundException"); } catch (ClassNotFoundException e) { assertFalse(e instanceof ServiceNotFoundException); } try { ClassLoaderUtil.loadServiceClass("myservice.notexist", (Class<?>) null); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } try { ClassLoaderUtil.loadServiceClass("myservice.notfound", (Class<?>) null); fail("expected ClassNotFoundException"); } catch (ClassNotFoundException e) { assertFalse(e instanceof ServiceNotFoundException); } assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("myservice", getClass())); assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("myservice", (Class<?>) null)); assertNull(ClassLoaderUtil.loadServiceClass(null, getClass().getClassLoader())); assertNull(ClassLoaderUtil.loadServiceClass(null, (ClassLoader) null)); try { ClassLoaderUtil.loadServiceClass("myservice.notexist", getClass().getClassLoader()); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } try { ClassLoaderUtil.loadServiceClass("myservice.notfound", getClass().getClassLoader()); fail("expected ClassNotFoundException"); } catch (ClassNotFoundException e) { assertFalse(e instanceof ServiceNotFoundException); } try { ClassLoaderUtil.loadServiceClass("myservice.notexist", (ClassLoader) null); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } try { ClassLoaderUtil.loadServiceClass("myservice.notfound", (ClassLoader) null); fail("expected ClassNotFoundException"); } catch (ClassNotFoundException e) { assertFalse(e instanceof ServiceNotFoundException); } assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("myservice", getClass().getClassLoader())); assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("myservice", (ClassLoader) null)); try { assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("com.baidu.unbiz.common.sample.MyService", getClass().getClassLoader())); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } try { assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("com.baidu.unbiz.common.sample.MyService", (ClassLoader) null)); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } }
@Test public void loadServiceClass_ClassNameServiceId() throws Exception { assertNull(ClassLoaderUtil.loadServiceClass(null, (String) null)); try { ClassLoaderUtil.loadServiceClass("myservice.notexist", "myservice.notexist"); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } try { ClassLoaderUtil.loadServiceClass("myservice.notfound", "myservice.notfound"); fail("expected ClassNotFoundException"); } catch (ClassNotFoundException e) { assertFalse(e instanceof ServiceNotFoundException); } assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("myservice", "myservice")); assertNull(ClassLoaderUtil.loadServiceClass(null, null, getClass())); assertNull(ClassLoaderUtil.loadServiceClass(null, null, (Class<?>) null)); try { ClassLoaderUtil.loadServiceClass("myservice.notexist", "myservice.notexist", getClass()); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } try { ClassLoaderUtil.loadServiceClass("myservice.notfound", "myservice.notfound", getClass()); fail("expected ClassNotFoundException"); } catch (ClassNotFoundException e) { assertFalse(e instanceof ServiceNotFoundException); } try { ClassLoaderUtil.loadServiceClass("myservice.notexist", "myservice.notexist", (Class<?>) null); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } try { ClassLoaderUtil.loadServiceClass("myservice.notfound", "myservice.notfound", (Class<?>) null); fail("expected ClassNotFoundException"); } catch (ClassNotFoundException e) { assertFalse(e instanceof ServiceNotFoundException); } assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("myservice", "myservice", getClass())); assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("myservice", "myservice", (Class<?>) null)); assertNull(ClassLoaderUtil.loadServiceClass(null, null, getClass().getClassLoader())); assertNull(ClassLoaderUtil.loadServiceClass(null, null, (ClassLoader) null)); try { ClassLoaderUtil.loadServiceClass("myservice.notexist", "myservice.notexist", getClass().getClassLoader()); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } try { ClassLoaderUtil.loadServiceClass("myservice.notfound", "myservice.notfound", getClass().getClassLoader()); fail("expected ClassNotFoundException"); } catch (ClassNotFoundException e) { assertFalse(e instanceof ServiceNotFoundException); } try { ClassLoaderUtil.loadServiceClass("myservice.notexist", "myservice.notexist", (ClassLoader) null); fail("expected ServiceNotFoundException"); } catch (ServiceNotFoundException e) { } try { ClassLoaderUtil.loadServiceClass("myservice.notfound", "myservice.notfound", (ClassLoader) null); fail("expected ClassNotFoundException"); } catch (ClassNotFoundException e) { assertFalse(e instanceof ServiceNotFoundException); } assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("myservice", "myservice", getClass().getClassLoader())); assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("myservice", "myservice", (ClassLoader) null)); assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("com.baidu.unbiz.common.sample.MyService", "com.baidu.unbiz.common.sample.MyService", getClass().getClassLoader())); assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("com.baidu.unbiz.common.sample.MyService", "com.baidu.unbiz.common.sample.MyService", (ClassLoader) null)); assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("com.baidu.unbiz.common.sample.MyService", "abc")); assertEquals(MyService.class, ClassLoaderUtil.loadServiceClass("abc", "myservice")); }