Libraryless. Click here for Pure Java version (11152L/64K).
1 | // Idea: Every class loader makes only a single class so they can be |
2 | // garbage collected more easily. |
3 | |
4 | srecord noeq LASMultiClassLoader(ClassLoader parent) is ILASClassLoader, AutoCloseable { |
5 | settable bool rememberClassBytes; |
6 | |
7 | new WeakValueMap<S, Class> definedClasses; |
8 | Lock lock = lock(); |
9 | |
10 | event classDefined(Class c); |
11 | |
12 | *(Class parent) { this(parent.getClassLoader()); } |
13 | |
14 | { |
15 | definedClasses.onValueForKeyReleased(key -> |
16 | printWithMS(":: CLASS UNLOADED :: " + key)); |
17 | } |
18 | |
19 | selfType runnablesReferenceQueue(RunnablesReferenceQueue queue) { |
20 | definedClasses.queue(queue); |
21 | this; |
22 | } |
23 | |
24 | int nClassesLoaded() { ret l(definedClasses); } |
25 | |
26 | Cl<Class> definedClasses() { |
27 | ret values(definedClasses.snapshot()); |
28 | } |
29 | |
30 | public Class<?> defineLASClass(S name, IF0<byte[]> generateClass) { |
31 | lock lock; |
32 | |
33 | try object definedClasses.get(name); |
34 | |
35 | var cl = new IndividualClassLoader().rememberClassBytes(rememberClassBytes); |
36 | print("Generating class " + name); |
37 | Class c = cl.defineAClass(name, generateClass!); |
38 | definedClasses.put(name, c); |
39 | classDefined(c); |
40 | ret c; |
41 | } |
42 | |
43 | close { |
44 | dispose definedClasses; |
45 | } |
46 | |
47 | class IndividualClassLoader extends InMemoryClassLoader { |
48 | *() { super(parent); } |
49 | |
50 | // Allows one script-defined class to see another one |
51 | protected Class<?> findClass(S name) throws ClassNotFoundException { |
52 | try object definedClasses.get(name); |
53 | ret super.findClass(name); |
54 | } |
55 | } |
56 | } |
Began life as a copy of #1035083
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035329 |
Snippet name: | LASMultiClassLoader |
Eternal ID of this version: | #1035329/16 |
Text MD5: | cb12f50bc3481d766a0626dd732fba7f |
Transpilation MD5: | 4879bf0ec62bed41b42c5fa6915fd135 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-12-13 19:21:14 |
Source code size: | 1549 bytes / 56 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 196 / 344 |
Version history: | 15 change(s) |
Referenced in: | [show references] |