Java Code Examples for sun.misc.Unsafe#INVALID_FIELD_OFFSET

The following examples show how to use sun.misc.Unsafe#INVALID_FIELD_OFFSET . 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: ObjectStreamClass.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs FieldReflector capable of setting/getting values from the
 * subset of fields whose ObjectStreamFields contain non-null
 * reflective Field objects.  ObjectStreamFields with null Fields are
 * treated as filler, for which get operations return default values
 * and set operations discard given values.
 */
FieldReflector(ObjectStreamField[] fields) {
    this.fields = fields;
    int nfields = fields.length;
    readKeys = new long[nfields];
    writeKeys = new long[nfields];
    offsets = new int[nfields];
    typeCodes = new char[nfields];
    ArrayList<Class<?>> typeList = new ArrayList<>();
    Set<Long> usedKeys = new HashSet<>();


    for (int i = 0; i < nfields; i++) {
        ObjectStreamField f = fields[i];
        Field rf = f.getField();
        long key = (rf != null) ?
            unsafe.objectFieldOffset(rf) : Unsafe.INVALID_FIELD_OFFSET;
        readKeys[i] = key;
        writeKeys[i] = usedKeys.add(key) ?
            key : Unsafe.INVALID_FIELD_OFFSET;
        offsets[i] = f.getOffset();
        typeCodes[i] = f.getTypeCode();
        if (!f.isPrimitive()) {
            typeList.add((rf != null) ? rf.getType() : null);
        }
    }

    types = typeList.toArray(new Class<?>[typeList.size()]);
    numPrimFields = nfields - types.length;
}
 
Example 2
Source File: ObjectStreamClass.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the serializable object fields of object obj using values from
 * array vals starting at offset 0.  The caller is responsible for
 * ensuring that obj is of the proper type; however, attempts to set a
 * field with a value of the wrong type will trigger an appropriate
 * ClassCastException.
 */
void setObjFieldValues(Object obj, Object[] vals) {
    if (obj == null) {
        throw new NullPointerException();
    }
    for (int i = numPrimFields; i < fields.length; i++) {
        long key = writeKeys[i];
        if (key == Unsafe.INVALID_FIELD_OFFSET) {
            continue;           // discard value
        }
        switch (typeCodes[i]) {
            case 'L':
            case '[':
                Object val = vals[offsets[i]];
                if (val != null &&
                    !types[i - numPrimFields].isInstance(val))
                {
                    Field f = fields[i].getField();
                    throw new ClassCastException(
                        "cannot assign instance of " +
                        val.getClass().getName() + " to field " +
                        f.getDeclaringClass().getName() + "." +
                        f.getName() + " of type " +
                        f.getType().getName() + " in instance of " +
                        obj.getClass().getName());
                }
                unsafe.putObject(obj, key, val);
                break;

            default:
                throw new InternalError();
        }
    }
}
 
Example 3
Source File: UnsafeSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
DateFieldSerializer(Field field)
{
    _field = field;
    _offset = _unsafe.objectFieldOffset(field);

    if (_offset == Unsafe.INVALID_FIELD_OFFSET)
        throw new IllegalStateException();
}
 
Example 4
Source File: ObjectStreamClass.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the serializable object fields of object obj using values from
 * array vals starting at offset 0.  The caller is responsible for
 * ensuring that obj is of the proper type; however, attempts to set a
 * field with a value of the wrong type will trigger an appropriate
 * ClassCastException.
 */
void setObjFieldValues(Object obj, Object[] vals) {
    if (obj == null) {
        throw new NullPointerException();
    }
    for (int i = numPrimFields; i < fields.length; i++) {
        long key = writeKeys[i];
        if (key == Unsafe.INVALID_FIELD_OFFSET) {
            continue;           // discard value
        }
        switch (typeCodes[i]) {
            case 'L':
            case '[':
                Object val = vals[offsets[i]];
                if (val != null &&
                    !types[i - numPrimFields].isInstance(val))
                {
                    Field f = fields[i].getField();
                    throw new ClassCastException(
                        "cannot assign instance of " +
                        val.getClass().getName() + " to field " +
                        f.getDeclaringClass().getName() + "." +
                        f.getName() + " of type " +
                        f.getType().getName() + " in instance of " +
                        obj.getClass().getName());
                }
                unsafe.putObject(obj, key, val);
                break;

            default:
                throw new InternalError();
        }
    }
}
 
Example 5
Source File: ObjectStreamClass.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Constructs FieldReflector capable of setting/getting values from the
 * subset of fields whose ObjectStreamFields contain non-null
 * reflective Field objects.  ObjectStreamFields with null Fields are
 * treated as filler, for which get operations return default values
 * and set operations discard given values.
 */
FieldReflector(ObjectStreamField[] fields) {
    this.fields = fields;
    int nfields = fields.length;
    readKeys = new long[nfields];
    writeKeys = new long[nfields];
    offsets = new int[nfields];
    typeCodes = new char[nfields];
    ArrayList<Class<?>> typeList = new ArrayList<>();
    Set<Long> usedKeys = new HashSet<>();


    for (int i = 0; i < nfields; i++) {
        ObjectStreamField f = fields[i];
        Field rf = f.getField();
        long key = (rf != null) ?
            unsafe.objectFieldOffset(rf) : Unsafe.INVALID_FIELD_OFFSET;
        readKeys[i] = key;
        writeKeys[i] = usedKeys.add(key) ?
            key : Unsafe.INVALID_FIELD_OFFSET;
        offsets[i] = f.getOffset();
        typeCodes[i] = f.getTypeCode();
        if (!f.isPrimitive()) {
            typeList.add((rf != null) ? rf.getType() : null);
        }
    }

    types = typeList.toArray(new Class<?>[typeList.size()]);
    numPrimFields = nfields - types.length;
}
 
Example 6
Source File: ObjectStreamClass.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the serializable object fields of object obj using values from
 * array vals starting at offset 0.  The caller is responsible for
 * ensuring that obj is of the proper type; however, attempts to set a
 * field with a value of the wrong type will trigger an appropriate
 * ClassCastException.
 */
void setObjFieldValues(Object obj, Object[] vals) {
    if (obj == null) {
        throw new NullPointerException();
    }
    for (int i = numPrimFields; i < fields.length; i++) {
        long key = writeKeys[i];
        if (key == Unsafe.INVALID_FIELD_OFFSET) {
            continue;           // discard value
        }
        switch (typeCodes[i]) {
            case 'L':
            case '[':
                Object val = vals[offsets[i]];
                if (val != null &&
                    !types[i - numPrimFields].isInstance(val))
                {
                    Field f = fields[i].getField();
                    throw new ClassCastException(
                        "cannot assign instance of " +
                        val.getClass().getName() + " to field " +
                        f.getDeclaringClass().getName() + "." +
                        f.getName() + " of type " +
                        f.getType().getName() + " in instance of " +
                        obj.getClass().getName());
                }
                unsafe.putObject(obj, key, val);
                break;

            default:
                throw new InternalError();
        }
    }
}
 
Example 7
Source File: ObjectStreamClass.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the serializable object fields of object obj using values from
 * array vals starting at offset 0.  The caller is responsible for
 * ensuring that obj is of the proper type; however, attempts to set a
 * field with a value of the wrong type will trigger an appropriate
 * ClassCastException.
 */
void setObjFieldValues(Object obj, Object[] vals) {
    if (obj == null) {
        throw new NullPointerException();
    }
    for (int i = numPrimFields; i < fields.length; i++) {
        long key = writeKeys[i];
        if (key == Unsafe.INVALID_FIELD_OFFSET) {
            continue;           // discard value
        }
        switch (typeCodes[i]) {
            case 'L':
            case '[':
                Object val = vals[offsets[i]];
                if (val != null &&
                    !types[i - numPrimFields].isInstance(val))
                {
                    Field f = fields[i].getField();
                    throw new ClassCastException(
                        "cannot assign instance of " +
                        val.getClass().getName() + " to field " +
                        f.getDeclaringClass().getName() + "." +
                        f.getName() + " of type " +
                        f.getType().getName() + " in instance of " +
                        obj.getClass().getName());
                }
                unsafe.putObject(obj, key, val);
                break;

            default:
                throw new InternalError();
        }
    }
}
 
Example 8
Source File: UnsafeSerializer.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
FloatFieldSerializer(Field field) {
    _field = field;
    _offset = _unsafe.objectFieldOffset(field);

    if (_offset == Unsafe.INVALID_FIELD_OFFSET) {
        throw new IllegalStateException();
    }
}
 
Example 9
Source File: UnsafeSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
ByteFieldSerializer(Field field)
{
    _field = field;
    _offset = _unsafe.objectFieldOffset(field);

    if (_offset == Unsafe.INVALID_FIELD_OFFSET)
        throw new IllegalStateException();
}
 
Example 10
Source File: UnsafeSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
IntFieldSerializer(Field field)
{
    _field = field;
    _offset = _unsafe.objectFieldOffset(field);

    if (_offset == Unsafe.INVALID_FIELD_OFFSET)
        throw new IllegalStateException();
}
 
Example 11
Source File: UnsafeSerializer.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
ByteFieldSerializer(Field field) {
    _field = field;
    _offset = _unsafe.objectFieldOffset(field);

    if (_offset == Unsafe.INVALID_FIELD_OFFSET) {
        throw new IllegalStateException();
    }
}
 
Example 12
Source File: UnsafeSerializer.java    From jvm-sandbox-repeater with Apache License 2.0 5 votes vote down vote up
ShortFieldSerializer(Field field)
{
  _field = field;
  _offset = _unsafe.objectFieldOffset(field);
  
  if (_offset == Unsafe.INVALID_FIELD_OFFSET)
    throw new IllegalStateException();
}
 
Example 13
Source File: ObjectStreamClass.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs FieldReflector capable of setting/getting values from the
 * subset of fields whose ObjectStreamFields contain non-null
 * reflective Field objects.  ObjectStreamFields with null Fields are
 * treated as filler, for which get operations return default values
 * and set operations discard given values.
 */
FieldReflector(ObjectStreamField[] fields) {
    this.fields = fields;
    int nfields = fields.length;
    readKeys = new long[nfields];
    writeKeys = new long[nfields];
    offsets = new int[nfields];
    typeCodes = new char[nfields];
    ArrayList<Class<?>> typeList = new ArrayList<>();
    Set<Long> usedKeys = new HashSet<>();


    for (int i = 0; i < nfields; i++) {
        ObjectStreamField f = fields[i];
        Field rf = f.getField();
        long key = (rf != null) ?
            unsafe.objectFieldOffset(rf) : Unsafe.INVALID_FIELD_OFFSET;
        readKeys[i] = key;
        writeKeys[i] = usedKeys.add(key) ?
            key : Unsafe.INVALID_FIELD_OFFSET;
        offsets[i] = f.getOffset();
        typeCodes[i] = f.getTypeCode();
        if (!f.isPrimitive()) {
            typeList.add((rf != null) ? rf.getType() : null);
        }
    }

    types = typeList.toArray(new Class<?>[typeList.size()]);
    numPrimFields = nfields - types.length;
}
 
Example 14
Source File: UnsafeSerializer.java    From jvm-sandbox-repeater with Apache License 2.0 5 votes vote down vote up
ByteFieldSerializer(Field field)
{
  _field = field;
  _offset = _unsafe.objectFieldOffset(field);
  
  if (_offset == Unsafe.INVALID_FIELD_OFFSET)
    throw new IllegalStateException();
}
 
Example 15
Source File: UnsafeSerializer.java    From jvm-sandbox-repeater with Apache License 2.0 5 votes vote down vote up
BooleanFieldSerializer(Field field)
{
  _field = field;
  _offset = _unsafe.objectFieldOffset(field);
  
  if (_offset == Unsafe.INVALID_FIELD_OFFSET)
    throw new IllegalStateException();
}
 
Example 16
Source File: UnsafeSerializer.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
CharFieldSerializer(Field field)
{
    _field = field;
    _offset = _unsafe.objectFieldOffset(field);

    if (_offset == Unsafe.INVALID_FIELD_OFFSET)
        throw new IllegalStateException();
}
 
Example 17
Source File: ObjectStreamClass.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the serializable object fields of object obj using values from
 * array vals starting at offset 0.  The caller is responsible for
 * ensuring that obj is of the proper type; however, attempts to set a
 * field with a value of the wrong type will trigger an appropriate
 * ClassCastException.
 */
void setObjFieldValues(Object obj, Object[] vals) {
    if (obj == null) {
        throw new NullPointerException();
    }
    for (int i = numPrimFields; i < fields.length; i++) {
        long key = writeKeys[i];
        if (key == Unsafe.INVALID_FIELD_OFFSET) {
            continue;           // discard value
        }
        switch (typeCodes[i]) {
            case 'L':
            case '[':
                Object val = vals[offsets[i]];
                if (val != null &&
                    !types[i - numPrimFields].isInstance(val))
                {
                    Field f = fields[i].getField();
                    throw new ClassCastException(
                        "cannot assign instance of " +
                        val.getClass().getName() + " to field " +
                        f.getDeclaringClass().getName() + "." +
                        f.getName() + " of type " +
                        f.getType().getName() + " in instance of " +
                        obj.getClass().getName());
                }
                unsafe.putObject(obj, key, val);
                break;

            default:
                throw new InternalError();
        }
    }
}
 
Example 18
Source File: ObjectStreamClass.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the serializable primitive fields of object obj using values
 * unmarshalled from byte array buf starting at offset 0.  The caller
 * is responsible for ensuring that obj is of the proper type.
 */
void setPrimFieldValues(Object obj, byte[] buf) {
    if (obj == null) {
        throw new NullPointerException();
    }
    for (int i = 0; i < numPrimFields; i++) {
        long key = writeKeys[i];
        if (key == Unsafe.INVALID_FIELD_OFFSET) {
            continue;           // discard value
        }
        int off = offsets[i];
        switch (typeCodes[i]) {
            case 'Z':
                unsafe.putBoolean(obj, key, Bits.getBoolean(buf, off));
                break;

            case 'B':
                unsafe.putByte(obj, key, buf[off]);
                break;

            case 'C':
                unsafe.putChar(obj, key, Bits.getChar(buf, off));
                break;

            case 'S':
                unsafe.putShort(obj, key, Bits.getShort(buf, off));
                break;

            case 'I':
                unsafe.putInt(obj, key, Bits.getInt(buf, off));
                break;

            case 'F':
                unsafe.putFloat(obj, key, Bits.getFloat(buf, off));
                break;

            case 'J':
                unsafe.putLong(obj, key, Bits.getLong(buf, off));
                break;

            case 'D':
                unsafe.putDouble(obj, key, Bits.getDouble(buf, off));
                break;

            default:
                throw new InternalError();
        }
    }
}
 
Example 19
Source File: ObjectStreamClass.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the serializable primitive fields of object obj using values
 * unmarshalled from byte array buf starting at offset 0.  The caller
 * is responsible for ensuring that obj is of the proper type.
 */
void setPrimFieldValues(Object obj, byte[] buf) {
    if (obj == null) {
        throw new NullPointerException();
    }
    for (int i = 0; i < numPrimFields; i++) {
        long key = writeKeys[i];
        if (key == Unsafe.INVALID_FIELD_OFFSET) {
            continue;           // discard value
        }
        int off = offsets[i];
        switch (typeCodes[i]) {
            case 'Z':
                unsafe.putBoolean(obj, key, Bits.getBoolean(buf, off));
                break;

            case 'B':
                unsafe.putByte(obj, key, buf[off]);
                break;

            case 'C':
                unsafe.putChar(obj, key, Bits.getChar(buf, off));
                break;

            case 'S':
                unsafe.putShort(obj, key, Bits.getShort(buf, off));
                break;

            case 'I':
                unsafe.putInt(obj, key, Bits.getInt(buf, off));
                break;

            case 'F':
                unsafe.putFloat(obj, key, Bits.getFloat(buf, off));
                break;

            case 'J':
                unsafe.putLong(obj, key, Bits.getLong(buf, off));
                break;

            case 'D':
                unsafe.putDouble(obj, key, Bits.getDouble(buf, off));
                break;

            default:
                throw new InternalError();
        }
    }
}
 
Example 20
Source File: ObjectStreamClass.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the serializable primitive fields of object obj using values
 * unmarshalled from byte array buf starting at offset 0.  The caller
 * is responsible for ensuring that obj is of the proper type.
 */
void setPrimFieldValues(Object obj, byte[] buf) {
    if (obj == null) {
        throw new NullPointerException();
    }
    for (int i = 0; i < numPrimFields; i++) {
        long key = writeKeys[i];
        if (key == Unsafe.INVALID_FIELD_OFFSET) {
            continue;           // discard value
        }
        int off = offsets[i];
        switch (typeCodes[i]) {
            case 'Z':
                unsafe.putBoolean(obj, key, Bits.getBoolean(buf, off));
                break;

            case 'B':
                unsafe.putByte(obj, key, buf[off]);
                break;

            case 'C':
                unsafe.putChar(obj, key, Bits.getChar(buf, off));
                break;

            case 'S':
                unsafe.putShort(obj, key, Bits.getShort(buf, off));
                break;

            case 'I':
                unsafe.putInt(obj, key, Bits.getInt(buf, off));
                break;

            case 'F':
                unsafe.putFloat(obj, key, Bits.getFloat(buf, off));
                break;

            case 'J':
                unsafe.putLong(obj, key, Bits.getLong(buf, off));
                break;

            case 'D':
                unsafe.putDouble(obj, key, Bits.getDouble(buf, off));
                break;

            default:
                throw new InternalError();
        }
    }
}