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 (2832L/18K).

sbool cleanUp_interruptThreads; // experimental

static void cleanUp(O c) {
  if (c == null) ret;
  
  if (c cast AutoCloseable) ret with close_pcall(c);
  
  if (c cast java.util.Timer) ret with c.cancel();
  
  if (c instanceof Collection) { cleanUp((Collection) c); ret; }
  if (c instanceof Map) {
    for (O o : keys((Map) c)) cleanUp(o);
    for (O o : values((Map) c)) cleanUp(o);
    ((Map) c).clear();
    ret;
  }
  //if (!(c instanceof Class)) ret;
  
  pcall {
    // revoke license
    
    preCleanUp(c);
    
    // unpause
    
    setOpt(c, "ping_pauseAll", false);
    
    // call custom cleanMeUp() and cleanMeUp_*() functions
    
    innerCleanUp(c);
        
    // Java spec says finalize should only be called by GC,
    // but we care to differ.
    // Edit: Not anymore (illegal access warnings)
    /*if (isTrue(vmMap_get('callFinalize)))
      pcallOpt(c, "finalize");*/

    // remove all virtual bots (hope this works)
    
    L androids = (L) getOpt(c, "record_list");
    for (O android : unnull(androids))
      pcallOpt(android, "dispose"); // heck we'll dispose anything

    // sub-cleanup
    
    L<WeakReference> classes = cast getOpt(c, "hotwire_classes");
    if (classes != null)
      for (WeakReference cc : classes) pcall {
        cleanUp(cc.get());
      }
      
    // interrupt all threads (experimental, they might be doing cleanup?)
    
    if (cleanUp_interruptThreads) {
      L<Thread> threads = registeredThreads(c);
      if (nempty(threads)) {
        print("cleanUp: Interrupting " + n2(threads, "thread") + ": " + joinWithComma(allToString(threads)));
        interruptThreads(threads);
      }
    }
  }
  
  setOpt(c, cleaningUp_flag := false);
  if (c instanceof Class && ((Class) c).getName().equals("main"))
    retireClassLoader(((Class) c).getClassLoader());
 }

static void cleanUp(Collection l) {
  if (l == null) ret;
  for (O c : l)
    cleanUp(c);
  l.clear();
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001931
Snippet name: cleanUp - try to clean up a program/object/class
Eternal ID of this version: #1001931/38
Text MD5: bba5133c9a169b161ba5e48a684af056
Transpilation MD5: 9e2d4216cff22950285c44f8a8af7500
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-31 22:43:06
Source code size: 2009 bytes / 73 lines
Pitched / IR pitched: No / No
Views / Downloads: 549 / 1178
Version history: 37 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1001931 & http://1001931.tinybrain.de