import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import java.text.NumberFormat;
import static x30_pkg.x30_util.DynamicObject;
class main {
static class Borrowable implements AutoCloseable {
A object;
boolean borrowed = false;
Borrowable(A object) {
this.object = object;}
Borrowable() {}
synchronized void set(A object) {
this.object = object;
borrowed = false;
}
synchronized boolean canBorrow() { return object != null && !borrowed; }
synchronized boolean isBorrowed() { return borrowed; }
// returns null if object is borrowed
synchronized IAutoCloseableF0 tempBorrow() {
boolean canBorrow = canBorrow();
A o = canBorrow ? object : null;
if (canBorrow) borrowed = true;
return new IAutoCloseableF0() {
public A get() { return o; }
public void close() { unborrow(o); }
};
}
synchronized void unborrow(A o) {
if (o == object) borrowed = false;
}
public void close() {
Object o;
synchronized(this) {
o = object;
object = null;
}
{ cleanUp(o); o = null; }
}
}
static boolean cleanUp_interruptThreads = false; // experimental
static void cleanUp(Object c) {
if (c == null) return;
if (c instanceof AutoCloseable) { close_pcall((AutoCloseable) c); return; }
if (c instanceof java.util.Timer) { ((java.util.Timer) c).cancel(); return; }
if (c instanceof Collection) { cleanUp((Collection) c); return; }
if (c instanceof Map) {
for (Object o : keys((Map) c)) cleanUp(o);
for (Object o : values((Map) c)) cleanUp(o);
((Map) c).clear();
return;
}
//if (!(c instanceof Class)) ret;
try {
// 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)
List androids = (List) getOpt(c, "record_list");
for (Object android : unnull(androids))
pcallOpt(android, "dispose"); // heck we'll dispose anything
// sub-cleanup
List classes = (List) (getOpt(c, "hotwire_classes"));
if (classes != null)
for (WeakReference cc : classes) { try {
cleanUp(cc.get());
} catch (Throwable __e) { _handleException(__e); }}
// interrupt all threads (experimental, they might be doing cleanup?)
if (cleanUp_interruptThreads) {
List threads = registeredThreads(c);
if (nempty(threads)) {
print("cleanUp: Interrupting " + n2(threads, "thread") + ": " + joinWithComma(allToString(threads)));
interruptThreads(threads);
}
}
} catch (Throwable __e) { _handleException(__e); }
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) return;
for (Object c : l)
cleanUp(c);
l.clear();
}
static void close_pcall(AutoCloseable c) {
if (c != null) { try { c.close(); } catch (Throwable __e) { _handleException(__e); }}
}
static Set keys(Map map) {
return map == null ? new HashSet() : map.keySet();
}
// convenience shortcut for keys_gen
static Set keys(Object map) {
return keys((Map) map);
}
static Collection values(Map map) {
return map == null ? emptyList() : map.values();
}
// convenience shortcut for values_gen
static Collection values(Object map) {
return values((Map) map);
}
static void preCleanUp(Object c) {
if (c instanceof Collection) { for (Object o : ((Collection) c)) preCleanUp(o); return; }
callOpt(c, "licensed_off");
setOpt(c, "ping_anyActions" , true); // so ping notices
setOpt(c, "cleaningUp_flag" , true);
}
static Field setOpt_findField(Class c, String field) {
HashMap map;
synchronized(getOpt_cache) {
map = getOpt_cache.get(c);
if (map == null)
map = getOpt_makeCache(c);
}
return map.get(field);
}
static void setOpt(Object o, String field, Object value) { try {
if (o == null) return;
Class c = o.getClass();
HashMap map;
if (getOpt_cache == null)
map = getOpt_makeCache(c); // in class init
else synchronized(getOpt_cache) {
map = getOpt_cache.get(c);
if (map == null)
map = getOpt_makeCache(c);
}
if (map == getOpt_special) {
if (o instanceof Class) {
setOpt((Class) o, field, value);
return;
}
// It's probably a subclass of Map. Use raw method
setOpt_raw(o, field, value);
return;
}
Field f = map.get(field);
if (f != null)
{ smartSet(f, o, value); return; } // possible improvement: skip setAccessible
if (o instanceof DynamicObject)
{ setDyn(((DynamicObject) o), field, value); return; }
if (o instanceof IMeta)
setDyn(((IMeta) o), field, value);
} catch (Exception __e) { throw rethrow(__e); } }
static void setOpt(Class c, String field, Object value) {
if (c == null) return;
try {
Field f = setOpt_findStaticField(c, field); // TODO: optimize
if (f != null)
smartSet(f, null, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field setOpt_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) {
makeAccessible(f);
return f;
}
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static void innerCleanUp(Object c) {
// call custom cleanMeUp() and cleanMeUp_*() functions
if (!isFalse(pcallOpt(c, "cleanMeUp")))
for (String name : sorted(methodsStartingWith(c, "cleanMeUp_"))) try {
callOpt(c, name);
} catch (Throwable e) {
print("Error cleaning up: " + programID(c));
_handleException(e);
}
}
static void innerCleanUp() {
innerCleanUp(mc());
}
static Object getOpt(Object o, String field) {
return getOpt_cached(o, field);
}
static Object getOpt(String field, Object o) {
return getOpt_cached(o, field);
}
static Object getOpt_raw(Object o, String field) { try {
Field f = getOpt_findField(o.getClass(), field);
if (f == null) return null;
makeAccessible(f);
return f.get(o);
} catch (Exception __e) { throw rethrow(__e); } }
// access of static fields is not yet optimized
static Object getOpt(Class c, String field) { try {
if (c == null) return null;
Field f = getOpt_findStaticField(c, field);
if (f == null) return null;
makeAccessible(f);
return f.get(null);
} catch (Exception __e) { throw rethrow(__e); } }
static Field getOpt_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static String unnull(String s) {
return s == null ? "" : s;
}
static Collection unnull(Collection l) {
return l == null ? emptyList() : l;
}
static List unnull(List l) { return l == null ? emptyList() : l; }
static int[] unnull(int[] l) { return l == null ? emptyIntArray() : l; }
static char[] unnull(char[] l) { return l == null ? emptyCharArray() : l; }
static double[] unnull(double[] l) { return l == null ? emptyDoubleArray() : l; }
static Map unnull(Map l) {
return l == null ? emptyMap() : l;
}
static Iterable unnull(Iterable i) {
return i == null ? emptyList() : i;
}
static A[] unnull(A[] a) {
return a == null ? (A[]) emptyObjectArray() : a;
}
static BitSet unnull(BitSet b) {
return b == null ? new BitSet() : b;
}
//ifclass Symbol
static Pair unnull(Pair p) {
return p != null ? p : new Pair(null, null);
}
static int unnull(Integer i) { return i == null ? 0 : i; }
static long unnull(Long l) { return l == null ? 0L : l; }
static double unnull(Double l) { return l == null ? 0.0 : l; }
static Object pcallOpt(Object o, String method, Object... args) {
try { return callOpt(o, method, args); } catch (Throwable __e) { _handleException(__e); }
return null;
}
static volatile PersistableThrowable _handleException_lastException;
static List _handleException_onException = synchroList(ll((IVF1) (__1 -> printStackTrace2(__1))));
static boolean _handleException_showThreadCancellations = false;
static void _handleException(Throwable e) {
_handleException_lastException = persistableThrowable(e);
Throwable e2 = innerException(e);
if (e2.getClass() == RuntimeException.class && eq(e2.getMessage(), "Thread cancelled.") || e2 instanceof InterruptedException) {
if (_handleException_showThreadCancellations)
System.out.println(getStackTrace_noRecord(e2));
return;
}
for (Object f : cloneList(_handleException_onException)) try {
callF(f, e);
} catch (Throwable e3) {
try {
printStackTrace2(e3); // not using pcall here - it could lead to endless loops
} catch (Throwable e4) {
System.out.println(getStackTrace(e3));
System.out.println(getStackTrace(e4));
}
}
}
static List registeredThreads(Object o) {
Map map = (Map) (getOpt(o, "_registerThread_threads"));
if (map == null) return ll();
map.size(); // force clean-up
synchronized(map) { return asList(keys(map)); }
}
static List registeredThreads() {
_registerThread_threads.size(); // force clean-up
return asList(keys(_registerThread_threads));
}
static boolean nempty(Collection c) {
return !empty(c);
}
static boolean nempty(CharSequence s) {
return !empty(s);
}
static boolean nempty(Object[] o) { return !empty(o); }
static boolean nempty(byte[] o) { return !empty(o); }
static boolean nempty(int[] o) { return !empty(o); }
static boolean nempty(Map m) {
return !empty(m);
}
static boolean nempty(Iterator i) {
return i != null && i.hasNext();
}
static boolean nempty(Object o) { return !empty(o); }
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal