### Question:
ReloadClassLoader extends ClassLoader { public synchronized void reload() { classMap.clear(); } private  ReloadClassLoader(URL[] urls); URL[] getClasspathURLs(); void addClasspathURL(URL url); synchronized void reload(); static ReloadClassLoader newClassloader(URL... urls); static ReloadClassLoader currentClassLoader();  }### Answer:
@Test public void testReload() throws Exception { String reloadClassName = "com.xinqihd.test.ReloadTest"; File sourceDir = new File(reloadSourceDir); File sourceCodeFile = new File(reloadSourceDir, reloadClassName.replace('.', '/')+".java"); writeToFile(sourceCode1, sourceCodeFile); ReloadClassLoader cloader = ReloadClassLoader.newClassloader(new URL[]{sourceDir.toURL()}); Class helloClazz = cloader.loadClass(reloadClassName); assertTrue(helloClazz != null); Method method = helloClazz.getMethod("execute"); String hello = (String)method.invoke(helloClazz, null); assertEquals("hello", hello); writeToFile(sourceCode2, sourceCodeFile); cloader.reload(); cloader = ReloadClassLoader.newClassloader(new URL[]{sourceDir.toURL()}); Class worldClazz = cloader.loadClass(reloadClassName); method = worldClazz.getMethod("execute"); String world = (String)method.invoke(worldClazz, null); assertEquals("world", world); }
@Test public void testClassLoaderLeak() throws Exception { int max = 10000; File sourceFile = new File(reloadSourceDir); URL[] urls = new URL[]{sourceFile.toURL()}; ReloadProtocolCodecFilter filter = new ReloadProtocolCodecFilter( GameServer.PROTOCOL_CODEC, GameServer.PROTOCOL_HANDLER); for ( int i=0; i<max; i++ ) { filter.reload(); } System.out.println("done"); }