Skip to content

Instantly share code, notes, and snippets.

@WouterG
Created August 31, 2017 12:17
Show Gist options
  • Select an option

  • Save WouterG/97b803f94654f3985816211c7a32a9eb to your computer and use it in GitHub Desktop.

Select an option

Save WouterG/97b803f94654f3985816211c7a32a9eb to your computer and use it in GitHub Desktop.
sun.misc.Unsafe made public/accessible
package net.wouto.debugutils;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.security.ProtectionDomain;
@SuppressWarnings("ALL")
public class JUnsafe {
private static Unsafe unsafe;
private static JUnsafe junsafe;
private JUnsafe() {
}
public static JUnsafe getUnsafe() {
if (unsafe == null) {
try {
for (Field field : Unsafe.class.getDeclaredFields()) {
if (field.getType() == Unsafe.class) {
field.setAccessible(true);
unsafe = (Unsafe) field.get(null);
}
}
if (unsafe == null) {
throw new NoSuchFieldException("Unable to find Unsafe field");
}
} catch (Exception e) {
throw new RuntimeException("Unable to get JUnsafe as Unsafe couldn't be fetched", e);
}
}
if (junsafe == null) {
junsafe = new JUnsafe();
}
return junsafe;
}
public int getInt(Object o, long l) {
return unsafe.getInt(o, l);
}
public void putInt(Object o, long l, int i) {
unsafe.putInt(o, l, i);
}
public Object getObject(Object o, long l) {
return unsafe.getObject(o, l);
}
public void putObject(Object o, long l, Object o1) {
unsafe.putObject(o, l, o1);
}
public boolean getBoolean(Object o, long l) {
return unsafe.getBoolean(o, l);
}
public void putBoolean(Object o, long l, boolean b) {
unsafe.putBoolean(o, l, b);
}
public byte getByte(Object o, long l) {
return unsafe.getByte(o, l);
}
public void putByte(Object o, long l, byte b) {
unsafe.putByte(o, l, b);
}
public short getShort(Object o, long l) {
return unsafe.getShort(o, l);
}
public void putShort(Object o, long l, short i) {
unsafe.putShort(o, l, i);
}
public char getChar(Object o, long l) {
return unsafe.getChar(o, l);
}
public void putChar(Object o, long l, char c) {
unsafe.putChar(o, l, c);
}
public long getLong(Object o, long l) {
return unsafe.getLong(o, l);
}
public void putLong(Object o, long l, long l1) {
unsafe.putLong(o, l, l1);
}
public float getFloat(Object o, long l) {
return unsafe.getFloat(o, l);
}
public void putFloat(Object o, long l, float v) {
unsafe.putFloat(o, l, v);
}
public double getDouble(Object o, long l) {
return unsafe.getDouble(o, l);
}
public void putDouble(Object o, long l, double v) {
unsafe.putDouble(o, l, v);
}
@Deprecated
public int getInt(Object o, int i) {
return unsafe.getInt(o, i);
}
@Deprecated
public void putInt(Object o, int i, int i1) {
unsafe.putInt(o, i, i1);
}
@Deprecated
public Object getObject(Object o, int i) {
return unsafe.getObject(o, i);
}
@Deprecated
public void putObject(Object o, int i, Object o1) {
unsafe.putObject(o, i, o1);
}
@Deprecated
public boolean getBoolean(Object o, int i) {
return unsafe.getBoolean(o, i);
}
@Deprecated
public void putBoolean(Object o, int i, boolean b) {
unsafe.putBoolean(o, i, b);
}
@Deprecated
public byte getByte(Object o, int i) {
return unsafe.getByte(o, i);
}
@Deprecated
public void putByte(Object o, int i, byte b) {
unsafe.putByte(o, i, b);
}
@Deprecated
public short getShort(Object o, int i) {
return unsafe.getShort(o, i);
}
@Deprecated
public void putShort(Object o, int i, short i1) {
unsafe.putShort(o, i, i1);
}
@Deprecated
public char getChar(Object o, int i) {
return unsafe.getChar(o, i);
}
@Deprecated
public void putChar(Object o, int i, char c) {
unsafe.putChar(o, i, c);
}
@Deprecated
public long getLong(Object o, int i) {
return unsafe.getLong(o, i);
}
@Deprecated
public void putLong(Object o, int i, long l) {
unsafe.putLong(o, i, l);
}
@Deprecated
public float getFloat(Object o, int i) {
return unsafe.getFloat(o, i);
}
@Deprecated
public void putFloat(Object o, int i, float v) {
unsafe.putFloat(o, i, v);
}
@Deprecated
public double getDouble(Object o, int i) {
return unsafe.getDouble(o, i);
}
@Deprecated
public void putDouble(Object o, int i, double v) {
unsafe.putDouble(o, i, v);
}
public byte getByte(long l) {
return unsafe.getByte(l);
}
public void putByte(long l, byte b) {
unsafe.putByte(l, b);
}
public short getShort(long l) {
return unsafe.getShort(l);
}
public void putShort(long l, short i) {
unsafe.putShort(l, i);
}
public char getChar(long l) {
return unsafe.getChar(l);
}
public void putChar(long l, char c) {
unsafe.putChar(l, c);
}
public int getInt(long l) {
return unsafe.getInt(l);
}
public void putInt(long l, int i) {
unsafe.putInt(l, i);
}
public long getLong(long l) {
return unsafe.getLong(l);
}
public void putLong(long l, long l1) {
unsafe.putLong(l, l1);
}
public float getFloat(long l) {
return unsafe.getFloat(l);
}
public void putFloat(long l, float v) {
unsafe.putFloat(l, v);
}
public double getDouble(long l) {
return unsafe.getDouble(l);
}
public void putDouble(long l, double v) {
unsafe.putDouble(l, v);
}
public long getAddress(long l) {
return unsafe.getAddress(l);
}
public void putAddress(long l, long l1) {
unsafe.putAddress(l, l1);
}
public long allocateMemory(long l) {
return unsafe.allocateMemory(l);
}
public long reallocateMemory(long l, long l1) {
return unsafe.reallocateMemory(l, l1);
}
public void setMemory(Object o, long l, long l1, byte b) {
unsafe.setMemory(o, l, l1, b);
}
public void setMemory(long l, long l1, byte b) {
unsafe.setMemory(l, l1, b);
}
public void copyMemory(Object sourceRef, long sourceOffset, Object destRef, long destOffset, long byteCount) {
this.copyMemory0(sourceRef, sourceOffset, destRef, destOffset, byteCount);
}
private void copyMemory0(Object from, long fromOffset, Object to, long toOffset, long length) {
// use a loop to ensure there is a safe point every so often.
while (length > 0) {
long size = Math.min(length, 1024L * 1024L); // copy in blocks of 1 MiB
unsafe.copyMemory(from, fromOffset, to, toOffset, size);
length -= size;
fromOffset += size;
toOffset += size;
}
}
public void copyMemory(long l, long l1, long l2) {
this.copyMemory0(null, l, null, l1, l2);
}
public void freeMemory(long l) {
unsafe.freeMemory(l);
}
@Deprecated
public int fieldOffset(Field field) {
return unsafe.fieldOffset(field);
}
@Deprecated
public Object staticFieldBase(Class<?> aClass) {
return unsafe.staticFieldBase(aClass);
}
public long staticFieldOffset(Field field) {
return unsafe.staticFieldOffset(field);
}
public long objectFieldOffset(Field field) {
return unsafe.objectFieldOffset(field);
}
public Object staticFieldBase(Field field) {
return unsafe.staticFieldBase(field);
}
public boolean shouldBeInitialized(Class<?> aClass) {
return unsafe.shouldBeInitialized(aClass);
}
public void ensureClassInitialized(Class<?> aClass) {
unsafe.ensureClassInitialized(aClass);
}
public int arrayBaseOffset(Class<?> aClass) {
return unsafe.arrayBaseOffset(aClass);
}
public int arrayIndexScale(Class<?> aClass) {
return unsafe.arrayIndexScale(aClass);
}
public int addressSize() {
return unsafe.addressSize();
}
public int pageSize() {
return unsafe.pageSize();
}
public Class<?> defineClass(String s, byte[] bytes, int i, int i1, ClassLoader classLoader, ProtectionDomain protectionDomain) {
return unsafe.defineClass(s, bytes, i, i1, classLoader, protectionDomain);
}
public Class<?> defineAnonymousClass(Class<?> aClass, byte[] bytes, Object[] objects) {
return unsafe.defineAnonymousClass(aClass, bytes, objects);
}
public Object allocateInstance(Class<?> aClass) throws InstantiationException {
return unsafe.allocateInstance(aClass);
}
@Deprecated
public void monitorEnter(Object o) {
unsafe.monitorEnter(o);
}
@Deprecated
public void monitorExit(Object o) {
unsafe.monitorExit(o);
}
@Deprecated
public boolean tryMonitorEnter(Object o) {
return unsafe.tryMonitorEnter(o);
}
public void throwException(Throwable throwable) {
unsafe.throwException(throwable);
}
public Object getObjectVolatile(Object o, long l) {
return unsafe.getObjectVolatile(o, l);
}
public void putObjectVolatile(Object o, long l, Object o1) {
unsafe.putObjectVolatile(o, l, o1);
}
public int getIntVolatile(Object o, long l) {
return unsafe.getIntVolatile(o, l);
}
public void putIntVolatile(Object o, long l, int i) {
unsafe.putIntVolatile(o, l, i);
}
public boolean getBooleanVolatile(Object o, long l) {
return unsafe.getBooleanVolatile(o, l);
}
public void putBooleanVolatile(Object o, long l, boolean b) {
unsafe.putBooleanVolatile(o, l, b);
}
public byte getByteVolatile(Object o, long l) {
return unsafe.getByteVolatile(o, l);
}
public void putByteVolatile(Object o, long l, byte b) {
unsafe.putByteVolatile(o, l, b);
}
public short getShortVolatile(Object o, long l) {
return unsafe.getShortVolatile(o, l);
}
public void putShortVolatile(Object o, long l, short i) {
unsafe.putShortVolatile(o, l, i);
}
public char getCharVolatile(Object o, long l) {
return unsafe.getCharVolatile(o, l);
}
public void putCharVolatile(Object o, long l, char c) {
unsafe.putCharVolatile(o, l, c);
}
public long getLongVolatile(Object o, long l) {
return unsafe.getLongVolatile(o, l);
}
public void putLongVolatile(Object o, long l, long l1) {
unsafe.putLongVolatile(o, l, l1);
}
public float getFloatVolatile(Object o, long l) {
return unsafe.getFloatVolatile(o, l);
}
public void putFloatVolatile(Object o, long l, float v) {
unsafe.putFloatVolatile(o, l, v);
}
public double getDoubleVolatile(Object o, long l) {
return unsafe.getDoubleVolatile(o, l);
}
public void putDoubleVolatile(Object o, long l, double v) {
unsafe.putDoubleVolatile(o, l, v);
}
public void putOrderedObject(Object o, long l, Object o1) {
unsafe.putOrderedObject(o, l, o1);
}
public void putOrderedInt(Object o, long l, int i) {
unsafe.putOrderedInt(o, l, i);
}
public void putOrderedLong(Object o, long l, long l1) {
unsafe.putOrderedLong(o, l, l1);
}
public void unpark(Object o) {
unsafe.unpark(o);
}
public void park(boolean b, long l) {
unsafe.park(b, l);
}
public int getLoadAverage(double[] doubles, int i) {
return unsafe.getLoadAverage(doubles, i);
}
public void loadFence() {
unsafe.loadFence();
}
public void storeFence() {
unsafe.storeFence();
}
public void fullFence() {
unsafe.fullFence();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment