package me.nallar.tickprofiler.util.contextaccess;

import me.nallar.tickprofiler.Log;

class ContextAccessProvider {
	private static final Class[] contextAccessClasses = {
			ContextAccessReflection.class,
			ContextAccessSecurityManager.class,
	};

	static ContextAccess getContextAccess() {
		for (Class<?> clazz : contextAccessClasses) {
			try {
				ContextAccess contextAccess = (ContextAccess) clazz.newInstance();
				Class<?> currentClass = contextAccess.getContext(0);
				if (currentClass != ContextAccessProvider.class) {
					throw new Error("Wrong class returned: " + currentClass + ", expected ContextAccessProvider");
				}
				return contextAccess;
			} catch (Throwable t) {
				Log.warning("Failed to instantiate " + clazz, t);
			}
		}
		throw new Error("Failed to set up any context access");
	}
}

@@ -539,24 +539,299 @@
 r.class) %7B%0D%0A
+%09%09%09%09%09StringBuilder sb = new StringBuilder();%0D%0A%09%09%09%09%09sb.append(%22Stack:%5Cn%22);%0D%0A%09%09%09%09%09for (int i = -2; i %3C 3; i++) %7B%0D%0A%09%09%09%09%09%09try %7B%0D%0A%09%09%09%09%09%09%09sb.append(contextAccess.getContext(i).getName()).append(%22 at %22).append(i).append('%5Cn');%0D%0A%09%09%09%09%09%09%7D catch (Throwable ignored) %7B%0D%0A%09%09%09%09%09%09%7D%0D%0A%09%09%09%09%09%7D%0D%0A
 %09%09%09%09%09throw n
@@ -914,17 +914,24 @@
 Provider
-%22
+. %22 + sb
 );%0D%0A%09%09%09%09
@@ -996,57 +996,213 @@
 %09%09%09%09
-Log.warning(%22Fai
+if (Log.debug) %7B%0D%0A%09%09%09%09%09Log.debug(%22Unab
 le
-d
  to 
-instantiate %22 + clazz, t);
+set up context access class %22 + clazz + %22. %22 + t.getMessage() + %22, falling back to slower context access. On JRE: %22 + System.getProperty(%22java.version%22));%0D%0A%09%09%09%09%7D
 %0D%0A%09%09

Fix #5