@Test public void testClassLoaderDelegationToSecondClassLoader() throws ClassNotFoundException { ClassLoader cl1 = mock(ClassLoader.class); ClassLoader cl2 = mock(ClassLoader.class); when(cl1.loadClass(Mockito.anyString())).thenAnswer(new Answer<Class<?>>() { @Override public Class<?> answer(InvocationOnMock invocation) throws Throwable { throw new ClassNotFoundException(); } }); when(cl2.loadClass(Mockito.anyString())).thenAnswer(new Answer<Class<?>>() { @Override public Class<?> answer(InvocationOnMock invocation) throws Throwable { return String2.class; } }); SeparatedClassLoader separated = new SeparatedClassLoader(cl1, cl2); Class<?> loadedClass = separated.loadClass(String.class.getName()); assertSame(String2.class, loadedClass); }
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { for (ClassLoader classLoader : chain) { try { return classLoader.loadClass(name); } catch (ClassNotFoundException e) { } } throw new ClassNotFoundException("Class with name " + name + " wasnt found in any of the class loaders in chain"); }
SeparatedClassLoader extends ClassLoader { @Override public Class<?> loadClass(String name) throws ClassNotFoundException { for (ClassLoader classLoader : chain) { try { return classLoader.loadClass(name); } catch (ClassNotFoundException e) { } } throw new ClassNotFoundException("Class with name " + name + " wasnt found in any of the class loaders in chain"); } }
SeparatedClassLoader extends ClassLoader { @Override public Class<?> loadClass(String name) throws ClassNotFoundException { for (ClassLoader classLoader : chain) { try { return classLoader.loadClass(name); } catch (ClassNotFoundException e) { } } throw new ClassNotFoundException("Class with name " + name + " wasnt found in any of the class loaders in chain"); } SeparatedClassLoader(ClassLoader... classLoaderChain); }
SeparatedClassLoader extends ClassLoader { @Override public Class<?> loadClass(String name) throws ClassNotFoundException { for (ClassLoader classLoader : chain) { try { return classLoader.loadClass(name); } catch (ClassNotFoundException e) { } } throw new ClassNotFoundException("Class with name " + name + " wasnt found in any of the class loaders in chain"); } SeparatedClassLoader(ClassLoader... classLoaderChain); @Override Class<?> loadClass(String name); }
SeparatedClassLoader extends ClassLoader { @Override public Class<?> loadClass(String name) throws ClassNotFoundException { for (ClassLoader classLoader : chain) { try { return classLoader.loadClass(name); } catch (ClassNotFoundException e) { } } throw new ClassNotFoundException("Class with name " + name + " wasnt found in any of the class loaders in chain"); } SeparatedClassLoader(ClassLoader... classLoaderChain); @Override Class<?> loadClass(String name);  }