@@ -26,7 +26,7 @@ import java.util.zip.ZipFile
26
26
Class resolving:
27
27
28
28
|
29
- `- Resolve standard classes: by super class loader.
29
+ `- Resolve standard classes: hard linked by console (@see AllDependenciesClassesHolder)
30
30
`- Resolve classes in shared libraries (Shared in all plugins)
31
31
|
32
32
|-===== SANDBOX =====
@@ -35,6 +35,7 @@ Class resolving:
35
35
`- Resolve classes in independent libraries (Can only be loaded by current plugin)
36
36
`- Resolve classes in current jar.
37
37
`- Resolve classes from other plugin jar
38
+ `- Resolve by AppClassLoader
38
39
39
40
*/
40
41
@@ -58,6 +59,17 @@ internal class DynLibClassLoader(
58
59
}
59
60
}
60
61
62
+ internal fun loadClassInThisClassLoader (name : String ): Class <* >? {
63
+ synchronized(getClassLoadingLock(name)) {
64
+ findLoadedClass(name)?.let { return it }
65
+ try {
66
+ return findClass(name)
67
+ } catch (ignored: ClassNotFoundException ) {
68
+ }
69
+ }
70
+ return null
71
+ }
72
+
61
73
internal fun addLib (url : URL ) {
62
74
addURL(url)
63
75
}
@@ -75,6 +87,7 @@ internal class DynLibClassLoader(
75
87
internal class JvmPluginClassLoaderN : URLClassLoader {
76
88
val file: File
77
89
val ctx: JvmPluginsLoadingCtx
90
+ val sharedLibrariesLogger: DynLibClassLoader
78
91
79
92
val dependencies: MutableCollection <JvmPluginClassLoaderN > = hashSetOf()
80
93
@@ -85,6 +98,7 @@ internal class JvmPluginClassLoaderN : URLClassLoader {
85
98
private constructor (file: File , ctx: JvmPluginsLoadingCtx , unused: Unit ) : super (
86
99
arrayOf(), ctx.sharedLibrariesLoader
87
100
) {
101
+ this .sharedLibrariesLogger = ctx.sharedLibrariesLoader
88
102
this .file = file
89
103
this .ctx = ctx
90
104
init0()
@@ -94,6 +108,7 @@ internal class JvmPluginClassLoaderN : URLClassLoader {
94
108
file.name,
95
109
arrayOf(), ctx.sharedLibrariesLoader
96
110
) {
111
+ this .sharedLibrariesLogger = ctx.sharedLibrariesLoader
97
112
this .file = file
98
113
this .ctx = ctx
99
114
init0()
@@ -184,12 +199,26 @@ internal class JvmPluginClassLoaderN : URLClassLoader {
184
199
internal fun resolvePluginPublicClass (name : String ): Class <* >? {
185
200
if (pluginMainPackages.contains(name.pkgName())) {
186
201
if (declaredFilter?.isExported(name) == false ) return null
187
- return loadClass(name)
202
+ synchronized(getClassLoadingLock(name)) {
203
+ findLoadedClass(name)?.let { return it }
204
+ return super .findClass(name)
205
+ }
188
206
}
189
207
return null
190
208
}
191
209
192
- override fun findClass (name : String ): Class <* > {
210
+ override fun loadClass (name : String ): Class <* > {
211
+ if (name.startsWith(" io.netty" ) || name in AllDependenciesClassesHolder .allclasses) {
212
+ return AllDependenciesClassesHolder .appClassLoader.loadClass(name)
213
+ }
214
+ if (name.startsWith(" net.mamoe.mirai." )) { // Avoid plugin classing cheating
215
+ try {
216
+ return AllDependenciesClassesHolder .appClassLoader.loadClass(name)
217
+ } catch (ignored: ClassNotFoundException ) {
218
+ }
219
+ }
220
+ sharedLibrariesLogger.loadClassInThisClassLoader(name)?.let { return it }
221
+
193
222
// Search dependencies first
194
223
dependencies.forEach { dependency ->
195
224
dependency.resolvePluginSharedLibAndPluginClass(name)?.let { return it }
@@ -202,15 +231,18 @@ internal class JvmPluginClassLoaderN : URLClassLoader {
202
231
}
203
232
204
233
try {
205
- return super .findClass(name)
234
+ synchronized(getClassLoadingLock(name)) {
235
+ findLoadedClass(name)?.let { return it }
236
+ return super .findClass(name)
237
+ }
206
238
} catch (error: ClassNotFoundException ) {
207
- // Finally, try search from other plugins
239
+ // Finally, try search from other plugins and console system
208
240
ctx.pluginClassLoaders.forEach { other ->
209
- if (other != = this ) {
241
+ if (other != = this && other !in dependencies ) {
210
242
other.resolvePluginPublicClass(name)?.let { return it }
211
243
}
212
244
}
213
- throw error
245
+ return AllDependenciesClassesHolder .appClassLoader.loadClass(name)
214
246
}
215
247
}
216
248
0 commit comments