Java Code Examples for sun.misc.Unsafe#getInt()

The following examples show how to use sun.misc.Unsafe#getInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From mnemonic with Apache License 2.0 6 votes vote down vote up
/**
 * get the address of an object
 *
 * @param unsafe
 *         an unsafe object
 * @param o
 *         an object to retrieve its address
 *
 * @return
 *         the address of this object
 */
public static long addressOf(Unsafe unsafe, Object o) {
  Object[] array = new Object[] {o};

  long baseOffset = unsafe.arrayBaseOffset(Object[].class);
  int addressSize = unsafe.addressSize();
  long objectAddress;
  switch (addressSize) {
    case 4:
      objectAddress = unsafe.getInt(array, baseOffset);
      break;
    case 8:
      objectAddress = unsafe.getLong(array, baseOffset);
      break;
    default:
      throw new Error("unsupported address size: " + addressSize);
  }
  return objectAddress;
}
 
Example 2
Source File: UnsafeSubstitutionsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("all")
public static int unsafePutInt(Unsafe unsafe, Object obj, long offset, int value) {
    int res = 1;
    unsafe.putInt(obj, offset, value);
    res += unsafe.getInt(obj, offset);
    unsafe.putIntVolatile(obj, offset, value + 1);
    res += unsafe.getInt(obj, offset);
    unsafe.putOrderedInt(obj, offset, value + 2);
    res += unsafe.getInt(obj, offset);
    return res;
}
 
Example 3
Source File: UnsafeSubstitutionsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("all")
public static double unsafeDirectMemoryRead(Unsafe unsafe, long address) {
    // Unsafe.getBoolean(long) and Unsafe.getObject(long) do not exist
    // @formatter:off
    return unsafe.getByte(address) +
           unsafe.getShort(address + 8) +
           unsafe.getChar(address + 16) +
           unsafe.getInt(address + 24) +
           unsafe.getLong(address + 32) +
           unsafe.getFloat(address + 40) +
           unsafe.getDouble(address + 48);
    // @formatter:on
}
 
Example 4
Source File: BeanAccessor.java    From TarsJava with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static Object getBeanValue(Object bean, String fieldName) {
    if (bean == null || fieldName == null) {
        return null;
    }
    Class<?> clazz = bean.getClass();

    Map<String, Field> fieldMap = getBeanFieldMap(clazz);
    Field field = fieldMap.get(fieldName);
    Unsafe unsafe = getUnsafeInstance();
    if (unsafe == null) {
        try {
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            return field.get(bean);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    long offset = getBeanFieldOffset(clazz, fieldName);
    if (offset == -1) {
        return null;
    }

    Class<?> type = field.getType();
    if (type == boolean.class) {
        return unsafe.getBoolean(bean, offset);
    } else if (type == byte.class) {
        return unsafe.getByte(bean, offset);
    } else if (type == short.class) {
        return unsafe.getShort(bean, offset);
    } else if (type == char.class) {
        return unsafe.getChar(bean, offset);
    } else if (type == int.class) {
        return unsafe.getInt(bean, offset);
    } else if (type == long.class) {
        return unsafe.getLong(bean, offset);
    } else if (type == float.class) {
        return unsafe.getFloat(bean, offset);
    } else if (type == double.class) {
        return unsafe.getDouble(bean, offset);
    }
    return unsafe.getObject(bean, offset);

}
 
Example 5
Source File: UnsafeSubstitutionsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("all")
public static int unsafeGetInt(Unsafe unsafe, Object obj, long offset) {
    return unsafe.getInt(obj, offset) + unsafe.getIntVolatile(obj, offset);
}