Class UnsafeAccess


  • @SuppressAnimalSniffer
    public final class UnsafeAccess
    extends java.lang.Object
    All use of this class MUST first check that UnsafeAccess.isUnsafeAvailable() == true otherwise NPEs will happen in environments without "suc.misc.Unsafe" such as Android.

    Note that you can force RxJava to not use Unsafe API by setting any value to System Property rx.unsafe-disable.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static boolean DISABLED_BY_USER  
      static sun.misc.Unsafe UNSAFE  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private UnsafeAccess()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long addressOf​(java.lang.Class<?> clazz, java.lang.String fieldName)
      Returns the address of the specific field on the class and wraps a NoSuchFieldException into an internal error.
      static boolean compareAndSwapInt​(java.lang.Object obj, long offset, int expected, int newValue)  
      static int getAndAddInt​(java.lang.Object obj, long offset, int n)  
      static int getAndIncrementInt​(java.lang.Object obj, long offset)  
      static int getAndSetInt​(java.lang.Object obj, long offset, int newValue)  
      static boolean isUnsafeAvailable()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UNSAFE

        public static final sun.misc.Unsafe UNSAFE
      • DISABLED_BY_USER

        private static final boolean DISABLED_BY_USER
    • Constructor Detail

      • UnsafeAccess

        private UnsafeAccess()
    • Method Detail

      • isUnsafeAvailable

        public static boolean isUnsafeAvailable()
      • getAndIncrementInt

        public static int getAndIncrementInt​(java.lang.Object obj,
                                             long offset)
      • getAndAddInt

        public static int getAndAddInt​(java.lang.Object obj,
                                       long offset,
                                       int n)
      • getAndSetInt

        public static int getAndSetInt​(java.lang.Object obj,
                                       long offset,
                                       int newValue)
      • compareAndSwapInt

        public static boolean compareAndSwapInt​(java.lang.Object obj,
                                                long offset,
                                                int expected,
                                                int newValue)
      • addressOf

        public static long addressOf​(java.lang.Class<?> clazz,
                                     java.lang.String fieldName)
        Returns the address of the specific field on the class and wraps a NoSuchFieldException into an internal error.

        One can avoid using static initializers this way and just assign the address directly to the target static field.

        Parameters:
        clazz - the target class
        fieldName - the target field name
        Returns:
        the address (offset) of the field