Java Unsafe工具类

public class ReflectUtils {

    private static ReflectUtils sInstance;

    private ReflectUtils() {
    }

    public static ReflectUtils getInstance() {
        if (sInstance == null) {
            sInstance = new ReflectUtils();
        }
        return sInstance;
    }

    public Method getMethod(Class<?> clazz, String methodName, Class<?> ...values) {
        Method method = null;
        try {
            if(values != null) {
                method = clazz.getDeclaredMethod(methodName, values);
            }else {
                method = clazz.getDeclaredMethod(methodName);
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return method;
    }

    public Object invokeStaticMethod(Method method, Object ...values) {
        return invokeMethod(method, null, values);
    }

    public Object invokeMethod(Method method, Object classValue, Object ...values) {
        if(method != null) {
            try {
                return method.invoke(classValue, values);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 这里我们通过反射静态方法 getUnsafe()方法来获取Unsafe对象,其实在之前的代码
     * 的时候我们也可以直接反射静态的成员变量来直接获取Unsafe对象的,其实都是差不多的。
     */
    public Object getUnsafe(Class<?> clazz) {
        Method method = getMethod(clazz, "getUnsafe");
        return invokeStaticMethod(method);
    }
}
public class UnsafeProxy {

    private static ReflectUtils sUtils;
    private static Class sUnsafeClass;
    private static Object sUnsafe;

    static {

        try {
            sUtils = ReflectUtils.getInstance();
            sUnsafeClass = Class.forName("sun.misc.Unsafe");
            sUnsafe = sUtils.getUnsafe(sUnsafeClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Object allocateInstance(Class<?> params) {
        Method method = sUtils.getMethod(sUnsafeClass, "allocateInstance", Class.class);
        return sUtils.invokeMethod(method, sUnsafe, params);
    }

    public static long objectFieldOffset(Field field) {
        Method method = sUtils.getMethod(sUnsafeClass, "objectFieldOffset", Field.class);
        Object obj = sUtils.invokeMethod(method, sUnsafe, field);
        return obj == null ? 0 : (Long) obj;
    }

    public static long putLong(Object object, long offset, long newValue) {
        Method method = sUtils.getMethod(sUnsafeClass, "putLong", Object.class, long.class, long.class);
        Object obj = sUtils.invokeMethod(method, sUnsafe, object, offset, newValue);
        return obj == null ? 0 : (Long) obj;
    }

    public static long putInt(Object object, long offset, int newValue) {
        Method method = sUtils.getMethod(sUnsafeClass, "putInt", Object.class, long.class, int.class);
        Object obj = sUtils.invokeMethod(method, sUnsafe, object, offset, newValue);
        return obj == null ? 0 : (Long) obj;
    }

    public static long putObject(Object object, long offset, Object newValue) {
        Method method = sUtils.getMethod(sUnsafeClass, "putObject", Object.class, long.class, Object.class);
        Object obj = sUtils.invokeMethod(method, sUnsafe, object, offset, newValue);
        return obj == null ? 0 : (Long) obj;
    }

    public static int arrayIndexScale(Class clazz) {
        Method method = sUtils.getMethod(sUnsafeClass, "arrayIndexScale", Class.class);
        Object obj = sUtils.invokeMethod(method, sUnsafe, clazz);
        return obj == null ? 0 : (Integer) obj;
    }

    public static int arrayBaseOffset(Class clazz) {
        Method method = sUtils.getMethod(sUnsafeClass, "arrayBaseOffset", Class.class);
        Object obj = sUtils.invokeMethod(method, sUnsafe, clazz);
        return obj == null ? 0 : (Integer) obj;
    }

    public static boolean compareAndSwapInt(Object object, long offset, int expectedValue, int newValue) {
        Method method = sUtils.getMethod(sUnsafeClass, "compareAndSwapInt", Object.class, long.class, int.class, int.class);
        Object obj = sUtils.invokeMethod(method, sUnsafe, object, offset, expectedValue, newValue);
        Log.d("Test", "" + (obj == null) + "method:" + (method) + "sUnsafe:" + sUnsafe);
        return obj == null ? false : (Boolean) obj;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容