From 1a0362acf49a718d11bc7d4f220cca8ff2e0ddd1 Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Tue, 28 May 2019 22:40:30 +0200 Subject: [PATCH] Replaced ClassLoader.getResource() + URL.openStream() with ClassLoader.getResourceAsStream() This optimization allows to reuse UrlClassLoader's capability to load resources faster: e.g. using existing ZipFile handles or preloaded contents GitOrigin-RevId: 315b251b7fcb5b600b0626d694f0d7e761ee973f --- .../InspectionProfileEntry.java | 21 ++++++++-------- .../ex/InspectionToolWrapper.java | 23 +++++++---------- .../DumpInspectionDescriptionsAction.java | 12 ++++----- .../ui/search/SearchableOptionIndexLoader.kt | 2 +- .../SearchableOptionsRegistrarImpl.java | 7 +++--- .../src/com/intellij/ide/util/TipUIUtil.java | 12 ++++----- .../src/com/intellij/util/ResourceUtil.java | 25 ++++++++++++++++++- .../output/MavenBuildToolLogTestUtils.java | 2 +- 8 files changed, 61 insertions(+), 43 deletions(-) diff --git a/platform/analysis-api/src/com/intellij/codeInspection/InspectionProfileEntry.java b/platform/analysis-api/src/com/intellij/codeInspection/InspectionProfileEntry.java index 00dbcd9d1dab..7840bffa5a03 100644 --- a/platform/analysis-api/src/com/intellij/codeInspection/InspectionProfileEntry.java +++ b/platform/analysis-api/src/com/intellij/codeInspection/InspectionProfileEntry.java @@ -24,14 +24,12 @@ import com.intellij.util.xmlb.annotations.Property; import gnu.trove.THashSet; import gnu.trove.TObjectHashingStrategy; import org.jdom.Element; -import org.jetbrains.annotations.Nls; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.*; import javax.swing.*; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -399,11 +397,11 @@ public abstract class InspectionProfileEntry implements BatchSuppressableTool { return null; } + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "2020.1") @Nullable protected URL getDescriptionUrl() { - final String fileName = getDescriptionFileName(); - if (fileName == null) return null; - return ResourceUtil.getResource(getDescriptionContextClass(), "/inspectionDescriptions", fileName); + return null; } @NotNull @@ -429,9 +427,12 @@ public abstract class InspectionProfileEntry implements BatchSuppressableTool { if (description != null) return description; try { - URL descriptionUrl = getDescriptionUrl(); - if (descriptionUrl == null) return null; - return ResourceUtil.loadText(descriptionUrl); + InputStream descriptionStream = null; + final String fileName = getDescriptionFileName(); + if (fileName != null) { + descriptionStream = ResourceUtil.getResourceAsStream(getDescriptionContextClass(), "/inspectionDescriptions", fileName); + } + return descriptionStream != null ? ResourceUtil.loadText(descriptionStream) : null; } catch (IOException ignored) { } diff --git a/platform/analysis-api/src/com/intellij/codeInspection/ex/InspectionToolWrapper.java b/platform/analysis-api/src/com/intellij/codeInspection/ex/InspectionToolWrapper.java index 2741ecfb5883..cc5d1b6b1243 100644 --- a/platform/analysis-api/src/com/intellij/codeInspection/ex/InspectionToolWrapper.java +++ b/platform/analysis-api/src/com/intellij/codeInspection/ex/InspectionToolWrapper.java @@ -17,7 +17,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.IOException; -import java.net.URL; +import java.io.InputStream; /** * @author Dmitry Avdeev @@ -161,28 +161,23 @@ public abstract class InspectionToolWrapper bundles = calculateBundleNames(fixedPath, Locale.getDefault()); + for (String bundle : bundles) { + InputStream stream = loader.getResourceAsStream(bundle + "/" + fileName); + if (stream == null) continue; + + return stream; + } + + return loader.getResourceAsStream(fixedPath + "/" + fileName); + } + public static URL getResource(@NotNull ClassLoader loader, @NonNls @NotNull String basePath, @NonNls @NotNull String fileName) { String fixedPath = StringUtil.trimStart(StringUtil.trimEnd(basePath, "/"), "/"); @@ -98,7 +116,12 @@ public class ResourceUtil { @NotNull public static String loadText(@NotNull URL url) throws IOException { - InputStream inputStream = new BufferedInputStream(URLUtil.openStream(url)); + return loadText(URLUtil.openStream(url)); + } + + @NotNull + public static String loadText(@NotNull InputStream in) throws IOException { + InputStream inputStream = in instanceof BufferedInputStream ? in : new BufferedInputStream(in); try (InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) { StringBuilder text = new StringBuilder(); diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/externalSystemIntegration/output/MavenBuildToolLogTestUtils.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/externalSystemIntegration/output/MavenBuildToolLogTestUtils.java index 74d34cbec639..358fedc2de66 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/externalSystemIntegration/output/MavenBuildToolLogTestUtils.java +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/externalSystemIntegration/output/MavenBuildToolLogTestUtils.java @@ -46,7 +46,7 @@ public abstract class MavenBuildToolLogTestUtils extends UsefulTestCase { @NotNull protected static String[] fromFile(String resource) throws IOException { - try (InputStream stream = ResourceUtil.getResource(MavenBuildToolLogTestUtils.class, "", resource).openStream(); + try (InputStream stream = ResourceUtil.getResourceAsStream(MavenBuildToolLogTestUtils.class, "", resource); Scanner scanner = new Scanner(stream)) { List result = new ArrayList<>(); while (scanner.hasNextLine()) {