@Test public void testLoadClass() { ClassLoader cl = new DelegatingClassLoader(clOne, clTwo, clThree, clClasses); Class c = null; try { c = cl.loadClass(DelegatingClassLoaderTest.class.getName()); } catch (Exception e) { throw new RuntimeException("Failed to load class via DelegatingClassLoader: " + DelegatingClassLoaderTest.class.getName(), e); } Assert.assertSame(clClasses, c.getClassLoader()); cl = new DelegatingClassLoader(clOne, clTwo, clThree); try { cl.loadClass(DelegatingClassLoaderTest.class.getName()); Assert.fail("Loading the class should fail: " + DelegatingClassLoaderTest.class.getName()); } catch (ClassNotFoundException e) { } catch (Exception e) { throw new RuntimeException("Failed to load class unexpectedly: " + DelegatingClassLoaderTest.class.getName(), e); } cl = new DelegatingClassLoader(clOne, clTwo, clThree, getClass().getClassLoader(), clClasses); try { c = cl.loadClass(DelegatingClassLoaderTest.class.getName()); } catch (Exception e) { throw new RuntimeException("Failed to load class: " + DelegatingClassLoaderTest.class.getName(), e); } Assert.assertSame(DelegatingClassLoaderTest.class, c); Assert.assertNotSame(clClasses, c.getClassLoader()); }
@Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Class cl; for (ClassLoader delegate : delegates) { try { cl = delegate.loadClass(name); if (cl != null) { return cl; } } catch (ClassNotFoundException ignore) { } if (log.isTraceEnabled()) { log.trace("Class " + name + " not found with classloader: " + delegate + ". Trying other delegates"); } } throw new ClassNotFoundException("Class " + name + " not found with any of delegates " + delegates); }
DelegatingClassLoader extends ClassLoader { @Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Class cl; for (ClassLoader delegate : delegates) { try { cl = delegate.loadClass(name); if (cl != null) { return cl; } } catch (ClassNotFoundException ignore) { } if (log.isTraceEnabled()) { log.trace("Class " + name + " not found with classloader: " + delegate + ". Trying other delegates"); } } throw new ClassNotFoundException("Class " + name + " not found with any of delegates " + delegates); } }
DelegatingClassLoader extends ClassLoader { @Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Class cl; for (ClassLoader delegate : delegates) { try { cl = delegate.loadClass(name); if (cl != null) { return cl; } } catch (ClassNotFoundException ignore) { } if (log.isTraceEnabled()) { log.trace("Class " + name + " not found with classloader: " + delegate + ". Trying other delegates"); } } throw new ClassNotFoundException("Class " + name + " not found with any of delegates " + delegates); } DelegatingClassLoader(ClassLoader... delegates); }
DelegatingClassLoader extends ClassLoader { @Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Class cl; for (ClassLoader delegate : delegates) { try { cl = delegate.loadClass(name); if (cl != null) { return cl; } } catch (ClassNotFoundException ignore) { } if (log.isTraceEnabled()) { log.trace("Class " + name + " not found with classloader: " + delegate + ". Trying other delegates"); } } throw new ClassNotFoundException("Class " + name + " not found with any of delegates " + delegates); } DelegatingClassLoader(ClassLoader... delegates); @Override InputStream getResourceAsStream(String name); @Override URL getResource(String name); @Override Enumeration<URL> getResources(String name); }
DelegatingClassLoader extends ClassLoader { @Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Class cl; for (ClassLoader delegate : delegates) { try { cl = delegate.loadClass(name); if (cl != null) { return cl; } } catch (ClassNotFoundException ignore) { } if (log.isTraceEnabled()) { log.trace("Class " + name + " not found with classloader: " + delegate + ". Trying other delegates"); } } throw new ClassNotFoundException("Class " + name + " not found with any of delegates " + delegates); } DelegatingClassLoader(ClassLoader... delegates); @Override InputStream getResourceAsStream(String name); @Override URL getResource(String name); @Override Enumeration<URL> getResources(String name);  }