Skip to content

Instantly share code, notes, and snippets.

@burningtnt
Created April 30, 2025 13:21
Show Gist options
  • Save burningtnt/e8f43d6917a60a3c2be59f41b2b2e653 to your computer and use it in GitHub Desktop.
Save burningtnt/e8f43d6917a60a3c2be59f41b2b2e653 to your computer and use it in GitHub Desktop.
import sun.misc.Unsafe;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
public final class TrustedLookupAccessor {
public static MethodHandles.Lookup get() throws Throwable {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe U = (Unsafe) theUnsafe.get(null);
Field implLookup = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
Object base = U.staticFieldBase(implLookup);
long l = U.staticFieldOffset(implLookup);
return (MethodHandles.Lookup) U.getObject(base, l);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment