Ability to use IconPatcher for icons with clashing relative paths

This commit is contained in:
Konstantin Bulenkov
2018-06-08 13:57:35 +03:00
parent f6acc52774
commit 63ecf61cd0
3 changed files with 44 additions and 30 deletions
@@ -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);
}
}
@@ -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<String, Class> patchedPath = patchPath(path);
ClassLoader classLoader = aClass.getClassLoader();
Pair<String, ClassLoader> 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<String, Class> patchPath(@NotNull String path) {
private static Pair<String, ClassLoader> 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<String, Class> patchedPath = patchPath(path);
Pair<String, ClassLoader> 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<String, Class> patchedPath = patchPath(myOriginalPath);
Pair<String, ClassLoader> 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);
@@ -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;
}
}