|
|
@@ -0,0 +1,46 @@
|
|
|
+package net.ranides.assira.reflection;
|
|
|
+
|
|
|
+import net.ranides.assira.annotations.Meta;
|
|
|
+import net.ranides.assira.reflection.IClass;
|
|
|
+import sun.misc.Unsafe;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+
|
|
|
+public class SunUtils {
|
|
|
+
|
|
|
+ static final class Li {
|
|
|
+ static final Unsafe API = getUnsafe();
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private static <T> T getUnsafe() {
|
|
|
+ return (T)IClass.typeinfo("sun.misc.Unsafe").field("theUnsafe").get().get(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Meta.Unsafe
|
|
|
+ @Deprecated
|
|
|
+ public static boolean putObjectVolatile(String type, String field, Object value) {
|
|
|
+ try {
|
|
|
+ Class cls = Class.forName(type);
|
|
|
+ Field fld = cls.getDeclaredField(field);
|
|
|
+ Li.API.putObjectVolatile(cls, Li.API.staticFieldOffset(fld), value);
|
|
|
+ return true;
|
|
|
+ } catch (Exception cause) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Meta.Unsafe
|
|
|
+ @Deprecated
|
|
|
+ public static boolean putObjectVolatile(Object that, String field, Object value) {
|
|
|
+ try {
|
|
|
+ Class cls = that.getClass();
|
|
|
+ Field fld = cls.getDeclaredField(field);
|
|
|
+ Li.API.putObjectVolatile(that, Li.API.objectFieldOffset(fld), value);
|
|
|
+ return true;
|
|
|
+ } catch (Exception cause) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|