Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

73
LINES

< > BotCompany Repo | #1001931 // cleanUp - try to clean up a program/object/class

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (10512L/59K).

1  
sbool cleanUp_interruptThreads; // experimental
2  
3  
static void cleanUp(O c) {
4  
  if (c == null) ret;
5  
  
6  
  if (c cast AutoCloseable) ret with close_pcall(c);
7  
  
8  
  if (c cast java.util.Timer) ret with c.cancel();
9  
  
10  
  if (c cast Collection) { cleanUp(c); ret; }
11  
  if (c cast Map) {
12  
    for (O o : keysList(c)) cleanUp(o);
13  
    for (O o : valuesList(c)) cleanUp(o);
14  
    syncClear(c);
15  
    ret;
16  
  }
17  
  //if (!(c instanceof Class)) ret;
18  
  
19  
  pcall {
20  
    // revoke license
21  
    
22  
    preCleanUp(c);
23  
    
24  
    // unpause
25  
    
26  
    setOpt_raw(c, "ping_pauseAll", false);
27  
    
28  
    // call custom cleanMeUp() and cleanMeUp_*() functions
29  
    
30  
    innerCleanUp(c);
31  
        
32  
    // Java spec says finalize should only be called by GC,
33  
    // but we care to differ.
34  
    // Edit: Not anymore (illegal access warnings)
35  
    /*if (isTrue(vmMap_get('callFinalize)))
36  
      pcallOpt(c, "finalize");*/
37  
38  
    // remove all virtual bots (hope this works)
39  
    
40  
    L androids = (L) getOpt(c, "record_list");
41  
    for (O android : unnull(androids))
42  
      pcallOpt(android, "dispose"); // heck we'll dispose anything
43  
44  
    // sub-cleanup
45  
    
46  
    L<WeakReference> classes = cast getOpt(c, "hotwire_classes");
47  
    if (classes != null)
48  
      for (WeakReference cc : classes) pcall {
49  
        cleanUp(cc.get());
50  
      }
51  
      
52  
    // interrupt all threads (experimental, they might be doing cleanup?)
53  
    
54  
    if (cleanUp_interruptThreads) {
55  
      L<Thread> threads = registeredThreads(c);
56  
      if (nempty(threads)) {
57  
        print("cleanUp: Interrupting " + n2(threads, "thread") + ": " + joinWithComma(allToString(threads)));
58  
        interruptThreads(threads);
59  
      }
60  
    }
61  
  }
62  
  
63  
  setOpt_raw(c, cleaningUp_flag := false);
64  
  if (c instanceof Class && ((Class) c).getName().equals("main"))
65  
    retireClassLoader(((Class) c).getClassLoader());
66  
 }
67  
68  
static void cleanUp(Collection l) {
69  
  if (l == null) ret;
70  
  for (O c : l)
71  
    cleanUp(c);
72  
  l.clear();
73  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001931
Snippet name: cleanUp - try to clean up a program/object/class
Eternal ID of this version: #1001931/40
Text MD5: 44ed69c1e537371a40aff24b75609fbf
Transpilation MD5: 551a918a24aededdb1651ba4f422dd56
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-22 14:24:02
Source code size: 1983 bytes / 73 lines
Pitched / IR pitched: No / No
Views / Downloads: 792 / 1550
Version history: 39 change(s)
Referenced in: [show references]