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

com.taobao.arthas.common.UnsafeUtils Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
package com.taobao.arthas.common;


import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;

import sun.misc.Unsafe;

/**
 * 
 * @author hengyunabc 2023-09-21
 *
 */
public class UnsafeUtils {
    public static final Unsafe UNSAFE;
    private static MethodHandles.Lookup IMPL_LOOKUP;

    static {
        Unsafe unsafe = null;
        try {
            Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafeField.setAccessible(true);
            unsafe = (Unsafe) theUnsafeField.get(null);
        } catch (Throwable ignored) {
            // ignored
        }
        UNSAFE = unsafe;
    }

    public static MethodHandles.Lookup implLookup() {
        if (IMPL_LOOKUP == null) {
            Class lookupClass = MethodHandles.Lookup.class;

            try {
                Field implLookupField = lookupClass.getDeclaredField("IMPL_LOOKUP");
                long offset = UNSAFE.staticFieldOffset(implLookupField);
                IMPL_LOOKUP = (MethodHandles.Lookup) UNSAFE.getObject(UNSAFE.staticFieldBase(implLookupField), offset);
            } catch (Throwable e) {
                // ignored
            }
        }
        return IMPL_LOOKUP;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy