@Override Class<?> defineClass(String name, byte[] b, int off, int len, Class<?> neighbor, ClassLoader loader, ProtectionDomain protectionDomain) throws ClassFormatError { try { if (getCallerClass.invoke(stack) != DefineClassHelper.class) throw new IllegalAccessError("Access denied for caller."); } catch (Exception e) { throw new RuntimeException("cannot initialize", e); } return sunMiscUnsafe.defineClass(name, b, off, len, loader, protectionDomain); } }
private final ReferencedUnsafe getReferencedUnsafe() { try { if (privileged != null && getCallerClass.invoke(stack) != this.getClass()) throw new IllegalAccessError("Access denied for caller."); } catch (Exception e) { throw new RuntimeException("cannot initialize", e); } try { SecurityActions.TheUnsafe usf = SecurityActions.getSunMiscUnsafeAnonymously(); List<Method> defineClassMethod = usf.methods.get("defineClass"); // On Java 11+ the defineClass method does not exist anymore if (null == defineClassMethod) return null; MethodHandle meth = MethodHandles.lookup().unreflect(defineClassMethod.get(0)); return new ReferencedUnsafe(usf, meth); } catch (Throwable e) { throw new RuntimeException("cannot initialize", e); } }