Minor update

- Added more Android compatibility code
This commit is contained in:
Gabriel Tofvesson 2017-03-05 02:34:21 +01:00
parent 3d12866dc2
commit 1cfee5ff47
2 changed files with 12 additions and 7 deletions

View File

@ -197,19 +197,22 @@ public class Async<T> implements Awaitable{
public static <T> Async<T> current(){
try{
Object holder;
Field f = Thread.class.getDeclaredField("threadLocals");
Field f;
boolean android = false;
try{
f = Thread.class.getDeclaredField("threadLocals");
}catch(Exception ignored){ f = Thread.class.getDeclaredField("localValues"); android = true; }
f.setAccessible(true);
f = (holder=f.get(Thread.currentThread())).getClass().getDeclaredField("table");
f.setAccessible(true);
boolean containsData = false;
for(Object o : (Object[]) f.get(holder))
if(o != null) {
if (!containsData) {
if(android) holder = o;
else {
f = o.getClass().getDeclaredField("value");
f.setAccessible(true);
containsData = true;
}
if((holder=f.get(o)) instanceof Async) return (Async<T>) holder;
if((android?holder:(holder=f.get(o))) instanceof Async) return (Async<T>) holder;
}
}catch(Exception e){}
return null;

View File

@ -36,11 +36,13 @@ public final class SafeReflection {
f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
u = (Unsafe) f.get(null);
f = Field.class.getDeclaredField("modifiers");
f.setAccessible(true);
try {
f = Field.class.getDeclaredField("modifiers");
f.setAccessible(true);
l = u.objectFieldOffset(AccessibleObject.class.getDeclaredField("override")); // Most desktop versions of Java
}catch(Exception e){
f = Field.class.getDeclaredField("accessFlags");
f.setAccessible(true);
l = u.objectFieldOffset(AccessibleObject.class.getDeclaredField("flag")); // God-damned Android
}
try {