-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
PluginClassLoader.java
346 lines (299 loc) · 12.3 KB
/
PluginClassLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2000-2019 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.ide.plugins.cl;
import com.intellij.diagnostic.PluginException;
import com.intellij.ide.plugins.PluginManagerCore;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.ContainerUtilRt;
import com.intellij.util.lang.UrlClassLoader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
/**
* @author Eugene Zhuravlev
*/
public class PluginClassLoader extends UrlClassLoader {
static {
if (registerAsParallelCapable()) markParallelCapable(PluginClassLoader.class);
}
private final ClassLoader[] myParents;
private final PluginId myPluginId;
private final String myPluginVersion;
private final List<String> myLibDirectories;
public PluginClassLoader(@NotNull List<URL> urls,
@NotNull ClassLoader[] parents,
PluginId pluginId,
String version,
File pluginRoot) {
super(build().urls(urls).allowLock().useCache());
myParents = parents;
myPluginId = pluginId;
myPluginVersion = version;
myLibDirectories = ContainerUtil.newSmartList();
File libDir = new File(pluginRoot, "lib");
if (libDir.exists()) {
myLibDirectories.add(libDir.getAbsolutePath());
}
}
@Override
public Class loadClass(@NotNull String name, boolean resolve) throws ClassNotFoundException {
Class c = tryLoadingClass(name, resolve, null);
if (c == null) {
throw new ClassNotFoundException(name + " " + this);
}
return c;
}
private interface ActionWithClassloader<Result, ParameterType> {
Result execute(String name, ClassLoader classloader, ParameterType parameter);
}
private abstract static class ActionWithPluginClassLoader<Result, ParameterType> {
Result execute(String name, PluginClassLoader classloader, Set<ClassLoader> visited,
ActionWithPluginClassLoader<Result, ParameterType> actionWithPluginClassLoader,
ActionWithClassloader<Result, ParameterType> actionWithClassloader,
ParameterType parameter) {
Result resource = doExecute(name, classloader, parameter);
if (resource != null) return resource;
return classloader.processResourcesInParents(name, actionWithPluginClassLoader, actionWithClassloader, visited, parameter);
}
protected abstract Result doExecute(String name, PluginClassLoader classloader, ParameterType parameter);
}
@Nullable
private <Result, ParameterType> Result processResourcesInParents(String name,
ActionWithPluginClassLoader<Result, ParameterType> actionWithPluginClassLoader,
ActionWithClassloader<Result, ParameterType> actionWithClassloader,
Set<ClassLoader> visited, ParameterType parameter) {
for (ClassLoader parent : myParents) {
if (visited == null) visited = ContainerUtilRt.newHashSet(this);
if (!visited.add(parent)) {
continue;
}
if (parent instanceof PluginClassLoader) {
Result resource = actionWithPluginClassLoader.execute(name, (PluginClassLoader)parent, visited, actionWithPluginClassLoader,
actionWithClassloader, parameter);
if (resource != null) {
return resource;
}
continue;
}
Result resource = actionWithClassloader.execute(name, parent, parameter);
if (resource != null) return resource;
}
return null;
}
private static final ActionWithPluginClassLoader<Class, Void> loadClassInPluginCL = new ActionWithPluginClassLoader<Class, Void>() {
@Override
Class execute(String name,
PluginClassLoader classloader,
Set<ClassLoader> visited,
ActionWithPluginClassLoader<Class, Void> actionWithPluginClassLoader,
ActionWithClassloader<Class, Void> actionWithClassloader,
Void parameter) {
return classloader.tryLoadingClass(name, false, visited);
}
@Override
protected Class doExecute(String name, PluginClassLoader classloader, Void parameter) {
return null;
}
};
private static final ActionWithClassloader<Class, Void> loadClassInCl = new ActionWithClassloader<Class, Void>() {
@Override
public Class execute(String name, ClassLoader classloader, Void parameter) {
try {
return classloader.loadClass(name);
}
catch (ClassNotFoundException ignoreAndContinue) {
// Ignore and continue
}
return null;
}
};
// Changed sequence in which classes are searched, this is essential if plugin uses library,
// a different version of which is used in IDEA.
@Nullable
private Class tryLoadingClass(@NotNull String name, boolean resolve, @Nullable Set<ClassLoader> visited) {
Class c = null;
if (!mustBeLoadedByPlatform(name)) {
c = loadClassInsideSelf(name);
}
if (c == null) {
c = processResourcesInParents(name, loadClassInPluginCL, loadClassInCl, visited, null);
}
if (c != null) {
if (resolve) {
resolveClass(c);
}
return c;
}
return null;
}
private static final Set<String> KOTLIN_STDLIB_CLASSES_USED_IN_SIGNATURES = ContainerUtil.set(
"kotlin.sequences.Sequence",
"kotlin.Unit",
"kotlin.Pair",
"kotlin.Triple",
"kotlin.jvm.internal.DefaultConstructorMarker",
"kotlin.properties.ReadWriteProperty",
"kotlin.properties.ReadOnlyProperty"
);
private static boolean mustBeLoadedByPlatform(String className) {
if (className.startsWith("java.")) return true;
//some commonly used classes from kotlin-runtime must be loaded by the platform classloader. Otherwise if a plugin bundles its own version
// of kotlin-runtime.jar it won't be possible to call platform's methods with these types in signatures from such a plugin.
//We assume that these classes don't change between Kotlin versions so it's safe to always load them from platform's kotlin-runtime.
return className.startsWith("kotlin.") && (className.startsWith("kotlin.jvm.functions.") ||
(className.startsWith("kotlin.reflect.") && className.indexOf('.', 15 /* "kotlin.reflect".length */) < 0) ||
KOTLIN_STDLIB_CLASSES_USED_IN_SIGNATURES.contains(className));
}
@Nullable
private Class loadClassInsideSelf(@NotNull String name) {
synchronized (getClassLoadingLock(name)) {
Class c = findLoadedClass(name);
if (c != null) {
return c;
}
try {
c = _findClass(name);
}
catch (IncompatibleClassChangeError | UnsupportedClassVersionError e) {
throw new PluginException("While loading class " + name + ": " + e.getMessage(), e, myPluginId);
}
if (c != null) {
PluginManagerCore.addPluginClass(myPluginId);
}
return c;
}
}
private static final ActionWithPluginClassLoader<URL, Void> findResourceInPluginCL = new ActionWithPluginClassLoader<URL, Void>() {
@Override
protected URL doExecute(String name, PluginClassLoader classloader, Void parameter) {
return classloader.findOwnResource(name);
}
};
private static final ActionWithClassloader<URL, Void> findResourceInCl = new ActionWithClassloader<URL, Void>() {
@Override
public URL execute(String name, ClassLoader classloader, Void parameter) {
return classloader.getResource(name);
}
};
@Override
public URL findResource(String name) {
URL resource = findOwnResource(name);
if (resource != null) return resource;
return processResourcesInParents(name, findResourceInPluginCL, findResourceInCl, null, null);
}
@Nullable
private URL findOwnResource(String name) {
URL resource = super.findResource(name);
if (resource != null) return resource;
return null;
}
private static final ActionWithPluginClassLoader<InputStream, Void>
getResourceAsStreamInPluginCL = new ActionWithPluginClassLoader<InputStream, Void>() {
@Override
protected InputStream doExecute(String name, PluginClassLoader classloader, Void parameter) {
return classloader.getOwnResourceAsStream(name);
}
};
private static final ActionWithClassloader<InputStream, Void> getResourceAsStreamInCl = new ActionWithClassloader<InputStream, Void>() {
@Override
public InputStream execute(String name, ClassLoader classloader, Void parameter) {
return classloader.getResourceAsStream(name);
}
};
@Override
public InputStream getResourceAsStream(String name) {
InputStream stream = getOwnResourceAsStream(name);
if (stream != null) return stream;
return processResourcesInParents(name, getResourceAsStreamInPluginCL, getResourceAsStreamInCl, null, null);
}
@Nullable
private InputStream getOwnResourceAsStream(String name) {
InputStream stream = super.getResourceAsStream(name);
if (stream != null) return stream;
return null;
}
private static final ActionWithPluginClassLoader<Void, List<Enumeration<URL>>>
findResourcesInPluginCL = new ActionWithPluginClassLoader<Void, List<Enumeration<URL>>>() {
@Override
protected Void doExecute(String name,
PluginClassLoader classloader,
List<Enumeration<URL>> enumerations) {
try {
enumerations.add(classloader.findOwnResources(name));
} catch (IOException ignore) {}
return null;
}
};
private static final ActionWithClassloader<Void, List<Enumeration<URL>>>
findResourcesInCl = new ActionWithClassloader<Void, List<Enumeration<URL>>>() {
@Override
public Void execute(String name, ClassLoader classloader, List<Enumeration<URL>> enumerations) {
try {
enumerations.add(classloader.getResources(name));
} catch (IOException ignore) {}
return null;
}
};
@Override
public Enumeration<URL> findResources(String name) throws IOException {
@SuppressWarnings("unchecked") List<Enumeration<URL>> resources = new ArrayList<>();
resources.add(findOwnResources(name));
processResourcesInParents(name, findResourcesInPluginCL, findResourcesInCl, null, resources);
return new DeepEnumeration(resources.toArray(new Enumeration[resources.size()]));
}
private Enumeration<URL> findOwnResources(String name) throws IOException {
return super.findResources(name);
}
@SuppressWarnings("UnusedDeclaration")
public void addLibDirectories(@NotNull Collection<String> libDirectories) {
myLibDirectories.addAll(libDirectories);
}
@Override
protected String findLibrary(String libName) {
if (!myLibDirectories.isEmpty()) {
String libFileName = System.mapLibraryName(libName);
ListIterator<String> i = myLibDirectories.listIterator(myLibDirectories.size());
while (i.hasPrevious()) {
File libFile = new File(i.previous(), libFileName);
if (libFile.exists()) {
return libFile.getAbsolutePath();
}
}
}
return null;
}
public PluginId getPluginId() {
return myPluginId;
}
@Override
public String toString() {
return "PluginClassLoader[" + myPluginId + ", " + myPluginVersion + "] " + super.toString();
}
private static class DeepEnumeration implements Enumeration<URL> {
private final Enumeration<URL>[] myEnumerations;
private int myIndex;
DeepEnumeration(Enumeration<URL>[] enumerations) {
myEnumerations = enumerations;
}
@Override
public boolean hasMoreElements() {
while (myIndex < myEnumerations.length) {
Enumeration<URL> e = myEnumerations[myIndex];
if (e != null && e.hasMoreElements()) return true;
myIndex++;
}
return false;
}
@Override
public URL nextElement() {
if (!hasMoreElements()) {
throw new NoSuchElementException();
}
return myEnumerations[myIndex].nextElement();
}
}
}