-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ContextAccessProvider.java
35 lines (32 loc) · 1.27 KB
/
ContextAccessProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package nallar.tickthreading.util.contextaccess;
import nallar.tickthreading.Log;
import nallar.tickthreading.util.EnvironmentInfo;
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) {
StringBuilder sb = new StringBuilder();
sb.append("Stack:\n");
for (int i = -2; i < 3; i++) {
try {
sb.append(contextAccess.getContext(i).getName()).append(" at ").append(i).append('\n');
} catch (Throwable ignored) {
}
}
throw new Error("Wrong class returned: " + currentClass + ", expected ContextAccessProvider. " + sb);
}
return contextAccess;
} catch (Throwable t) {
Log.warning("Unable to set up context access class " + clazz + ". " + t.getMessage() + ", falling back to slower context access. On JRE: " + EnvironmentInfo.getJavaVersion());
}
}
throw new Error("Failed to set up any context access");
}
}