### Question:
HelperClassManager { @Nullable public T getForClassLoaderOfClass(Class<?> classOfTargetClassLoader) { T ret = null; try { ret = doGetForClassLoaderOfClass(classOfTargetClassLoader); } catch (Throwable throwable) { logger.error("Failed to load Helper class " + implementation + " for class " + classOfTargetClassLoader + ", loaded by " + "ClassLoader " + classOfTargetClassLoader.getClassLoader(), throwable); } return ret; } protected  HelperClassManager(ElasticApmTracer tracer, String implementation, String[] additionalHelpers); @Nullable T getForClassLoaderOfClass(Class<?> classOfTargetClassLoader);  }### Answer:
@SuppressWarnings("UnusedAssignment") @Test void testCaching() throws ClassNotFoundException, InterruptedException { HelperClassManager.ForAnyClassLoader<?> helperClassManager1 = HelperClassManager.ForAnyClassLoader.of(mock(ElasticApmTracer.class), "co.elastic.apm.agent.bci.HelperClassManagerTest$InnerTestClass$HelperClassImpl"); URL[] urls = {getClass().getProtectionDomain().getCodeSource().getLocation()}; ClassLoader targetClassLoader1 = new URLClassLoader(urls, getClass().getClassLoader().getParent()); Class libClass1 = targetClassLoader1.loadClass("co.elastic.apm.agent.bci.HelperClassManagerTest$InnerTestClass$LibClass"); Object helper1 = helperClassManager1.getForClassLoaderOfClass(libClass1); assertThat(helperClassManager1.clId2helperMap.containsKey(targetClassLoader1)).isTrue(); assertThat(helperClassManager1.clId2helperMap.get(targetClassLoader1).get()).isEqualTo(helper1); assertThat(HelperClassManager.ForAnyClassLoader.clId2helperImplListMap.size()).isEqualTo(1); assertThat(HelperClassManager.ForAnyClassLoader.clId2helperImplListMap.get(targetClassLoader1)).isNotNull(); HelperClassManager.ForAnyClassLoader<?> helperClassManager2 = HelperClassManager.ForAnyClassLoader.of(mock(ElasticApmTracer.class), "co.elastic.apm.agent.bci.HelperClassManagerTest$InnerTestClass$HelperClassImpl"); Object helper2 = helperClassManager2.getForClassLoaderOfClass(libClass1); assertThat(helperClassManager2.clId2helperMap.containsKey(targetClassLoader1)).isTrue(); assertThat(helperClassManager2.clId2helperMap.get(targetClassLoader1).get()).isEqualTo(helper2); assertThat(HelperClassManager.ForAnyClassLoader.clId2helperImplListMap.size()).isEqualTo(1); assertThat(HelperClassManager.ForAnyClassLoader.clId2helperImplListMap.get(targetClassLoader1)).isNotNull(); targetClassLoader1 = null; libClass1 = null; helper1 = helper2 = null; System.gc(); Thread.sleep(1000); assertThat(helperClassManager1.clId2helperMap.approximateSize()).isEqualTo(1); assertThat(helperClassManager1.clId2helperMap.iterator().hasNext()).isFalse(); assertThat(helperClassManager2.clId2helperMap.approximateSize()).isEqualTo(1); assertThat(helperClassManager2.clId2helperMap.iterator().hasNext()).isFalse(); ClassLoader targetClassLoader3 = new URLClassLoader(urls, getClass().getClassLoader().getParent()); Class libClass3 = targetClassLoader3.loadClass("co.elastic.apm.agent.bci.HelperClassManagerTest$InnerTestClass$LibClass"); @SuppressWarnings("unused") Object helper3 = helperClassManager1.getForClassLoaderOfClass(libClass3); assertThat(helperClassManager1.clId2helperMap.approximateSize()).isEqualTo(1); assertThat(helperClassManager1.clId2helperMap.get(targetClassLoader3).get()).isEqualTo(helper3); assertThat(HelperClassManager.ForAnyClassLoader.clId2helperImplListMap.size()).isEqualTo(1); assertThat(HelperClassManager.ForAnyClassLoader.clId2helperImplListMap.get(targetClassLoader3)).isNotNull(); }