From a7e57430802046d65c3e1e164969dbeec5891fc7 Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Fri, 17 Nov 2017 22:08:25 +0100 Subject: [PATCH] findResources("SomeDir/") fails (IDEA-182329) --- .../src/com/intellij/util/lang/ClassPath.java | 13 +++--- .../util/lang/UrlClassLoaderTest.java | 42 ++++++++++++++++++- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/platform/util/src/com/intellij/util/lang/ClassPath.java b/platform/util/src/com/intellij/util/lang/ClassPath.java index 03a76797f817..e9dc13257636 100644 --- a/platform/util/src/com/intellij/util/lang/ClassPath.java +++ b/platform/util/src/com/intellij/util/lang/ClassPath.java @@ -266,19 +266,16 @@ public class ClassPath { List loaders = null; if (myCanUseCache && myAllUrlsWereProcessed) { - boolean nameIsDirectory = name.endsWith("/"); - Collection loadersSet = nameIsDirectory ? new SmartList() : new LinkedHashSet(); + Collection loadersSet = new LinkedHashSet(); myCache.iterateLoaders(name, ourLoaderCollector, loadersSet, this); - if (!nameIsDirectory) { + if (name.endsWith("/")) { + myCache.iterateLoaders(name.substring(0, name.length() - 1), ourLoaderCollector, loadersSet, this); + } else { myCache.iterateLoaders(name.concat("/"), ourLoaderCollector, loadersSet, this); } - if (nameIsDirectory) { - loaders = (List)loadersSet; - } else { - loaders = new ArrayList(loadersSet); - } + loaders = new ArrayList(loadersSet); } myLoaders = loaders; diff --git a/platform/util/testSrc/com/intellij/util/lang/UrlClassLoaderTest.java b/platform/util/testSrc/com/intellij/util/lang/UrlClassLoaderTest.java index 89c5e664636c..fd3cd2a2f56b 100644 --- a/platform/util/testSrc/com/intellij/util/lang/UrlClassLoaderTest.java +++ b/platform/util/testSrc/com/intellij/util/lang/UrlClassLoaderTest.java @@ -17,8 +17,7 @@ package com.intellij.util.lang; import com.intellij.openapi.application.PathManager; import com.intellij.openapi.util.io.FileUtil; -import com.intellij.util.ConcurrencyUtil; -import com.intellij.util.ObjectUtils; +import com.intellij.util.*; import com.intellij.util.containers.ContainerUtil; import org.junit.Test; @@ -218,4 +217,43 @@ public class UrlClassLoaderTest { return loader.findResource(name); } } + + @Test + public void testFindDirWhenUsingCache() throws IOException { + int counter = 1; + for (String dirName : new String[]{ "dir", "dir/", "dir.class", "dir.class/"}) { + for(String resourceName: new String[] {"a.class", "a.txt"} ) { + File root = FileUtil.createTempDirectory("testFindDirWhenUsingCache", String.valueOf(counter++)); + File subDir = createTestDir(root, dirName); + createTestFile(subDir, resourceName); + + URL url = root.toURI().toURL(); + URLClassLoader standardCl = new URLClassLoader(new URL[] {url}); + + try { + Enumeration resources = standardCl.findResources(dirName); + assertTrue(resources.hasMoreElements()); + URL expectedResourceUrl = resources.nextElement(); + + withCustomCachedClassloader(url, (customCl) -> { + assertNull(customCl.findResource("SomeNonExistentResource.resource")); + checkResourceUrlIsTheSame(customCl, dirName, expectedResourceUrl); + }); + withCustomCachedClassloader(url, (customCl) -> checkResourceUrlIsTheSame(customCl, dirName, expectedResourceUrl)); + } finally { + standardCl.close(); + } + } + } + } + + private static void checkResourceUrlIsTheSame(UrlClassLoader customCl, String resourceName, URL expectedResourceUrl) throws IOException { + Enumeration customClResources = customCl.findResources(resourceName); + assertTrue(customClResources.hasMoreElements()); + assertEquals(expectedResourceUrl, customClResources.nextElement()); + } + + private static void withCustomCachedClassloader(URL url, ThrowableConsumer testAction) throws IOException { + testAction.consume(UrlClassLoader.build().useCache().urls(url).get()); + } } \ No newline at end of file