Repository navigation
Description
Spring Boot 4.2.0-SNAPSHOT, main at commit f2abe77.
I went looking at why JarUrlClassLoader's caches (the per-URL jarFiles map, and through it the jar and zip caches underneath NestedJarFile and NestedLocation) never seem to shrink after a context refresh in an executable jar application, even though clearCache()'s own Javadoc says plainly: "This method is called reflectively by ClearCachesApplicationListener." In a launched executable jar, that call never actually happens.
ClearCachesApplicationListener.clearClassLoaderCaches looks the method up with:
Method clearCacheMethod = classLoader.getClass().getDeclaredMethod("clearCache");
clearCacheMethod.invoke(classLoader);for the thread context class loader, then for each parent up the chain. In an executable jar, Launcher sets the context class loader to a LaunchedClassLoader. LaunchedClassLoader extends JarUrlClassLoader, and JarUrlClassLoader declares public void clearCache() (JarUrlClassLoader.java:247), but LaunchedClassLoader itself never overrides or redeclares that method, it only inherits it. Class.getDeclaredMethod returns only methods declared directly on the class you call it on, not inherited ones, so the lookup throws NoSuchMethodException every time. The listener's catch block swallows that silently, and walking up to the parent class loaders (the application and platform class loaders) doesn't help either, since neither of those has a clearCache method at all.
This is demonstrable directly from the class hierarchy, without needing to run a full application:
ClassLoader loader = new LaunchedClassLoader(false, new URL[0], ClassLoader.getSystemClassLoader());
loader.getClass().getMethod("clearCache"); // finds the inherited public JarUrlClassLoader.clearCache()
loader.getClass().getDeclaredMethod("clearCache"); // throws NoSuchMethodExceptionExpected: after ContextRefreshedEvent fires in an executable jar, JarUrlClassLoader.clearCache() runs and clears the Handler URL caches, the class loader's own jarFiles map, and each open NestedJarFile's cache.
Actual: it never runs. Nothing fails or logs anything, because the exception is caught and ignored. The caches just stay populated for the life of the application, along with the open jar file handles clearCache() would otherwise close.
I haven't run this end to end against a live ApplicationContext refresh to show the retained caches as a directly observable side effect, since I can't run JVM processes in this environment right now. The snippet above demonstrates the actual reflective lookup failing, which is the entire mechanism, so I'm confident in it, but I'd still recommend whoever picks this up confirm it live: put a breakpoint inside clearClassLoaderCaches after a context refresh in a launched jar and watch the NoSuchMethodException get caught and ignored.
This looks like it dates from the nested-jar rewrite in commit 7ad4a98 ("Rewrite nested jar support code and remove Java 8 support", 2023-09-18). Before that commit, LaunchedURLClassLoader declared clearCache() itself, so getDeclaredMethod found it directly on the class being asked. Since then, the method has lived on the new JarUrlClassLoader superclass instead, and nothing updated the lookup to match. LaunchedClassLoader is the only main-code subclass of JarUrlClassLoader in the repository. Looking the method up with ReflectionUtils.findMethod(classLoader.getClass(), "clearCache"), or with getMethod since the method is public, would find it either way.
Activity
[-]ClearCachesApplicationListener does not find inherited clearCache()[/-][+]ClearCachesApplicationListener does not find inherited clearCache() methods[/+]Find inherited clearCache methods
Fix checkstyle violations