當前位置: 首頁>>代碼示例>>Java>>正文


Java Unsafe.INVALID_FIELD_OFFSET屬性代碼示例

本文整理匯總了Java中sun.misc.Unsafe.INVALID_FIELD_OFFSET屬性的典型用法代碼示例。如果您正苦於以下問題:Java Unsafe.INVALID_FIELD_OFFSET屬性的具體用法?Java Unsafe.INVALID_FIELD_OFFSET怎麽用?Java Unsafe.INVALID_FIELD_OFFSET使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在sun.misc.Unsafe的用法示例。


在下文中一共展示了Unsafe.INVALID_FIELD_OFFSET屬性的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: FieldReflector

/**
 * 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;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:36,代碼來源:ObjectStreamClass.java

示例2: setObjFieldValues

/**
 * 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();
        }
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:40,代碼來源:ObjectStreamClass.java

示例3: setObjFieldValues

/**
 * 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.
 */
public 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();
        }
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:40,代碼來源:ObjectStreamClass.java

示例4: setPrimFieldValues

/**
 * 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();
        }
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:53,代碼來源:ObjectStreamClass.java

示例5: setPrimFieldValues

/**
 * 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.
 */
public 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();
        }
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:53,代碼來源:ObjectStreamClass.java


注:本文中的sun.misc.Unsafe.INVALID_FIELD_OFFSET屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。