All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wizzardo.tools.reflection.UnsafeTools Maven / Gradle / Ivy

There is a newer version: 0.23
Show newest version
package com.wizzardo.tools.reflection;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @author: wizzardo
 * Date: 6/11/14
 */
public class UnsafeTools {
    private static Unsafe unsafe;

    static {
        init();
    }

    private static void init() {
        Field f = null;
        try {
            f = Unsafe.class.getDeclaredField("theUnsafe");
            f.setAccessible(true);
            unsafe = (Unsafe) f.get(null);
        } catch (NoSuchFieldException ignored) {
        } catch (IllegalAccessException ignored) {
        }

        if (unsafe == null)
            try {
                f = Unsafe.class.getDeclaredField("THE_ONE"); // android
                f.setAccessible(true);
                unsafe = (Unsafe) f.get(null);
            } catch (NoSuchFieldException ignored) {
            } catch (IllegalAccessException ignored) {
            }

//        if (unsafe == null)
//            throw new IllegalStateException("can't get instance of sun.misc.Unsafe");
    }

    public static Unsafe getUnsafe() {
        return unsafe;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy