From 267f0883e5a092dbddbcc8d8a3f9cb1455d7d979 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 13 May 2025 10:21:05 +0200 Subject: [PATCH] Cleanup (dead code; optimization; API pruning) GitOrigin-RevId: 2a660f124c400cb260950755041e01c9761adef0 --- .../src/com/intellij/ide/RegionUrlMapper.java | 127 ++++++------------ 1 file changed, 44 insertions(+), 83 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/ide/RegionUrlMapper.java b/platform/platform-impl/src/com/intellij/ide/RegionUrlMapper.java index 0f8e27e9afad..10a70b5b1504 100644 --- a/platform/platform-impl/src/com/intellij/ide/RegionUrlMapper.java +++ b/platform/platform-impl/src/com/intellij/ide/RegionUrlMapper.java @@ -8,12 +8,12 @@ import com.intellij.openapi.diagnostic.ControlFlowException; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.text.Strings; import com.intellij.util.SmartList; +import com.intellij.util.SystemProperties; import com.intellij.util.concurrency.annotations.RequiresBackgroundThread; import com.intellij.util.concurrency.annotations.RequiresReadLockAbsence; import com.intellij.util.net.PlatformHttpClient; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.io.JsonReaderEx; import org.jetbrains.io.JsonUtil; @@ -21,7 +21,10 @@ import java.io.IOException; import java.net.URI; import java.net.http.HttpResponse; import java.util.*; -import java.util.concurrent.*; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.TimeUnit; /** * @see Region @@ -31,19 +34,7 @@ import java.util.concurrent.*; public final class RegionUrlMapper { private static final Logger LOG = Logger.getInstance("#com.intellij.ide.RegionUrlMapper"); - private static final int CACHE_DATA_EXPIRATION_MIN; - - static { - int expiration; - try { - expiration = Integer.parseInt(System.getProperty("ide.region.url.mapping.expiration.timeout", "2")); - } - catch (NumberFormatException e) { - expiration = 2; - } - CACHE_DATA_EXPIRATION_MIN = expiration; - } - + private static final int CACHE_DATA_EXPIRATION_MIN = SystemProperties.getIntProperty("ide.region.url.mapping.expiration.timeout", 2); private static final String CONFIG_URL_DEFAULT = "https://www.jetbrains.com/config/JetBrainsResourceMapping.json"; private static final Map CONFIG_URL_TABLE = Map.of( // augment the table with other regions if needed @@ -72,22 +63,6 @@ public final class RegionUrlMapper { private RegionUrlMapper() { } - /** @deprecated Use the more explicitly named {@link #tryMapUrlBlocking}, or {@link #tryMapUrl} when calling from a suspending context */ - @Deprecated - @RequiresBackgroundThread - @RequiresReadLockAbsence - public static @NotNull String mapUrl(@NotNull String url) { - return tryMapUrlBlocking(url); - } - - /** @deprecated Use the more explicitly named {@link #tryMapUrlBlocking}, or {@link #tryMapUrl} when calling from a suspending context */ - @Deprecated - @RequiresBackgroundThread - @RequiresReadLockAbsence - public static @NotNull String mapUrl(@NotNull String url, @NotNull Region region) { - return tryMapUrlBlocking(url, region); - } - /** * @see #tryMapUrlBlocking(String, Region) * @see #tryMapUrl(String) when calling from a suspending context, consider using the async version @@ -142,42 +117,35 @@ public final class RegionUrlMapper { * @return a CompletableFuture that resolves to the adjusted url in case the mapping is configured, or the original url otherwise */ public static @NotNull CompletableFuture<@NotNull String> tryMapUrl(@NotNull String url, @NotNull Region region) { - CompletableFuture<@NotNull RegionMapping> mappingFuture = tryLoadMappingOrEmpty(region); - return mappingFuture.thenApply(mapping -> mapping.apply(url)); + return ourCache.get(region) + .exceptionally(t -> { + while (t instanceof CompletionException) { + t = t.getCause(); + } + if (t instanceof CancellationException || t instanceof ControlFlowException) { + LOG.debug("Loading regional URL mappings interrupted (using non-regional URL as fallback): " + t); + } + else if (FORCE_REGION_MAPPINGS_LOAD) { + LOG.error("Failed to load URL mappings for " + region + ", URL=" + getConfigUrl(region), t); + } + else if (t instanceof IOException) { + // legitimate failure when using the IDE offline; just log it without the stack trace + LOG.info("Failed to fetch regional URL mappings (using non-regional URL as fallback): " + t); + } + else if (t instanceof JsonParseException) { + LOG.warn("Failed to parse regional URL mappings (using non-regional URL as fallback): " + t); + } + else { + // never suppress errors indicating programmatic bugs or an IDE misconfiguration + LOG.error("Failed to load regional URL mappings (using non-regional URL as fallback)", t); + } + return RegionMapping.EMPTY; + }) + .thenApply(mapping -> mapping.apply(url)); } - private static @NotNull CompletableFuture<@NotNull RegionMapping> tryLoadMappingOrEmpty(@NotNull Region region) { - return loadMapping(region).exceptionally(t -> { - while (t instanceof CompletionException) { - t = t.getCause(); - } - if (t instanceof CancellationException || t instanceof ControlFlowException) { - LOG.debug("Loading regional URL mappings interrupted (using non-regional URL as fallback): " + t); - } - else if (FORCE_REGION_MAPPINGS_LOAD) { - LOG.error("Failed to load URL mappings for " + region + ", URL=" + getConfigUrl(region), t); - } - else if (t instanceof IOException) { - // legitimate failure when using the IDE offline; just log it without the stack trace - LOG.info("Failed to fetch regional URL mappings (using non-regional URL as fallback): " + t); - } - else if (t instanceof JsonParseException) { - LOG.warn("Failed to parse regional URL mappings (using non-regional URL as fallback): " + t); - } - else { - // never suppress errors indicating programmatic bugs or an IDE misconfiguration - LOG.error("Failed to load regional URL mappings (using non-regional URL as fallback)", t); - } - return RegionMapping.empty(); - }); - } - - /** - * Loads or retrieves a cached value of the {@link RegionMapping} corresponding to the specified region. - * Loading may fail with an {@link IOException} or {@link JsonParseException}, in which case the resulting future fails with that error. - * - * @return a {@link CompletableFuture} that resolves to either the requested mapping once it is loaded, or an error during loading - */ + /** @deprecated needlessly exposes internal data; use {@link #tryMapUrl(String, Region)} instead */ + @Deprecated(forRemoval = true) public static @NotNull CompletableFuture<@NotNull RegionMapping> loadMapping(@NotNull Region region) { return ourCache.get(region); } @@ -195,32 +163,29 @@ public final class RegionUrlMapper { return overridden != null ? overridden : CONFIG_URL_TABLE.getOrDefault(reg, CONFIG_URL_DEFAULT); } - /** + /* * Mapper for a given region. * Represents the contents of the JSON configuration loaded for a particular region * and provides the methods for applying the mapping rules found in that configuration. */ @ApiStatus.Internal public static final class RegionMapping { - private final @NotNull List myPatternReplacements; + private static final RegionMapping EMPTY = new RegionMapping(List.of()); - private RegionMapping(@NotNull List patternReplacements) { this.myPatternReplacements = patternReplacements; } + private final List myPatternReplacements; - public @NotNull String apply(@NotNull String url) { - String mappedUrl = applyOrNull(url); - return mappedUrl != null ? mappedUrl : url; + private RegionMapping(List patternReplacements) { + myPatternReplacements = patternReplacements; } - public @Nullable String applyOrNull(@NotNull String url) { - for (PatternReplacement pair : myPatternReplacements) { - String pattern = pair.pattern(); - int entry = Strings.indexOfIgnoreCase(url, pattern, 0); + public @NotNull String apply(@NotNull String url) { + for (var pair : myPatternReplacements) { + var entry = Strings.indexOfIgnoreCase(url, pair.pattern, 0); if (entry >= 0) { - String replacement = pair.replacement(); - return url.substring(0, entry) + replacement + url.substring(entry + pattern.length()); + return url.substring(0, entry) + pair.replacement + url.substring(entry + pair.pattern.length()); } } - return null; + return url; } public static @NotNull RegionMapping fromJson(@NotNull String json) throws JsonParseException { @@ -237,13 +202,9 @@ public final class RegionUrlMapper { return new RegionMapping(result); } - public static @NotNull RegionMapping empty() { - return new RegionMapping(Collections.emptyList()); - } - @Override public boolean equals(Object obj) { - return obj == this || obj instanceof RegionMapping that && Objects.equals(this.myPatternReplacements, that.myPatternReplacements); + return obj == this || obj instanceof RegionMapping that && Objects.equals(myPatternReplacements, that.myPatternReplacements); } @Override