Skip to content

Commit a3f6ede

Browse files
committed
updated PluginClassLoader_Fork
1 parent 4592efe commit a3f6ede

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

src/main/liveplugin/implementation/pluginrunner/PluginClassLoader_Fork.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
import com.intellij.util.lang.Resource;
1515
import com.intellij.util.lang.UrlClassLoader;
1616
import com.intellij.util.ui.EDT;
17-
import org.jetbrains.annotations.*;
17+
import org.jetbrains.annotations.ApiStatus;
18+
import org.jetbrains.annotations.NonNls;
19+
import org.jetbrains.annotations.NotNull;
20+
import org.jetbrains.annotations.Nullable;
1821

1922
import java.io.*;
2023
import java.net.URL;
2124
import java.nio.file.Files;
2225
import java.nio.file.Path;
2326
import java.nio.file.Paths;
24-
import java.security.CodeSource;
25-
import java.security.ProtectionDomain;
26-
import java.security.cert.Certificate;
2727
import java.util.*;
2828
import java.util.concurrent.atomic.AtomicInteger;
2929
import java.util.concurrent.atomic.AtomicLong;
3030
import java.util.function.BiFunction;
3131
import java.util.function.Function;
3232

3333
/**
34-
* Fork of PluginClassLoader_Fork
34+
* Fork of com.intellij.ide.plugins.cl.PluginClassLoader
3535
* because its an internal IJ API and it has been changing in IJ 2020.2 causing LivePlugin to break.
3636
* The assumption is that using fork will make LivePlugin forward-compatible with more IJ versions
3737
* given that PluginClassLoader_Fork implementation compatibility is more stable than PluginClassLoader API.
@@ -52,9 +52,6 @@ public final class PluginClassLoader_Fork extends UrlClassLoader implements Plug
5252

5353
private static final Set<String> KOTLIN_STDLIB_CLASSES_USED_IN_SIGNATURES;
5454

55-
// avoid capturing reference to classloader in AccessControlContext
56-
private static final ProtectionDomain PROTECTION_DOMAIN = new ProtectionDomain(new CodeSource(null, (Certificate[]) null), null);
57-
5855
static {
5956
@SuppressWarnings("SSBasedInspection")
6057
Set<String> kotlinStdlibClassesUsedInSignatures = new HashSet<>(Arrays.asList(
@@ -80,11 +77,17 @@ public final class PluginClassLoader_Fork extends UrlClassLoader implements Plug
8077
"kotlin.coroutines.CoroutineContext",
8178
"kotlin.coroutines.CoroutineContext$Element",
8279
"kotlin.coroutines.CoroutineContext$Key",
80+
"kotlin.Result",
81+
"kotlin.Result$Failure",
82+
"kotlin.Result$Companion",
8383
// Even though it's internal class, it can leak (and it does) into API surface because it's exposed by public
8484
// `kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED` property
8585
"kotlin.coroutines.intrinsics.CoroutineSingletons",
8686
"kotlin.coroutines.AbstractCoroutineContextElement",
87-
"kotlin.coroutines.AbstractCoroutineContextKey"
87+
"kotlin.coroutines.AbstractCoroutineContextKey",
88+
"kotlin.coroutines.jvm.internal.ContinuationImpl", // IDEA-295189
89+
"kotlin.coroutines.jvm.internal.BaseContinuationImpl", // IDEA-295189
90+
"kotlin.coroutines.jvm.internal.CoroutineStackFrame" // IDEA-295189
8891
));
8992
String classes = System.getProperty("idea.kotlin.classes.used.in.signatures");
9093
if (classes != null) {
@@ -225,7 +228,7 @@ public Class<?> loadClass(@NotNull String name, boolean resolve) throws ClassNot
225228
}
226229

227230
/**
228-
* See https://stackoverflow.com/a/5428795 about resolve flag.
231+
* See <a href="https://stackoverflow.com/a/5428795">https://stackoverflow.com/a/5428795</a> about resolve flag.
229232
*/
230233
@Override
231234
public @Nullable Class<?> tryLoadingClass(@NotNull String name, boolean forceLoadFromSubPluginClassloader)
@@ -382,9 +385,23 @@ private static boolean mustBeLoadedByPlatform(@NonNls String className) {
382385
// some commonly used classes from kotlin-runtime must be loaded by the platform classloader. Otherwise, if a plugin bundles its own version
383386
// of kotlin-runtime.jar it won't be possible to call platform's methods with these types in signatures from such a plugin.
384387
// We assume that these classes don't change between Kotlin versions, so it's safe to always load them from platform's kotlin-runtime.
385-
return className.startsWith("kotlin.") && (className.startsWith("kotlin.jvm.functions.") ||
386-
(className.startsWith("kotlin.reflect.") &&
387-
className.indexOf('.', 15 /* "kotlin.reflect".length */) < 0) ||
388+
return className.startsWith("kotlin.") &&
389+
(className.startsWith("kotlin.jvm.functions.") ||
390+
// Those are kotlin-reflect related classes but unfortunately, they are placed in kotlin-stdlib. Since we always
391+
// want to load reflect from platform, we should force those classes with platform classloader as well
392+
className.startsWith("kotlin.reflect.") ||
393+
className.startsWith("kotlin.jvm.internal.CallableReference") ||
394+
className.startsWith("kotlin.jvm.internal.ClassReference") ||
395+
className.startsWith("kotlin.jvm.internal.FunInterfaceConstructorReference") ||
396+
className.startsWith("kotlin.jvm.internal.FunctionReference") ||
397+
className.startsWith("kotlin.jvm.internal.MutablePropertyReference") ||
398+
className.startsWith("kotlin.jvm.internal.PropertyReference") ||
399+
className.startsWith("kotlin.jvm.internal.TypeReference") ||
400+
className.equals("kotlin.jvm.internal.Lambda") ||
401+
className.startsWith("kotlin.jvm.internal.LocalVariableReference") ||
402+
className.startsWith("kotlin.jvm.internal.MutableLocalVariableReference") ||
403+
className.equals("kotlin.jvm.internal.ReflectionFactory") ||
404+
className.equals("kotlin.jvm.internal.Reflection") ||
388405
KOTLIN_STDLIB_CLASSES_USED_IN_SIGNATURES.contains(className));
389406
}
390407

@@ -510,7 +527,7 @@ private void logClass(@NotNull String name, @NotNull Writer logStream, @Nullable
510527
return doFindResource(name, f1, f2);
511528
}
512529

513-
private <T> @Nullable T doFindResource(String name, Function<Resource, T> f1, BiFunction<ClassLoader, String, T> f2) {
530+
private <T> @Nullable T doFindResource(String name, Function<? super Resource, ? extends T> f1, BiFunction<? super ClassLoader, ? super String, ? extends T> f2) {
514531
String canonicalPath = toCanonicalPath(name);
515532

516533
Resource resource = classPath.findResource(canonicalPath);
@@ -601,10 +618,10 @@ public String toString() {
601618
}
602619

603620
private static final class DeepEnumeration implements Enumeration<URL> {
604-
private final @NotNull List<Enumeration<URL>> list;
621+
private final @NotNull List<? extends Enumeration<URL>> list;
605622
private int myIndex;
606623

607-
DeepEnumeration(@NotNull List<Enumeration<URL>> enumerations) {
624+
private DeepEnumeration(@NotNull List<? extends Enumeration<URL>> enumerations) {
608625
list = enumerations;
609626
}
610627

@@ -629,10 +646,6 @@ public URL nextElement() {
629646
}
630647
}
631648

632-
protected ProtectionDomain getProtectionDomain() {
633-
return PROTECTION_DOMAIN;
634-
}
635-
636649
private static void flushDebugLog() {
637650
if (logStream != null) {
638651
try {

0 commit comments

Comments
 (0)