Search in sources :

Example 1 with Unsafe

use of sun.misc.Unsafe in project hbase by apache.

the class UnsafeUtil method getUnsafe.

/**
   * Gets the {@code sun.misc.Unsafe} instance, or {@code null} if not available on this platform.
   */
private static sun.misc.Unsafe getUnsafe() {
    sun.misc.Unsafe unsafe = null;
    try {
        unsafe = AccessController.doPrivileged(new PrivilegedExceptionAction<Unsafe>() {

            @Override
            public sun.misc.Unsafe run() throws Exception {
                Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
                for (Field f : k.getDeclaredFields()) {
                    f.setAccessible(true);
                    Object x = f.get(null);
                    if (k.isInstance(x)) {
                        return k.cast(x);
                    }
                }
                // The sun.misc.Unsafe field does not exist.
                return null;
            }
        });
    } catch (Throwable e) {
    // Catching Throwable here due to the fact that Google AppEngine raises NoClassDefFoundError
    // for Unsafe.
    }
    return unsafe;
}
Also used : Unsafe(sun.misc.Unsafe) Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction)

Example 2 with Unsafe

use of sun.misc.Unsafe in project groovy by apache.

the class FastStringUtils method loadUnsafe.

private static Unsafe loadUnsafe() {
    try {
        Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
        unsafeField.setAccessible(true);
        return (Unsafe) unsafeField.get(null);
    } catch (Exception e) {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 3 with Unsafe

use of sun.misc.Unsafe in project hadoop by apache.

the class ShortCircuitShm method safetyDance.

private static Unsafe safetyDance() {
    try {
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        return (Unsafe) f.get(null);
    } catch (Throwable e) {
        LOG.error("failed to load misc.Unsafe", e);
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 4 with Unsafe

use of sun.misc.Unsafe in project neo4j by neo4j.

the class UnsafeUtil method getUnsafe.

private static Unsafe getUnsafe() {
    try {
        PrivilegedExceptionAction<Unsafe> getUnsafe = () -> {
            try {
                return Unsafe.getUnsafe();
            } catch (Exception e) {
                Class<Unsafe> type = Unsafe.class;
                Field[] fields = type.getDeclaredFields();
                for (Field field : fields) {
                    if (Modifier.isStatic(field.getModifiers()) && type.isAssignableFrom(field.getType())) {
                        field.setAccessible(true);
                        return type.cast(field.get(null));
                    }
                }
                LinkageError error = new LinkageError("No static field of type sun.misc.Unsafe");
                error.addSuppressed(e);
                throw error;
            }
        };
        return AccessController.doPrivileged(getUnsafe);
    } catch (Exception e) {
        throw new LinkageError("Cannot access sun.misc.Unsafe", e);
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 5 with Unsafe

use of sun.misc.Unsafe in project jdk8u_jdk by JetBrains.

the class VMAnonymousClass method getUnsafe.

private static synchronized Unsafe getUnsafe() {
    try {
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        return (Unsafe) f.get(null);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new RuntimeException("Unable to get Unsafe instance.", e);
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Aggregations

Unsafe (sun.misc.Unsafe)87 Field (java.lang.reflect.Field)60 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)6 Test (org.testng.annotations.Test)6 PrivilegedActionException (java.security.PrivilegedActionException)5 PrivilegedAction (java.security.PrivilegedAction)3 JavaKind (jdk.vm.ci.meta.JavaKind)3 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)3 ValueNode (org.graalvm.compiler.nodes.ValueNode)3 GraphBuilderContext (org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext)3 Registration (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration)3 Test (org.junit.Test)3 LazyArrayIntTags (edu.columbia.cs.psl.phosphor.struct.LazyArrayIntTags)2 LazyArrayObjTags (edu.columbia.cs.psl.phosphor.struct.LazyArrayObjTags)2 InvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin)2 Receiver (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver)2 FrameSlotTypeException (com.oracle.truffle.api.frame.FrameSlotTypeException)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1