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 void dm_profileThisThread(Runnable r) { dm_profileThisThread(poorMansProfiling_defaultInterval(), r); }
static void dm_profileThisThread(int interval, Runnable r) {
dm_showText("Profile", profileThisThreadToString(interval, r));
}
static A dm_profileThisThread(IF0 f) { return dm_profileThisThread(poorMansProfiling_defaultInterval(), f); }
static A dm_profileThisThread(int interval, IF0 f) {
Var v = new Var();
dm_profileThisThread(interval, new Runnable() { public void run() { try { v.set(f.get()) ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "v.set(f.get())"; }});
return v.get();
}
// in ms
static int poorMansProfiling_defaultInterval() {
return 25;
}
static String dm_showText(String text) {
return dm_showNewModuleWithFields("#1016122/TextArea", "text", text);
}
static String dm_showText(String title, String text) {
String mod = dm_showText(text);
dm_setModuleName(mod, title);
return mod;
}
// convenience synonym for dm_showLines
static String dm_showText(Iterable l) {
return dm_showLines(l);
}
static String profileThisThreadToString(Runnable r) { return profileThisThreadToString(poorMansProfiling_defaultInterval(), r); }
static String profileThisThreadToString(int interval, Runnable r) {
poorMansProfiling(interval, currentThread());
try {
r.run();
} catch (Throwable __e) { _handleException(__e); }
return poorMansProfiling_stopAndRenderResults();
}
static Pair profileThisThreadToString(IF0 f) { return profileThisThreadToString(poorMansProfiling_defaultInterval(), f); }
static Pair profileThisThreadToString(int interval, IF0 f) {
Var var = new Var();
String profile = profileThisThreadToString(interval, new Runnable() { public void run() { try { var.set(callF(f)) ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "var.set(callF(f))"; }});
return pair(var.get(), profile);
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static String dm_showNewModuleWithFields(String moduleLibID, Object... params) {
return dm_showNewModuleWithParams(moduleLibID, params);
}
static void dm_setModuleName(Object module, String name) {
dm_callModule(module, "setModuleName", name);
}
static void dm_setModuleName(String name) {
dm_setModuleName(dm_current_mandatory_generic(), name);
}
static String dm_showLines(Iterable l) {
return dm_showText(lines(l));
}
static MultiSet poorMansProfiling_results = new MultiSet();
static int poorMansProfiling_samples;
static java.util.Timer poorMansProfiling_timer;
static Lock poorMansProfiling_lock = lock();
static Thread poorMansProfiling_threadToSample; // otherwise sample all threads
static void poorMansProfiling() {
poorMansProfiling(poorMansProfiling_defaultInterval());
}
static void poorMansProfiling(Thread thread) {
poorMansProfiling(poorMansProfiling_defaultInterval(), thread);
}
static void poorMansProfiling(int interval) {
poorMansProfiling(interval, null);
}
static void poorMansProfiling(int interval, Thread thread) {
Lock __0 = poorMansProfiling_lock; lock(__0); try {
poorMansProfiling_threadToSample = thread;
poorMansProfiling_stop();
poorMansProfiling_clear();
poorMansProfiling_timer = doEvery_daemon(interval, new Runnable() { public void run() { try {
Map map =
poorMansProfiling_threadToSample != null
? litmap(poorMansProfiling_threadToSample, poorMansProfiling_threadToSample.getStackTrace())
: runnableThreadsWithStackTraces();
Lock __1 = poorMansProfiling_lock; lock(__1); try {
poorMansProfiling_samples++;
for (Thread t : keys(map)) {
{ if (isSystemThread(t)) continue; }
StringBuilder buf = new StringBuilder();
for (StackTraceElement e : map.get(t))
buf.append(e).append("\n");
poorMansProfiling_results.add(str(buf));
}
} finally { unlock(__1); }
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Map map =\r\n poorMansProfiling_threadToSampl..."; }});
} finally { unlock(__0); } }
static void poorMansProfiling_stop() {
Lock __2 = poorMansProfiling_lock; lock(__2); try {
if (poorMansProfiling_timer != null) {
stopTimer(poorMansProfiling_timer);
poorMansProfiling_timer = null;
}
} finally { unlock(__2); } }
static void poorMansProfiling_clear() {
Lock __3 = poorMansProfiling_lock; lock(__3); try {
poorMansProfiling_results.clear();
poorMansProfiling_samples = 0;
} finally { unlock(__3); } }
static MultiSet poorMansProfiling_results() {
return new MultiSet(poorMansProfiling_results);
}
static MultiSet poorMansProfiling_stopAndGetResults() {
Lock __4 = poorMansProfiling_lock; lock(__4); try {
poorMansProfiling_stop();
return poorMansProfiling_results();
} finally { unlock(__4); } }
static Thread currentThread() {
return Thread.currentThread();
}
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 String poorMansProfiling_stopAndRenderResults() {
return poorMansProfiling_renderFullResults(poorMansProfiling_stopAndGetResults());
}
static Map> callF_cache = newDangerousWeakHashMap();
static A callF(F0 f) {
return f == null ? null : f.get();
}
static B callF(F1 f, A a) {
return f == null ? null : f.get(a);
}
static A callF(IF0 f) {
return f == null ? null : f.get();
}
static B callF(IF1 f, A a) {
return f == null ? null : f.get(a);
}
static C callF(IF2 f, A a, B b) {
return f == null ? null : f.get(a, b);
}
static void callF(VF1 f, A a) {
if (f != null) f.get(a);
}
static Object callF(Runnable r) { { if (r != null) r.run(); } return null; }
static Object callF(Object f, Object... args) {
if (f instanceof String)
return callMCWithVarArgs((String) f, args); // possible SLOWDOWN over callMC
return safeCallF(f, args);
}
static Object safeCallF(Object f, Object... args) {
if (f instanceof Runnable) {
((Runnable) f).run();
return null;
}
if (f == null) return null;
Class c = f.getClass();
ArrayList methods;
synchronized(callF_cache) {
methods = callF_cache.get(c);
if (methods == null)
methods = callF_makeCache(c);
}
int n = l(methods);
if (n == 0) {
throw fail("No get method in " + getClassName(c));
}
if (n == 1) return invokeMethod(methods.get(0), f, args);
for (int i = 0; i < n; i++) {
Method m = methods.get(i);
if (call_checkArgs(m, args, false))
return invokeMethod(m, f, args);
}
throw fail("No matching get method in " + getClassName(c));
}
// used internally
static ArrayList callF_makeCache(Class c) {
ArrayList l = new ArrayList();
Class _c = c;
do {
for (Method m : _c.getDeclaredMethods())
if (m.getName().equals("get")) {
makeAccessible(m);
l.add(m);
}
if (!l.isEmpty()) break;
_c = _c.getSuperclass();
} while (_c != null);
callF_cache.put(c, l);
return l;
}
static Pair pair(A a, B b) {
return new Pair(a, b);
}
static Pair pair(A a) {
return new Pair(a, a);
}
static void _handleError(Error e) {
call(javax(), "_handleError", e);
}
static String dm_showNewModuleWithParams(String moduleLibID, Object... params) {
String moduleID = dm_makeNewModuleWithParams(moduleLibID, params);
dm_showModule(moduleID);
return moduleID;
}
static Object dm_callModule(Object moduleOrID, String method, Object... args) {
Object mod = dm_getModule(moduleOrID);
if (mod == null) return null;
AutoCloseable __1 = dm_enter(mod); try {
return call(mod, method, args);
} finally { _close(__1); }}
static Object dm_current_mandatory_generic() {
return assertNotNull("No module set", dm_current_generic());
}
static String lines(Iterable lines) { return fromLines(lines); }
static String lines(Object[] lines) { return fromLines(asList(lines)); }
static List lines(String s) { return toLines(s); }
// convenience map call
static String lines(Iterable l, IF1 f) {
return mapToLines(l, f);
}
static void lock(Lock lock) { try {
ping();
if (lock == null) return;
try {
vmBus_send("locking", lock, "thread" , currentThread());
lock.lockInterruptibly();
vmBus_send("locked", lock, "thread" , currentThread());
} catch (InterruptedException e) {
Object reason = vm_threadInterruptionReasonsMap().get(currentThread());
print("Locking interrupted! Reason: " + strOr(reason, "Unknown"));
printStackTrace(e);
rethrow(e);
}
// NO call to ping here! Make sure lock is always released.
} catch (Exception __e) { throw rethrow(__e); } }
static void lock(Lock lock, String msg) {
print("Locking: " + msg);
lock(lock);
}
static void lock(Lock lock, String msg, long timeout) {
print("Locking: " + msg);
lockOrFail(lock, timeout);
}
static ReentrantLock lock() {
return fairLock();
}
// firstDelay = delay
static FixedRateTimer doEvery_daemon(long delay, final Object r) { return doEvery_daemon(defaultTimerName(), delay, r); }
static FixedRateTimer doEvery_daemon(String timerName, long delay, final Object r) {
return doEvery_daemon(timerName, delay, delay, r);
}
static FixedRateTimer doEvery_daemon(long delay, long firstDelay, final Object r) { return doEvery_daemon(defaultTimerName(), delay, firstDelay, r); }
static FixedRateTimer doEvery_daemon(String timerName, long delay, long firstDelay, final Object r) {
FixedRateTimer timer = new FixedRateTimer(true);
timer.scheduleAtFixedRate(smartTimerTask(r, timer, delay), firstDelay, delay);
return timer;
}
static FixedRateTimer doEvery_daemon(double delaySeconds, final Object r) {
return doEvery_daemon(toMS(delaySeconds), r);
}
static HashMap litmap(Object... x) {
HashMap map = new HashMap();
litmap_impl(map, x);
return map;
}
static void litmap_impl(Map map, Object... x) {
if (x != null) for (int i = 0; i < x.length-1; i += 2)
if (x[i+1] != null)
map.put(x[i], x[i+1]);
}
static Map runnableThreadsWithStackTraces() {
Map map = filterMap((__18, __19) -> isThreadRunnable_x(__18, __19), Thread.getAllStackTraces());
map.remove(currentThread());
return map;
}
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 Set keys(MultiSet ms) {
return ms.keySet();
}
static boolean isSystemThread(Thread t) {
ThreadGroup g = t.getThreadGroup();
return g != null && g.getName().equals("system");
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static void unlock(Lock lock, String msg) {
if (lock == null) return;
lock.unlock();
vmBus_send("unlocked", lock, "thread" , currentThread());
print("Unlocked: " + msg); // print afterwards to make sure the lock is always unlocked
}
static void unlock(Lock lock) {
if (lock == null) return;
lock.unlock();
vmBus_send("unlocked", lock, "thread" , currentThread());
}
static void stopTimer(java.util.Timer timer) {
if (timer != null) timer.cancel();
}
static List synchroList() {
return synchroList(new ArrayList());
}
static List synchroList(List l) {
return Collections.synchronizedList(l);
}
static List ll(A... a) {
ArrayList l = new ArrayList(a.length);
if (a != null) for (A x : a) l.add(x);
return l;
}
static Throwable printStackTrace2(Throwable e) {
// we go to system.out now - system.err is nonsense
print(getStackTrace2(e));
return e;
}
static void printStackTrace2() {
printStackTrace2(new Throwable());
}
static void printStackTrace2(String msg) {
printStackTrace2(new Throwable(msg));
}
static PersistableThrowable persistableThrowable(Throwable e) {
return e == null ? null : new PersistableThrowable(e);
}
static Throwable innerException(Throwable e) {
return getInnerException(e);
}
static boolean eq(Object a, Object b) {
return a == b || a != null && b != null && a.equals(b);
}
static String getStackTrace(Throwable throwable) {
lastException(throwable);
return getStackTrace_noRecord(throwable);
}
static String getStackTrace_noRecord(Throwable throwable) {
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
return hideCredentials(writer.toString());
}
static String getStackTrace() {
return getStackTrace_noRecord(new Throwable());
}
static ArrayList cloneList(Iterable l) {
return l instanceof Collection ? cloneList((Collection) l) : asList(l);
}
static ArrayList cloneList(Collection l) {
if (l == null) return new ArrayList();
synchronized(collectionMutex(l)) {
return new ArrayList(l);
}
}
static ThreadLocal poorMansProfiling_renderFullResults_backwards = new ThreadLocal();
static String poorMansProfiling_renderFullResults(final MultiSet traces) {
int samples = poorMansProfiling_samples;
boolean backwards = isTrue(getAndClearThreadLocal(poorMansProfiling_renderFullResults_backwards));
final int n = traces.size();
int percent = ratioToIntPercent(l(traces), samples);
return (samples == 0 ? "Nothing sampled"
: percent + "% core activity [" + n2(samples, "sample") + " taken]") + "\n\n"
+ joinMap(backwards ? traces.lowestFirst() : traces.highestFirst(), new F1() { public Object get(String trace) { try {
return traces.get(trace) + "/" + n + "\n" + trace + "\n\n";
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "traces.get(trace) + \"/\" + n + \"\\n\" + trace + \"\\n\\n\""; }});
}
static Map newDangerousWeakHashMap() {
return _registerDangerousWeakMap(synchroMap(new WeakHashMap()));
}
// initFunction: voidfunc(Map) - is called initially, and after clearing the map
static Map newDangerousWeakHashMap(Object initFunction) {
return _registerDangerousWeakMap(synchroMap(new WeakHashMap()), initFunction);
}
static Object callMCWithVarArgs(String method, Object... args) {
return call_withVarargs(mc(), method, args);
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static int l(MultiSet ms) { return ms == null ? 0 : ms.size(); }
static RuntimeException fail() { throw new RuntimeException("fail"); }
static RuntimeException fail(Throwable e) { throw asRuntimeException(e); }
static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); }
static RuntimeException fail(Object... objects) { throw new Fail(objects); }
static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }
static String getClassName(Object o) {
return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName();
}
static Object invokeMethod(Method m, Object o, Object... args) { try {
try {
return m.invoke(o, args);
} catch (InvocationTargetException e) {
throw rethrow(getExceptionCause(e));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(e.getMessage() + " - was calling: " + m + ", args: " + joinWithSpace(classNames(args)));
}
} catch (Exception __e) { throw rethrow(__e); } }
static boolean call_checkArgs(Method m, Object[] args, boolean debug) {
Class>[] types = m.getParameterTypes();
if (types.length != args.length) {
if (debug)
print("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++) {
Object arg = args[i];
if (!(arg == null ? !types[i].isPrimitive()
: isInstanceX(types[i], arg))) {
if (debug)
print("Bad parameter " + i + ": " + arg + " vs " + types[i]);
return false;
}
}
return true;
}
static Field makeAccessible(Field f) {
try {
f.setAccessible(true);
} catch (Throwable e) {
// Note: The error reporting only works with Java VM option --illegal-access=deny
vmBus_send("makeAccessible_error", e, f);
}
return f;
}
static Method makeAccessible(Method m) {
try {
m.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, m);
}
return m;
}
static Constructor makeAccessible(Constructor c) {
try {
c.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, c);
}
return c;
}
static Object call(Object o) {
return callF(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
//ret call_cached(o, method, args);
return call_withVarargs(o, method, args);
}
static Class javax() {
return getJavaX();
}
static String dm_makeNewModuleWithParams(String moduleLibID, Object... params) {
return (String) dm_callOS("makeNewModule", moduleLibID, false, "beforeStart" , new VF1