From 63ecf61cd03572924d8566283fd7553d1cff6045 Mon Sep 17 00:00:00 2001 From: Konstantin Bulenkov Date: Fri, 8 Jun 2018 13:55:56 +0300 Subject: [PATCH] Ability to use IconPatcher for icons with clashing relative paths --- .../DeprecatedDuplicatesIconPathPatcher.java | 2 +- .../com/intellij/openapi/util/IconLoader.java | 25 +++++----- .../openapi/util/IconPathPatcher.java | 47 ++++++++++++------- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/platform/util/src/com/intellij/openapi/util/DeprecatedDuplicatesIconPathPatcher.java b/platform/util/src/com/intellij/openapi/util/DeprecatedDuplicatesIconPathPatcher.java index 8f781ac033fd..b1235ca48a29 100644 --- a/platform/util/src/com/intellij/openapi/util/DeprecatedDuplicatesIconPathPatcher.java +++ b/platform/util/src/com/intellij/openapi/util/DeprecatedDuplicatesIconPathPatcher.java @@ -268,7 +268,7 @@ public class DeprecatedDuplicatesIconPathPatcher extends IconPathPatcher { @Nullable @Override - public String patchPath(String path) { + public String patchPath(String path, ClassLoader classLoader) { return ourDeprecatedIconsReplacements.get(path); } } diff --git a/platform/util/src/com/intellij/openapi/util/IconLoader.java b/platform/util/src/com/intellij/openapi/util/IconLoader.java index b44e9903de26..b3f411fd1ea4 100644 --- a/platform/util/src/com/intellij/openapi/util/IconLoader.java +++ b/platform/util/src/com/intellij/openapi/util/IconLoader.java @@ -185,14 +185,15 @@ public final class IconLoader { @Nullable public static Icon findIcon(@NotNull String path, @NotNull Class aClass, boolean computeNow, boolean strict) { String originalPath = path; - Pair patchedPath = patchPath(path); + ClassLoader classLoader = aClass.getClassLoader(); + Pair patchedPath = patchPath(path, classLoader); path = patchedPath.first; if (patchedPath.second != null) { - aClass = patchedPath.second; + classLoader = patchedPath.second; } - if (isReflectivePath(path)) return getReflectiveIcon(path, aClass.getClassLoader()); + if (isReflectivePath(path)) return getReflectiveIcon(path, classLoader); - URL myURL = findURL(path, aClass); + URL myURL = findURL(path, classLoader); if (myURL == null) { if (strict) throw new RuntimeException("Can't find icon in '" + path + "' near " + aClass); return null; @@ -200,18 +201,18 @@ public final class IconLoader { final Icon icon = findIcon(myURL); if (icon instanceof CachedImageIcon) { ((CachedImageIcon)icon).myOriginalPath = originalPath; - ((CachedImageIcon)icon).myClassLoader = aClass.getClassLoader(); + ((CachedImageIcon)icon).myClassLoader = classLoader; } return icon; } @NotNull - private static Pair patchPath(@NotNull String path) { + private static Pair patchPath(@NotNull String path, ClassLoader classLoader) { for (IconPathPatcher patcher : ourPatchers) { - String newPath = patcher.patchPath(path); + String newPath = patcher.patchPath(path, classLoader); if (newPath != null) { LOG.info("replace '" + path + "' with '" + newPath + "'"); - return Pair.create(newPath, patcher.getContextClass(path)); + return Pair.create(newPath, patcher.getContextClassLoader(path, classLoader)); } } return Pair.create(path, null); @@ -268,10 +269,10 @@ public final class IconLoader { @Nullable public static Icon findIcon(@NotNull String path, @NotNull ClassLoader classLoader) { String originalPath = path; - Pair patchedPath = patchPath(path); + Pair patchedPath = patchPath(path, null); path = patchedPath.first; if (patchedPath.second != null) { - classLoader = patchedPath.second.getClassLoader(); + classLoader = patchedPath.second; } if (isReflectivePath(path)) return getReflectiveIcon(path, classLoader); if (!StringUtil.startsWithChar(path, '/')) return null; @@ -561,10 +562,10 @@ public final class IconLoader { myScaledIconsCache.clear(); if (numberOfPatchers != ourPatchers.size()) { numberOfPatchers = ourPatchers.size(); - Pair patchedPath = patchPath(myOriginalPath); + Pair patchedPath = patchPath(myOriginalPath, null); String path = myOriginalPath == null ? null : patchedPath.first; if (patchedPath.second != null) { - myClassLoader = patchedPath.second.getClassLoader(); + myClassLoader = patchedPath.second; } if (myClassLoader != null && path != null && path.startsWith("/")) { path = path.substring(1); diff --git a/platform/util/src/com/intellij/openapi/util/IconPathPatcher.java b/platform/util/src/com/intellij/openapi/util/IconPathPatcher.java index f7af082fccd9..2a06893456ba 100644 --- a/platform/util/src/com/intellij/openapi/util/IconPathPatcher.java +++ b/platform/util/src/com/intellij/openapi/util/IconPathPatcher.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.openapi.util; import org.jetbrains.annotations.Nullable; @@ -21,16 +7,43 @@ import org.jetbrains.annotations.Nullable; * @author Konstantin Bulenkov */ public abstract class IconPathPatcher { + /** + * @deprecated + * @see #patchPath(String, Class) + */ + @Nullable + public String patchPath(String path) { + return patchPath(path, null); + } + /** * Patches the path or returns null if nothing has patched * @param path path to the icon + * @param classLoader ClassLoader of the icon is requested from * @return patched path or null */ @Nullable - public abstract String patchPath(String path); + public String patchPath(String path, ClassLoader classLoader) { + return null; + } - @Nullable + /** + * @deprecated + * @see #getContextClass(String, Class) + */ + @Deprecated public Class getContextClass(String path) { return null; } + + /** + * Return ClassLoader for icon path or returns null if nothing has patched + * @param path path to the icon + * @param originalClassLoader ClassLoader of the icon is requested from + * @return patched icon ClassLoader or null + */ + @Nullable + public ClassLoader getContextClassLoader(String path, ClassLoader originalClassLoader) { + return null; + } }