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

The following examples show how to use sun.misc.Unsafe#getShort() . 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: UnsafeSubstitutionsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("all")
public static int unsafePutShort(Unsafe unsafe, Object obj, long offset, short value) {
    int res = 1;
    unsafe.putShort(obj, offset, (short) (value + 1));
    res += unsafe.getShort(obj, offset);
    unsafe.putShortVolatile(obj, offset, (short) (value + 2));
    res += unsafe.getShort(obj, offset);
    return res;
}
 
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 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 3
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 4
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 unsafeGetShort(Unsafe unsafe, Object obj, long offset) {
    return unsafe.getShort(obj, offset) + unsafe.getShortVolatile(obj, offset);
}