Java Examples for sun.misc.Unsafe

The following java examples will help you to understand the usage of sun.misc.Unsafe. These source code samples are taken from different open source projects.

Example 1
Project: flink-master  File: MemoryUtils.java View source code
@SuppressWarnings("restriction")
private static sun.misc.Unsafe getUnsafe() {
    try {
        Field unsafeField = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
        unsafeField.setAccessible(true);
        return (sun.misc.Unsafe) unsafeField.get(null);
    } catch (SecurityException e) {
        throw new RuntimeException("Could not access the sun.misc.Unsafe handle, permission denied by security manager.", e);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException("The static handle field in sun.misc.Unsafe was not found.");
    } catch (IllegalArgumentException e) {
        throw new RuntimeException("Bug: Illegal argument reflection access for static field.", e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Access to sun.misc.Unsafe is forbidden by the runtime.", e);
    } catch (Throwable t) {
        throw new RuntimeException("Unclassified error while trying to access the sun.misc.Unsafe handle.", t);
    }
}
Example 2
Project: Tank-master  File: SummaryReportObserverTest.java View source code
/**
     * Run the void observerJobEvents(JobEvent) method test.
     *
     * @throws Exception
     *
     * @generatedBy CodePro at 12/15/14 3:52 PM
     */
@Test
public void testObserverJobEvents_1() throws Exception {
    SummaryReportObserver fixture = new SummaryReportObserver();
    JobEvent jobEvent = new JobEvent("", "", JobLifecycleEvent.AGENT_EXCESSIVE_CPU);
    fixture.observerJobEvents(jobEvent);
// An unexpected exception was thrown in user code while executing this test:
//    java.lang.NoClassDefFoundError: com_cenqua_clover/CoverageRecorder
//       at com.intuit.tank.api.enumerated.JobLifecycleEvent.<init>(JobLifecycleEvent.java:43)
//       at com.intuit.tank.api.enumerated.JobLifecycleEvent.<clinit>(JobLifecycleEvent.java:14)
//       at sun.misc.Unsafe.ensureClassInitialized(Native Method)
}
Example 3
Project: guava-master  File: UnsignedBytes.java View source code
/**
       * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. Replace with a simple
       * call to Unsafe.getUnsafe when integrating into a jdk.
       *
       * @return a sun.misc.Unsafe
       */
private static sun.misc.Unsafe getUnsafe() {
    try {
        return sun.misc.Unsafe.getUnsafe();
    } catch (SecurityException e) {
    }
    try {
        return java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {

            @Override
            public sun.misc.Unsafe run() throws Exception {
                Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
                for (java.lang.reflect.Field f : k.getDeclaredFields()) {
                    f.setAccessible(true);
                    Object x = f.get(null);
                    if (k.isInstance(x)) {
                        return k.cast(x);
                    }
                }
                throw new NoSuchFieldError("the Unsafe");
            }
        });
    } catch (java.security.PrivilegedActionException e) {
        throw new RuntimeException("Could not initialize intrinsics", e.getCause());
    }
}
Example 4
Project: netty3.9-note-master  File: DetectionUtil.java View source code
private static boolean hasUnsafe(ClassLoader loader) {
    boolean noUnsafe = SystemPropertyUtil.getBoolean("io.netty.noUnsafe", false);
    if (noUnsafe) {
        return false;
    }
    // Legacy properties
    boolean tryUnsafe;
    if (SystemPropertyUtil.contains("io.netty.tryUnsafe")) {
        tryUnsafe = SystemPropertyUtil.getBoolean("io.netty.tryUnsafe", true);
    } else {
        tryUnsafe = SystemPropertyUtil.getBoolean("org.jboss.netty.tryUnsafe", true);
    }
    if (!tryUnsafe) {
        return false;
    }
    try {
        Class<?> unsafeClazz = Class.forName("sun.misc.Unsafe", true, loader);
        return hasUnsafeField(unsafeClazz);
    } catch (Exception e) {
    }
    return false;
}
Example 5
Project: guava-experimental-master  File: Striped64.java View source code
/**
     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
     * Replace with a simple call to Unsafe.getUnsafe when integrating
     * into a jdk.
     *
     * @return a sun.misc.Unsafe
     */
private static sun.misc.Unsafe getUnsafe() {
    try {
        return sun.misc.Unsafe.getUnsafe();
    } catch (SecurityException se) {
        try {
            return java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {

                public sun.misc.Unsafe run() throws Exception {
                    java.lang.reflect.Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
                    f.setAccessible(true);
                    return (sun.misc.Unsafe) f.get(null);
                }
            });
        } catch (java.security.PrivilegedActionException e) {
            throw new RuntimeException("Could not initialize intrinsics", e.getCause());
        }
    }
}
Example 6
Project: cloudtm-data-platform-master  File: Striped64.java View source code
/**
    * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
    * Replace with a simple call to Unsafe.getUnsafe when integrating
    * into a jdk.
    *
    * @return a sun.misc.Unsafe
    */
private static sun.misc.Unsafe getUnsafe() {
    try {
        return sun.misc.Unsafe.getUnsafe();
    } catch (SecurityException se) {
        try {
            return java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {

                @Override
                public sun.misc.Unsafe run() throws Exception {
                    java.lang.reflect.Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
                    f.setAccessible(true);
                    return (sun.misc.Unsafe) f.get(null);
                }
            });
        } catch (java.security.PrivilegedActionException e) {
            throw new RuntimeException("Could not initialize intrinsics", e.getCause());
        }
    }
}
Example 7
Project: jnlp-master  File: UnsignedBytes.java View source code
/**
       * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
       * Replace with a simple call to Unsafe.getUnsafe when integrating
       * into a jdk.
       *
       * @return a sun.misc.Unsafe
       */
private static sun.misc.Unsafe getUnsafe() {
    try {
        return sun.misc.Unsafe.getUnsafe();
    } catch (SecurityException tryReflectionInstead) {
    }
    try {
        return java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {

            public sun.misc.Unsafe run() throws Exception {
                Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
                for (java.lang.reflect.Field f : k.getDeclaredFields()) {
                    f.setAccessible(true);
                    Object x = f.get(null);
                    if (k.isInstance(x))
                        return k.cast(x);
                }
                throw new NoSuchFieldError("the Unsafe");
            }
        });
    } catch (java.security.PrivilegedActionException e) {
        throw new RuntimeException("Could not initialize intrinsics", e.getCause());
    }
}
Example 8
Project: metrics-master  File: UnsafeStriped64.java View source code
/**
     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package. Replace with a simple
     * call to Unsafe.getUnsafe when integrating into a jdk.
     *
     * @return a sun.misc.Unsafe
     */
private static sun.misc.Unsafe getUnsafe() {
    try {
        return sun.misc.Unsafe.getUnsafe();
    } catch (SecurityException ignored) {
    }
    try {
        return java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {

            @Override
            public sun.misc.Unsafe run() throws Exception {
                Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
                for (java.lang.reflect.Field f : k.getDeclaredFields()) {
                    f.setAccessible(true);
                    Object x = f.get(null);
                    if (k.isInstance(x))
                        return k.cast(x);
                }
                throw new NoSuchFieldError("the Unsafe");
            }
        });
    } catch (java.security.PrivilegedActionException e) {
        throw new RuntimeException("Could not initialize intrinsics", e.getCause());
    }
}
Example 9
Project: org.openntf.domino-master  File: UnsignedBytes.java View source code
/**
       * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
       * Replace with a simple call to Unsafe.getUnsafe when integrating
       * into a jdk.
       *
       * @return a sun.misc.Unsafe
       */
private static sun.misc.Unsafe getUnsafe() {
    try {
        return sun.misc.Unsafe.getUnsafe();
    } catch (SecurityException tryReflectionInstead) {
    }
    try {
        return java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {

            public sun.misc.Unsafe run() throws Exception {
                Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
                for (java.lang.reflect.Field f : k.getDeclaredFields()) {
                    f.setAccessible(true);
                    Object x = f.get(null);
                    if (k.isInstance(x))
                        return k.cast(x);
                }
                throw new NoSuchFieldError("the Unsafe");
            }
        });
    } catch (java.security.PrivilegedActionException e) {
        throw new RuntimeException("Could not initialize intrinsics", e.getCause());
    }
}
Example 10
Project: j2objc-master  File: SynchronousQueue.java View source code
// Unsafe mechanics
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, String field, Class<?> klazz) {
    try {
        return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
    } catch (NoSuchFieldException e) {
        NoSuchFieldError error = new NoSuchFieldError(field);
        error.initCause(e);
        throw error;
    }
}
Example 11
Project: ARTPart-master  File: SynchronousQueue.java View source code
// Unsafe mechanics
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, String field, Class<?> klazz) {
    try {
        return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
    } catch (NoSuchFieldException e) {
        NoSuchFieldError error = new NoSuchFieldError(field);
        error.initCause(e);
        throw error;
    }
}
Example 12
Project: openjdk8-jdk-master  File: SynchronousQueue.java View source code
// Unsafe mechanics
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, String field, Class<?> klazz) {
    try {
        return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
    } catch (NoSuchFieldException e) {
        NoSuchFieldError error = new NoSuchFieldError(field);
        error.initCause(e);
        throw error;
    }
}
Example 13
Project: openjdk-master  File: PrintTest.java View source code
public void run(CommandExecutor executor) {
    MonitorThread mThread = new MonitorThread();
    mThread.start();
    LockThread lThread = new LockThread();
    lThread.start();
    /* Wait for threads to get ready */
    waitForBarrier(readyBarrier);
    /* Execute */
    OutputAnalyzer output = executor.execute("Thread.print" + (jucLocks ? " -l=true" : ""));
    /* Signal that we've got the thread dump */
    waitForBarrier(doneBarrier);
    /*
         * Example output (trimmed) with arrows indicating the rows we are looking for:
         *
         *     ...
         *     "Thread-2" #24 prio=5 os_prio=0 tid=0x00007f913411f800 nid=0x4fc9 waiting on condition [0x00007f91fbffe000]
         *        java.lang.Thread.State: WAITING (parking)
         *       at sun.misc.Unsafe.park(Native Method)
         *       - parking to wait for  <0x000000071a0868a8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
         *       at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
         *       at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
         *       at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:234)
         *       at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
         *       at Print.waitForBarrier(Print.java:26)
         *       at Print.access$000(Print.java:18)
         *       at Print$LockThread.run(Print.java:58)
         *
         * -->    Locked ownable synchronizers:
         * -->    - <0x000000071a294930> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
         *
         *     "Thread-1" #23 prio=5 os_prio=0 tid=0x00007f913411e800 nid=0x4fc8 waiting on condition [0x00007f9200113000]
         *        java.lang.Thread.State: WAITING (parking)
         *       at sun.misc.Unsafe.park(Native Method)
         *       - parking to wait for  <0x000000071a0868a8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
         *       at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
         *       at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
         *       at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:234)
         *       at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
         *       at Print.waitForBarrier(Print.java:26)
         *       at Print.access$000(Print.java:18)
         *       at Print$MonitorThread.run(Print.java:42)
         * -->   - locked <0x000000071a294390> (a java.lang.Object)
         *
         *        Locked ownable synchronizers:
         *       - None
         *
         *     "MainThread" #22 prio=5 os_prio=0 tid=0x00007f923015b000 nid=0x4fc7 in Object.wait() [0x00007f9200840000]
         *        java.lang.Thread.State: WAITING (on object monitor)
         *       at java.lang.Object.wait(Native Method)
         *       - waiting on <0x000000071a70ad98> (a java.lang.UNIXProcess)
         *       at java.lang.Object.wait(Object.java:502)
         *        at java.lang.UNIXProcess.waitFor(UNIXProcess.java:397)
         *        - locked <0x000000071a70ad98> (a java.lang.UNIXProcess)
         *        at jdk.test.lib.dcmd.JcmdExecutor.executeImpl(JcmdExecutor.java:32)
         *       at jdk.test.lib.dcmd.CommandExecutor.execute(CommandExecutor.java:24)
         * -->   at Print.run(Print.java:74)
         *       at Print.file(Print.java:112)
         *     ...

         */
    output.shouldMatch(".*at " + Pattern.quote(PrintTest.class.getName()) + "\\.run.*");
    output.shouldMatch(".*- locked <0x\\p{XDigit}+> \\(a " + Pattern.quote(mThread.lock.getClass().getName()) + "\\).*");
    String jucLockPattern1 = ".*Locked ownable synchronizers:.*";
    String jucLockPattern2 = ".*- <0x\\p{XDigit}+> \\(a " + Pattern.quote(lThread.lock.getClass().getName()) + ".*";
    if (jucLocks) {
        output.shouldMatch(jucLockPattern1);
        output.shouldMatch(jucLockPattern2);
    } else {
        output.shouldNotMatch(jucLockPattern1);
        output.shouldNotMatch(jucLockPattern2);
    }
}
Example 14
Project: jdk7u-jdk-master  File: SynchronousQueue.java View source code
// Unsafe mechanics
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, String field, Class<?> klazz) {
    try {
        return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
    } catch (NoSuchFieldException e) {
        NoSuchFieldError error = new NoSuchFieldError(field);
        error.initCause(e);
        throw error;
    }
}
Example 15
Project: android_platform_libcore-master  File: SynchronousQueue.java View source code
// Unsafe mechanics
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, String field, Class<?> klazz) {
    try {
        return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
    } catch (NoSuchFieldException e) {
        NoSuchFieldError error = new NoSuchFieldError(field);
        error.initCause(e);
        throw error;
    }
}
Example 16
Project: android-15-master  File: SynchronousQueue.java View source code
// Unsafe mechanics
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, String field, Class<?> klazz) {
    try {
        return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
    } catch (NoSuchFieldException e) {
        NoSuchFieldError error = new NoSuchFieldError(field);
        error.initCause(e);
        throw error;
    }
}
Example 17
Project: android-libcore64-master  File: SynchronousQueue.java View source code
// Unsafe mechanics
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, String field, Class<?> klazz) {
    try {
        return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
    } catch (NoSuchFieldException e) {
        NoSuchFieldError error = new NoSuchFieldError(field);
        error.initCause(e);
        throw error;
    }
}
Example 18
Project: android-sdk-sources-for-api-level-23-master  File: SynchronousQueue.java View source code
// Unsafe mechanics
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, String field, Class<?> klazz) {
    try {
        return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
    } catch (NoSuchFieldException e) {
        NoSuchFieldError error = new NoSuchFieldError(field);
        error.initCause(e);
        throw error;
    }
}
Example 19
Project: JDK-master  File: SynchronousQueue.java View source code
// Unsafe mechanics
static long objectFieldOffset(sun.misc.Unsafe UNSAFE, String field, Class<?> klazz) {
    try {
        return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
    } catch (NoSuchFieldException e) {
        NoSuchFieldError error = new NoSuchFieldError(field);
        error.initCause(e);
        throw error;
    }
}
Example 20
Project: Java-Lang-master  File: ByteBufferReuse.java View source code
private static ByteBufferReuse getReuse() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    final String reuseImplClassName = "net/openhft/lang/io/ByteBufferReuseImpl";
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, reuseImplClassName, null, "sun/reflect/MagicAccessorImpl", new String[] { "net/openhft/lang/io/ByteBufferReuse" });
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    String attachedBufferFieldName = getAttachedBufferFieldName();
    {
        mv = cw.visitMethod(ACC_PUBLIC, "reuse", "(JILjava/lang/Object;Ljava/nio/ByteBuffer;)Ljava/nio/ByteBuffer;", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 5);
        String directByteBuffer = "java/nio/DirectByteBuffer";
        mv.visitTypeInsn(INSTANCEOF, directByteBuffer);
        Label l0 = new Label();
        mv.visitJumpInsn(IFEQ, l0);
        mv.visitVarInsn(ALOAD, 5);
        mv.visitTypeInsn(CHECKCAST, directByteBuffer);
        mv.visitVarInsn(ASTORE, 6);
        mv.visitVarInsn(ALOAD, 6);
        mv.visitFieldInsn(GETFIELD, directByteBuffer, attachedBufferFieldName, "Ljava/lang/Object;");
        String settableAtt = "net/openhft/lang/io/SettableAtt";
        mv.visitTypeInsn(INSTANCEOF, settableAtt);
        mv.visitJumpInsn(IFEQ, l0);
        mv.visitVarInsn(ALOAD, 6);
        mv.visitVarInsn(LLOAD, 1);
        mv.visitFieldInsn(PUTFIELD, directByteBuffer, "address", "J");
        mv.visitVarInsn(ALOAD, 6);
        mv.visitInsn(ICONST_M1);
        mv.visitFieldInsn(PUTFIELD, directByteBuffer, "mark", "I");
        mv.visitVarInsn(ALOAD, 6);
        mv.visitInsn(ICONST_0);
        mv.visitFieldInsn(PUTFIELD, directByteBuffer, "position", "I");
        mv.visitVarInsn(ALOAD, 6);
        mv.visitVarInsn(ILOAD, 3);
        mv.visitFieldInsn(PUTFIELD, directByteBuffer, "limit", "I");
        mv.visitVarInsn(ALOAD, 6);
        mv.visitVarInsn(ILOAD, 3);
        mv.visitFieldInsn(PUTFIELD, directByteBuffer, "capacity", "I");
        mv.visitVarInsn(ALOAD, 6);
        mv.visitFieldInsn(GETFIELD, directByteBuffer, attachedBufferFieldName, "Ljava/lang/Object;");
        mv.visitTypeInsn(CHECKCAST, settableAtt);
        mv.visitVarInsn(ALOAD, 4);
        mv.visitFieldInsn(PUTFIELD, settableAtt, "att", "Ljava/lang/Object;");
        mv.visitVarInsn(ALOAD, 6);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l0);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitTypeInsn(NEW, settableAtt);
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, settableAtt, "<init>", "()V", false);
        mv.visitVarInsn(ASTORE, 6);
        mv.visitVarInsn(ALOAD, 6);
        mv.visitVarInsn(ALOAD, 4);
        mv.visitFieldInsn(PUTFIELD, settableAtt, "att", "Ljava/lang/Object;");
        mv.visitTypeInsn(NEW, directByteBuffer);
        mv.visitInsn(DUP);
        mv.visitVarInsn(LLOAD, 1);
        mv.visitVarInsn(ILOAD, 3);
        mv.visitVarInsn(ALOAD, 6);
        mv.visitMethodInsn(INVOKESPECIAL, directByteBuffer, "<init>", "(JILjava/lang/Object;)V", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(6, 7);
        mv.visitEnd();
    }
    cw.visitEnd();
    final byte[] impl = cw.toByteArray();
    final Unsafe unsafe = NativeBytes.UNSAFE;
    Class clazz = AccessController.doPrivileged(new PrivilegedAction<Class>() {

        @Override
        public Class run() {
            ClassLoader cl = MAGIC_CLASS_LOADER;
            return unsafe.defineClass(reuseImplClassName, impl, 0, impl.length, cl, null);
        }
    });
    try {
        return (ByteBufferReuse) clazz.newInstance();
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
Example 21
Project: SmartAndroidSource-master  File: UnsafeAllocator.java View source code
public static UnsafeAllocator create() {
    // }
    try {
        Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
        Field f = unsafeClass.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        final Object unsafe = f.get(null);
        final Method allocateInstance = unsafeClass.getMethod("allocateInstance", Class.class);
        return new UnsafeAllocator() {

            @Override
            @SuppressWarnings("unchecked")
            public <T> T newInstance(Class<T> c) throws Exception {
                return (T) allocateInstance.invoke(unsafe, c);
            }
        };
    } catch (Exception ignored) {
    }
    // }
    try {
        final Method newInstance = ObjectInputStream.class.getDeclaredMethod("newInstance", Class.class, Class.class);
        newInstance.setAccessible(true);
        return new UnsafeAllocator() {

            @Override
            @SuppressWarnings("unchecked")
            public <T> T newInstance(Class<T> c) throws Exception {
                return (T) newInstance.invoke(null, c, Object.class);
            }
        };
    } catch (Exception ignored) {
    }
    // }
    try {
        Method getConstructorId = ObjectStreamClass.class.getDeclaredMethod("getConstructorId", Class.class);
        getConstructorId.setAccessible(true);
        final int constructorId = (Integer) getConstructorId.invoke(null, Object.class);
        final Method newInstance = ObjectStreamClass.class.getDeclaredMethod("newInstance", Class.class, int.class);
        newInstance.setAccessible(true);
        return new UnsafeAllocator() {

            @Override
            @SuppressWarnings("unchecked")
            public <T> T newInstance(Class<T> c) throws Exception {
                return (T) newInstance.invoke(null, c, constructorId);
            }
        };
    } catch (Exception ignored) {
    }
    // give up
    return new UnsafeAllocator() {

        @Override
        public <T> T newInstance(Class<T> c) {
            throw new UnsupportedOperationException("Cannot allocate " + c);
        }
    };
}
Example 22
Project: netty4.0.27Learn-master  File: PlatformDependent.java View source code
private static boolean hasUnsafe0() {
    boolean noUnsafe = SystemPropertyUtil.getBoolean("io.netty.noUnsafe", false);
    logger.debug("-Dio.netty.noUnsafe: {}", noUnsafe);
    if (isAndroid()) {
        logger.debug("sun.misc.Unsafe: unavailable (Android)");
        return false;
    }
    if (noUnsafe) {
        logger.debug("sun.misc.Unsafe: unavailable (io.netty.noUnsafe)");
        return false;
    }
    // Legacy properties
    boolean tryUnsafe;
    if (SystemPropertyUtil.contains("io.netty.tryUnsafe")) {
        tryUnsafe = SystemPropertyUtil.getBoolean("io.netty.tryUnsafe", true);
    } else {
        tryUnsafe = SystemPropertyUtil.getBoolean("org.jboss.netty.tryUnsafe", true);
    }
    if (!tryUnsafe) {
        logger.debug("sun.misc.Unsafe: unavailable (io.netty.tryUnsafe/org.jboss.netty.tryUnsafe)");
        return false;
    }
    try {
        boolean hasUnsafe = PlatformDependent0.hasUnsafe();
        logger.debug("sun.misc.Unsafe: {}", hasUnsafe ? "available" : "unavailable");
        return hasUnsafe;
    } catch (Throwable t) {
        return false;
    }
}
Example 23
Project: Hungry-master  File: UnsafeAllocator.java View source code
public static UnsafeAllocator create() {
    // }
    try {
        Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
        Field f = unsafeClass.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        final Object unsafe = f.get(null);
        final Method allocateInstance = unsafeClass.getMethod("allocateInstance", Class.class);
        return new UnsafeAllocator() {

            @Override
            @SuppressWarnings("unchecked")
            public <T> T newInstance(Class<T> c) throws Exception {
                return (T) allocateInstance.invoke(unsafe, c);
            }
        };
    } catch (Exception ignored) {
    }
    // }
    try {
        final Method newInstance = ObjectInputStream.class.getDeclaredMethod("newInstance", Class.class, Class.class);
        newInstance.setAccessible(true);
        return new UnsafeAllocator() {

            @Override
            @SuppressWarnings("unchecked")
            public <T> T newInstance(Class<T> c) throws Exception {
                return (T) newInstance.invoke(null, c, Object.class);
            }
        };
    } catch (Exception ignored) {
    }
    // }
    try {
        Method getConstructorId = ObjectStreamClass.class.getDeclaredMethod("getConstructorId", Class.class);
        getConstructorId.setAccessible(true);
        final int constructorId = (Integer) getConstructorId.invoke(null, Object.class);
        final Method newInstance = ObjectStreamClass.class.getDeclaredMethod("newInstance", Class.class, int.class);
        newInstance.setAccessible(true);
        return new UnsafeAllocator() {

            @Override
            @SuppressWarnings("unchecked")
            public <T> T newInstance(Class<T> c) throws Exception {
                return (T) newInstance.invoke(null, c, constructorId);
            }
        };
    } catch (Exception ignored) {
    }
    // give up
    return new UnsafeAllocator() {

        @Override
        public <T> T newInstance(Class<T> c) {
            throw new UnsupportedOperationException("Cannot allocate " + c);
        }
    };
}
Example 24
Project: newFunf-master  File: UnsafeAllocator.java View source code
public static UnsafeAllocator create() {
    // }
    try {
        Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
        Field f = unsafeClass.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        final Object unsafe = f.get(null);
        final Method allocateInstance = unsafeClass.getMethod("allocateInstance", Class.class);
        return new UnsafeAllocator() {

            @Override
            @SuppressWarnings("unchecked")
            public <T> T newInstance(Class<T> c) throws Exception {
                return (T) allocateInstance.invoke(unsafe, c);
            }
        };
    } catch (Exception ignored) {
    }
    // }
    try {
        final Method newInstance = ObjectInputStream.class.getDeclaredMethod("newInstance", Class.class, Class.class);
        newInstance.setAccessible(true);
        return new UnsafeAllocator() {

            @Override
            @SuppressWarnings("unchecked")
            public <T> T newInstance(Class<T> c) throws Exception {
                return (T) newInstance.invoke(null, c, Object.class);
            }
        };
    } catch (Exception ignored) {
    }
    // }
    try {
        Method getConstructorId = ObjectStreamClass.class.getDeclaredMethod("getConstructorId", Class.class);
        getConstructorId.setAccessible(true);
        final int constructorId = (Integer) getConstructorId.invoke(null, Object.class);
        final Method newInstance = ObjectStreamClass.class.getDeclaredMethod("newInstance", Class.class, int.class);
        newInstance.setAccessible(true);
        return new UnsafeAllocator() {

            @Override
            @SuppressWarnings("unchecked")
            public <T> T newInstance(Class<T> c) throws Exception {
                return (T) newInstance.invoke(null, c, constructorId);
            }
        };
    } catch (Exception ignored) {
    }
    // give up
    return new UnsafeAllocator() {

        @Override
        public <T> T newInstance(Class<T> c) {
            throw new UnsupportedOperationException("Cannot allocate " + c);
        }
    };
}
Example 25
Project: trie4j-master  File: UnsafeDoubleArray.java View source code
@Override
public boolean isTerminate() {
    int nid = nodeId;
    while (true) {
        CharSequence children = listupChildChars(nid);
        int n = children.length();
        if (n == 0)
            return term.get(nid);
        int b = offsetToIntArrayIndex(base[nid]);
        char firstChar = children.charAt(0);
        if (n > 1) {
            return term.get(nid);
        } else {
            int firstNid = b + charToScaledCode[firstChar] / Unsafe.ARRAY_INT_INDEX_SCALE;
            if (term.get(firstNid))
                return true;
            nid = firstNid;
        }
    }
}