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;
    }

    /**
     * 這里我們通過反射靜態(tài)方法 getUnsafe()方法來獲取Unsafe對象,其實在之前的代碼
     * 的時候我們也可以直接反射靜態(tài)的成員變量來直接獲取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;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容