FileDocCategorySizeDatePackage
EmulatedFields.javaAPI DocAndroid 1.5 API23502Wed May 06 22:41:04 BST 2009java.io

EmulatedFields

public class EmulatedFields extends Object
An EmulatedFields is an object that represents a set of emulated fields for an object being dumped or loaded. It allows objects to be dumped with a shape different than the fields they were declared to have.
see
ObjectInputStream.GetField
see
ObjectOutputStream.PutField
see
EmulatedFieldsForLoading
see
EmulatedFieldsForDumping

Fields Summary
private ObjectSlot[]
slotsToSerialize
private ObjectStreamField[]
declaredFields
Constructors Summary
public EmulatedFields(ObjectStreamField[] fields, ObjectStreamField[] declared)
Constructs a new instance of EmulatedFields.

param
fields an array of ObjectStreamFields, which describe the fields to be emulated (names, types, etc).
param
declared an array of ObjectStreamFields, which describe the declared fields.

        super();
        // We assume the slots are already sorted in the right shape for dumping
        buildSlots(fields);
        declaredFields = declared;
    
Methods Summary
private voidbuildSlots(java.io.ObjectStreamField[] fields)
Build emulated slots that correspond to emulated fields. A slot is a field descriptor (ObjectStreamField) plus the actual value it holds.

param
fields an array of ObjectStreamField, which describe the fields to be emulated (names, types, etc).

        slotsToSerialize = new ObjectSlot[fields.length];
        for (int i = 0; i < fields.length; i++) {
            ObjectSlot s = new ObjectSlot();
            slotsToSerialize[i] = s;
            s.field = fields[i];
        }
        // We assume the slots are already sorted in the right shape for dumping
    
public booleandefaulted(java.lang.String name)
Return a boolean indicating if the field named name has been assigned a value explicitly (false) or if it still holds a default value for the type (true) because it hasn't been assigned to yet.

param
name a String, the name of the field to test
return
true if name still holds its default value, false otherwise
throws
IllegalArgumentException If name is null

        ObjectSlot slot = findSlot(name, null);
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        return slot.defaulted;
    
private java.io.EmulatedFields$ObjectSlotfindSlot(java.lang.String fieldName, java.lang.Class fieldType)
Find and return an ObjectSlot that corresponds to a field named fieldName and type fieldType. If the field type fieldType corresponds to a primitive type, the field type has to match exactly or null is returned. If the field type fieldType corresponds to an object type, the field type has to be compatible in terms of assignment, or null is returned. If fieldType is null, no such compatibility checking is performed and the slot is returned.

param
fieldName A String, the name of the field to find
param
fieldType A Class, the type of the field. This will be used to test compatibility. If null, no testing is done, the corresponding slot is returned.
return
If there is no field with that name, or no compatible field (relative to fieldType)

        boolean isPrimitive = fieldType != null && fieldType.isPrimitive();

        for (int i = 0; i < slotsToSerialize.length; i++) {
            ObjectSlot slot = slotsToSerialize[i];
            if (slot.field.getName().equals(fieldName)) {
                if (isPrimitive) {
                    // Looking for a primitive type field. Types must match
                    // *exactly*
                    if (slot.field.getType() == fieldType) {
                        return slot;
                    }
                } else {
                    // Looking for a non-primitive type field.
                    if (fieldType == null) {
                        return slot; // Null means we take anything
                    }
                    // Types must be compatible (assignment)
                    if (slot.field.getType().isAssignableFrom(fieldType)) {
                        return slot;
                    }
                }
            }
        }

        if (declaredFields != null) {
            for (int i = 0; i < declaredFields.length; i++) {
                ObjectStreamField field = declaredFields[i];
                if (field.getName().equals(fieldName)) {
                    if (isPrimitive ? field.getType() == fieldType
                            : fieldType == null
                                    || field.getType().isAssignableFrom(
                                            fieldType)) {
                        ObjectSlot slot = new ObjectSlot();
                        slot.field = field;
                        slot.defaulted = true;
                        return slot;
                    }
                }
            }
        }
        return null;
    
public longget(java.lang.String name, long defaultValue)
Find and return the long value of a given field named name in the receiver. If the field has not been assigned any value yet, the default value defaultValue is returned instead.

param
name A String, the name of the field to find
param
defaultValue Return value in case the field has not been assigned to yet.
return
the value of the given field if it has been assigned, the default value otherwise
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Long.TYPE);
        // if not initialized yet, we give the default value
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        return slot.defaulted ? defaultValue : ((Long) slot.fieldValue)
                .longValue();
    
public java.lang.Objectget(java.lang.String name, java.lang.Object defaultValue)
Find and return the Object value of a given field named name in the receiver. If the field has not been assigned any value yet, the default value defaultValue is returned instead.

param
name A String, the name of the field to find
param
defaultValue Return value in case the field has not been assigned to yet.
return
the value of the given field if it has been assigned, the default value otherwise
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, null);
        // if not initialized yet, we give the default value
        if (slot == null || slot.field.getType().isPrimitive()) {
            throw new IllegalArgumentException();
        }
        return slot.defaulted ? defaultValue : slot.fieldValue;
    
public shortget(java.lang.String name, short defaultValue)
Find and return the short value of a given field named name in the receiver. If the field has not been assigned any value yet, the default value defaultValue is returned instead.

param
name A String, the name of the field to find
param
defaultValue Return value in case the field has not been assigned to yet.
return
the value of the given field if it has been assigned, the default value otherwise
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Short.TYPE);
        // if not initialized yet, we give the default value
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        return slot.defaulted ? defaultValue : ((Short) slot.fieldValue)
                .shortValue();
    
public booleanget(java.lang.String name, boolean defaultValue)
Find and return the boolean value of a given field named name in the receiver. If the field has not been assigned any value yet, the default value defaultValue is returned instead.

param
name A String, the name of the field to find
param
defaultValue Return value in case the field has not been assigned to yet.
return
the value of the given field if it has been assigned, the default value otherwise
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Boolean.TYPE);
        // if not initialized yet, we give the default value
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        return slot.defaulted ? defaultValue : ((Boolean) slot.fieldValue)
                .booleanValue();
    
public byteget(java.lang.String name, byte defaultValue)
Find and return the byte value of a given field named name in the receiver. If the field has not been assigned any value yet, the default value defaultValue is returned instead.

param
name A String, the name of the field to find
param
defaultValue Return value in case the field has not been assigned to yet.
return
the value of the given field if it has been assigned, the default value otherwise
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Byte.TYPE);
        // if not initialized yet, we give the default value
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        return slot.defaulted ? defaultValue : ((Byte) slot.fieldValue)
                .byteValue();
    
public charget(java.lang.String name, char defaultValue)
Find and return the char value of a given field named name in the receiver. If the field has not been assigned any value yet, the default value defaultValue is returned instead.

param
name A String, the name of the field to find
param
defaultValue Return value in case the field has not been assigned to yet.
return
the value of the given field if it has been assigned, the default value otherwise
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Character.TYPE);
        // if not initialized yet, we give the default value
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        return slot.defaulted ? defaultValue : ((Character) slot.fieldValue)
                .charValue();
    
public doubleget(java.lang.String name, double defaultValue)
Find and return the double value of a given field named name in the receiver. If the field has not been assigned any value yet, the default value defaultValue is returned instead.

param
name A String, the name of the field to find
param
defaultValue Return value in case the field has not been assigned to yet.
return
the value of the given field if it has been assigned, the default value otherwise
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Double.TYPE);
        // if not initialized yet, we give the default value
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        return slot.defaulted ? defaultValue : ((Double) slot.fieldValue)
                .doubleValue();
    
public floatget(java.lang.String name, float defaultValue)
Find and return the float value of a given field named name in the receiver. If the field has not been assigned any value yet, the default value defaultValue is returned instead.

param
name A String, the name of the field to find
param
defaultValue Return value in case the field has not been assigned to yet.
return
the value of the given field if it has been assigned, the default value otherwise
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Float.TYPE);
        // if not initialized yet, we give the default value
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        return slot.defaulted ? defaultValue : ((Float) slot.fieldValue)
                .floatValue();
    
public intget(java.lang.String name, int defaultValue)
Find and return the int value of a given field named name in the receiver. If the field has not been assigned any value yet, the default value defaultValue is returned instead.

param
name A String, the name of the field to find
param
defaultValue Return value in case the field has not been assigned to yet.
return
the value of the given field if it has been assigned, the default value otherwise
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Integer.TYPE);
        // if not initialized yet, we give the default value
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        return slot.defaulted ? defaultValue : ((Integer) slot.fieldValue)
                .intValue();
    
public voidput(java.lang.String name, byte value)
Find and set the byte value of a given field named name in the receiver.

param
name A String, the name of the field to set
param
value New value for the field.
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Byte.TYPE);
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        slot.fieldValue = Byte.valueOf(value);
        slot.defaulted = false; // No longer default value
    
public voidput(java.lang.String name, char value)
Find and set the char value of a given field named name in the receiver.

param
name A String, the name of the field to set
param
value New value for the field.
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Character.TYPE);
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        slot.fieldValue = Character.valueOf(value);
        slot.defaulted = false; // No longer default value
    
public voidput(java.lang.String name, double value)
Find and set the double value of a given field named name in the receiver.

param
name A String, the name of the field to set
param
value New value for the field.
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Double.TYPE);
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        slot.fieldValue = Double.valueOf(value);
        slot.defaulted = false; // No longer default value
    
public voidput(java.lang.String name, float value)
Find and set the float value of a given field named name in the receiver.

param
name A String, the name of the field to set
param
value New value for the field.
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Float.TYPE);
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        slot.fieldValue = Float.valueOf(value);
        slot.defaulted = false; // No longer default value
    
public voidput(java.lang.String name, int value)
Find and set the int value of a given field named name in the receiver.

param
name A String, the name of the field to set
param
value New value for the field.
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Integer.TYPE);
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        slot.fieldValue = Integer.valueOf(value);
        slot.defaulted = false; // No longer default value
    
public voidput(java.lang.String name, long value)
Find and set the long value of a given field named name in the receiver.

param
name A String, the name of the field to set
param
value New value for the field.
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Long.TYPE);
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        slot.fieldValue = Long.valueOf(value);
        slot.defaulted = false; // No longer default value
    
public voidput(java.lang.String name, java.lang.Object value)
Find and set the Object value of a given field named name in the receiver.

param
name A String, the name of the field to set
param
value New value for the field.
throws
IllegalArgumentException If the corresponding field can not be found.

        Class<?> valueClass = null;
        if (value != null) {
            valueClass = value.getClass();
        }
        ObjectSlot slot = findSlot(name, valueClass);
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        slot.fieldValue = value;
        slot.defaulted = false; // No longer default value
    
public voidput(java.lang.String name, short value)
Find and set the short value of a given field named name in the receiver.

param
name A String, the name of the field to set
param
value New value for the field.
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Short.TYPE);
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        slot.fieldValue = Short.valueOf(value);
        slot.defaulted = false; // No longer default value
    
public voidput(java.lang.String name, boolean value)
Find and set the boolean value of a given field named name in the receiver.

param
name A String, the name of the field to set
param
value New value for the field.
throws
IllegalArgumentException If the corresponding field can not be found.

        ObjectSlot slot = findSlot(name, Boolean.TYPE);
        if (slot == null) {
            throw new IllegalArgumentException();
        }
        slot.fieldValue = Boolean.valueOf(value);
        slot.defaulted = false; // No longer default value
    
public java.io.EmulatedFields$ObjectSlot[]slots()
Return the array of ObjectSlot the receiver represents.

return
array of ObjectSlot the receiver represents.

        return slotsToSerialize;