TempClassLoader extends URLClassLoader { @Override public Class loadClass(final String name) throws ClassNotFoundException { return this.loadClass(name, false); } TempClassLoader(final ClassLoader parent); void skip(final Skip s); @Override Class loadClass(final String name); @Override Enumeration<URL> getResources(final String name);  }
@Test public void test() throws Exception { final ClassLoader tempCL = new TempClassLoader(this.getClass().getClassLoader()); Class<?> clazz; clazz = tempCL.loadClass(TempClassLoaderTest.class.getName()); assertSame(tempCL, clazz.getClassLoader()); clazz = tempCL.loadClass(javax.persistence.EntityManager.class.getName()); assertNotSame(tempCL, clazz.getClassLoader()); clazz = tempCL.loadClass(java.lang.String.class.getName()); assertNotSame(tempCL, clazz.getClassLoader()); clazz = tempCL.loadClass(SampleAnnotation.class.getName()); assertSame(tempCL, clazz.getClassLoader()); }