EmulatedFieldspublic 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. |
Fields Summary |
---|
private ObjectSlot[] | slotsToSerialize | private ObjectStreamField[] | declaredFields |
Constructors Summary |
---|
public EmulatedFields(ObjectStreamField[] fields, ObjectStreamField[] declared)Constructs a new instance of EmulatedFields.
super();
// We assume the slots are already sorted in the right shape for dumping
buildSlots(fields);
declaredFields = declared;
|
Methods Summary |
---|
private void | buildSlots(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.
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 boolean | defaulted(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.
ObjectSlot slot = findSlot(name, null);
if (slot == null) {
throw new IllegalArgumentException();
}
return slot.defaulted;
| private java.io.EmulatedFields$ObjectSlot | findSlot(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.
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 long | get(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.
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.Object | get(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.
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 short | get(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.
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 boolean | get(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.
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 byte | get(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.
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 char | get(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.
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 double | get(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.
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 float | get(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.
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 int | get(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.
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 void | put(java.lang.String name, byte value)Find and set the byte value of a given field named name in
the receiver.
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 void | put(java.lang.String name, char value)Find and set the char value of a given field named name in
the receiver.
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 void | put(java.lang.String name, double value)Find and set the double value of a given field named name
in the receiver.
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 void | put(java.lang.String name, float value)Find and set the float value of a given field named name
in the receiver.
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 void | put(java.lang.String name, int value)Find and set the int value of a given field named name in
the receiver.
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 void | put(java.lang.String name, long value)Find and set the long value of a given field named name in
the receiver.
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 void | put(java.lang.String name, java.lang.Object value)Find and set the Object value of a given field named name
in the receiver.
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 void | put(java.lang.String name, short value)Find and set the short value of a given field named name
in the receiver.
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 void | put(java.lang.String name, boolean value)Find and set the boolean value of a given field named name
in the receiver.
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 slotsToSerialize;
|
|