@Override public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { try { return super.loadClass(name, resolve); } catch (ClassNotFoundException e) { return mDefaultClassloader.loadClass(name, resolve); } } }
private Class<?> loadClass(File sourceJar, String classNameManifestKey, String libDir, String classNameToLoad) throws ClassNotFoundException { Predicate<JarEntry> filter = jarEntry -> jarEntry.getName().startsWith(libDir + "/") && jarEntry.getName().endsWith(".jar"); this.urlClassLoader = JarUtil.getClassLoaderFromJar(sourceJar, filter, getDepsDir(), DefaultAgentLauncherCreatorImpl.class.getClassLoader(), AgentLauncher.class, AgentLaunchDescriptor.class); LOG.info("Attempting to load {} as specified by manifest key {}", classNameToLoad, classNameManifestKey); return this.urlClassLoader.loadClass(classNameToLoad); }
@Override protected synchronized Class<?> loadClass(String arg0, boolean arg1) throws ClassNotFoundException { // System.out.println(this+" being asked to load class "+arg0+","+arg1); return super.loadClass(arg0, arg1); }
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { // System.out.println(this + " loading "+name); return super.loadClass(name); }
public int run(String launcherVersion, String launcherMd5, ServerUrlGenerator urlGenerator, Map<String, String> environmentVariables, Map context) { String agentProcessParentClassName = JarUtil.getManifestKey(Downloader.AGENT_BINARY_JAR, GO_AGENT_BOOTSTRAP_CLASS); String tempDirSuffix = new BigInteger(64, new SecureRandom()).toString(16) + "-" + Downloader.AGENT_BINARY_JAR; File tempDir = new File(FileUtil.TMP_PARENT_DIR, "deps-" + tempDirSuffix); try { try (URLClassLoader urlClassLoader = JarUtil.getClassLoaderFromJar(Downloader.AGENT_BINARY_JAR, jarEntryFilter(), tempDir, this.getClass().getClassLoader())) { Class<?> aClass = urlClassLoader.loadClass(agentProcessParentClassName); AgentProcessParent agentProcessParent = (AgentProcessParent) aClass.getDeclaredConstructor().newInstance(); return agentProcessParent.run(launcherVersion, launcherMd5, urlGenerator, environmentVariables, context); } catch (ReflectiveOperationException | IOException e) { throw new RuntimeException(e); } } finally { FileUtils.deleteQuietly(tempDir); } }
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { if (name.startsWith("org.apache.hadoop")) { throw new ClassNotFoundException(name); } else if (name.startsWith("org.apache.log4j")) { return properParent.loadClass(name); } else { return super.loadClass(name); } } }
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { if (name.startsWith(concurrentPackage) // Use other classloader for ListenableFuture, so that the objects can interact && !ListenableFuture.class.getName().equals(name)) { synchronized (loadedClasses) { Class<?> toReturn = loadedClasses.get(name); if (toReturn == null) { toReturn = super.findClass(name); loadedClasses.put(name, toReturn); } return toReturn; } } return super.loadClass(name); } };
/** {@inheritDoc} */ @Override protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { if (excludeClassNames.contains(name)) throw new ClassNotFoundException(name); return super.loadClass(name, resolve); }
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { if (blocklist.contains(name)) { throw new ClassNotFoundException("I'm sorry Dave, I'm afraid I can't do that."); } if (name.startsWith(concurrentPackage)) { Class<?> c = findLoadedClass(name); if (c == null) { return super.findClass(name); } return c; } return super.loadClass(name); } };
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { if (disallowedClassNames.contains(name)) { throw new ClassNotFoundException("I'm sorry Dave, I'm afraid I can't do that."); } if (name.startsWith(concurrentPackage)) { Class<?> c = findLoadedClass(name); if (c == null) { return super.findClass(name); } return c; } return super.loadClass(name); } };
@Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { // Force Finalizer to load from this class loader, not its parent. if (name.equals(Finalizer.class.getName())) { Class<?> clazz = findClass(name); if (resolve) { resolveClass(clazz); } return clazz; } return super.loadClass(name, resolve); } }
private WeakReference<ClassLoader> doTestUnloadableInStaticFieldIfClosed() throws Exception { final ClassLoader myLoader = getClass().getClassLoader(); URLClassLoader sepLoader = new URLClassLoader(getClassPathUrls(), myLoader.getParent()); Class<?> frqC = FinalizableReferenceQueue.class; Class<?> sepFrqC = sepLoader.loadClass(frqC.getName()); assertNotSame(frqC, sepFrqC); Class<?> sepFrqSystemLoaderC = sepLoader.loadClass(FinalizableReferenceQueue.SystemLoader.class.getName()); Field disabled = sepFrqSystemLoaderC.getDeclaredField("disabled"); disabled.setAccessible(true); disabled.set(null, true); Class<?> frqUserC = FrqUser.class; Class<?> sepFrqUserC = sepLoader.loadClass(frqUserC.getName()); assertNotSame(frqUserC, sepFrqUserC); assertSame(sepLoader, sepFrqUserC.getClassLoader()); Callable<?> sepFrqUser = (Callable<?>) sepFrqUserC.newInstance(); WeakReference<?> finalizableWeakReference = (WeakReference<?>) sepFrqUser.call(); GcFinalization.awaitClear(finalizableWeakReference); Field sepFrqUserFinalizedF = sepFrqUserC.getField("finalized"); Semaphore finalizeCount = (Semaphore) sepFrqUserFinalizedF.get(null); boolean finalized = finalizeCount.tryAcquire(5, TimeUnit.SECONDS); assertTrue(finalized); Field sepFrqUserFrqF = sepFrqUserC.getField("frq"); Closeable frq = (Closeable) sepFrqUserFrqF.get(null); frq.close(); return new WeakReference<ClassLoader>(sepLoader); }
Class<?> sepFrqC = sepLoader.loadClass(frqC.getName()); assertNotSame(frqC, sepFrqC); sepLoader.loadClass(FinalizableReferenceQueue.SystemLoader.class.getName()); Field disabled = sepFrqSystemLoaderC.getDeclaredField("disabled"); disabled.setAccessible(true); Class<?> sepFwrC = sepLoader.loadClass(MyFinalizableWeakReference.class.getName()); Constructor<?> sepFwrCons = sepFwrC.getConstructor(Object.class, sepFrqC); Class<?> sepStopwatchC = sepLoader.loadClass(Stopwatch.class.getName()); assertSame(sepLoader, sepStopwatchC.getClassLoader()); AtomicReference<Object> sepStopwatchA =
private Filter loadFilter(URLClassLoader urlC, String fClass) throws Exception { Filter f; try { f = config.framework().newClassInstance(Filter.class, (Class<Filter>) urlC.loadClass(fClass)); } catch (NullPointerException ex) { // We failed to load the Filter, let's try directly. f = config.framework().newClassInstance(Filter.class, (Class<Filter>) IOUtils.loadClass(getClass(), fClass)); } return f; }
@Override protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { // grab the magic lock synchronized (getClassLoadingLock(name)) { // Check if class is in the loaded classes cache Class<?> cachedClass = findLoadedClass(name); if (cachedClass != null) { return resolveClass(cachedClass, resolve); } // If this is an SPI class, only check SPI class loader if (isSpiClass(name)) { return resolveClass(spiClassLoader.loadClass(name), resolve); } // Look for class locally return super.loadClass(name, resolve); } }
@Test public void testFindInPackageFromVfsDirectoryURL() throws Exception { try (final URLClassLoader cl = ResolverUtilTest.compileAndCreateClassLoader("3")) { final ResolverUtil resolverUtil = new ResolverUtil(); resolverUtil .setClassLoader(new SingleURLClassLoader(new URL("vfs:/" + ResolverUtilTest.WORK_DIR + "/resolverutil3/customplugin3/"), cl)); resolverUtil.findInPackage(new PluginTest(), "customplugin3"); assertEquals("Class not found in packages", 1, resolverUtil.getClasses().size()); assertEquals("Unexpected class resolved", cl.loadClass("customplugin3.FixedString3Layout"), resolverUtil.getClasses().iterator().next()); } }
@Test public void testFindInPackageFromVfsJarURL() throws Exception { try (final URLClassLoader cl = ResolverUtilTest.compileJarAndCreateClassLoader("4")) { final ResolverUtil resolverUtil = new ResolverUtil(); resolverUtil.setClassLoader(new SingleURLClassLoader( new URL("vfs:/" + ResolverUtilTest.WORK_DIR + "/resolverutil4/customplugin4.jar/customplugin4/"), cl)); resolverUtil.findInPackage(new PluginTest(), "customplugin4"); assertEquals("Class not found in packages", 1, resolverUtil.getClasses().size()); assertEquals("Unexpected class resolved", cl.loadClass("customplugin4.FixedString4Layout"), resolverUtil.getClasses().iterator().next()); } }
@Test public void testFindInPackageFromJarPath() throws Exception { try (final URLClassLoader cl = compileJarAndCreateClassLoader("2")) { final ResolverUtil resolverUtil = new ResolverUtil(); resolverUtil.setClassLoader(cl); resolverUtil.findInPackage(new PluginTest(), "customplugin2"); assertEquals("Class not found in packages", 1, resolverUtil.getClasses().size()); assertEquals("Unexpected class resolved", cl.loadClass("customplugin2.FixedString2Layout"), resolverUtil.getClasses().iterator().next()); } }
@Test public void testFindInPackageFromDirectoryPath() throws Exception { try (final URLClassLoader cl = compileAndCreateClassLoader("1")) { final ResolverUtil resolverUtil = new ResolverUtil(); resolverUtil.setClassLoader(cl); resolverUtil.findInPackage(new PluginTest(), "customplugin1"); assertEquals("Class not found in packages", 1, resolverUtil.getClasses().size()); assertEquals("Unexpected class resolved", cl.loadClass("customplugin1.FixedString1Layout"), resolverUtil.getClasses().iterator().next()); } }