/** * Constructs a new instance of EmulatedFieldsForDumping. * * @param streamClass * a ObjectStreamClass, which describe the fields to be emulated * (names, types, etc). */ EmulatedFieldsForDumping(ObjectOutputStream oos, ObjectStreamClass streamClass) { this.oos = oos; this.emulatedFields = new EmulatedFields(streamClass.fields(), (ObjectStreamField[]) null); }
/** * 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. */ public EmulatedFields(ObjectStreamField[] fields, ObjectStreamField[] declared) { // We assume the slots are already sorted in the right shape for dumping buildSlots(fields); declaredFields = declared; }
/** * Return a boolean indicating if the field named <code>name</code> 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 <code>true</code> if the field holds it default value, * <code>false</code> otherwise. * * @throws IOException * If an IO error occurs * @throws IllegalArgumentException * If the corresponding field can not be found. */ @Override public boolean defaulted(String name) throws IOException, IllegalArgumentException { return emulatedFields.defaulted(name); }
/** * Returns {@code true} indicating the field called {@code name} has not had * a value explicitly assigned and that it still holds a default value for * its type, or {@code false} indicating that the field named has been * assigned a value explicitly. * * @param name * the name of the field to test. * @return {@code true} if {@code name} still holds its default value, * {@code false} otherwise * * @throws IllegalArgumentException * if {@code name} is {@code null} */ public boolean defaulted(String name) throws IllegalArgumentException { ObjectSlot slot = findSlot(name, null); if (slot == null) { throw new IllegalArgumentException("no field '" + name + "'"); } return slot.defaulted; }
/** * Find and set the int value of a given field named <code>name</code> in * the receiver. * * @param name * A String, the name of the field to set * @param value * New value for the field. */ @Override public void put(String name, int value) { emulatedFields.put(name, value); }
/** * Finds and returns the Object value of a given field named {@code name} in * the receiver. If the field has not been assigned any value yet, the * default value {@code defaultValue} is returned instead. * * @param name * 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. */ public Object get(String name, Object defaultValue) throws IllegalArgumentException { ObjectSlot slot = findMandatorySlot(name, null); return slot.defaulted ? defaultValue : slot.fieldValue; }
/** * Find and return the short value of a given field named <code>name</code> * in the receiver. If the field has not been assigned any value yet, the * default value <code>defaultValue</code> 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, or the * default value otherwise * * @throws IOException * If an IO error occurs * @throws IllegalArgumentException * If the corresponding field can not be found. */ @Override public short get(String name, short defaultValue) throws IOException, IllegalArgumentException { return emulatedFields.get(name, defaultValue); }
for (EmulatedFields.ObjectSlot slot : accessibleSimulatedFields.slots()) { Object fieldValue = slot.getFieldValue(); Class<?> type = slot.getField().getType();
private ObjectSlot findMandatorySlot(String name, Class<?> type) { ObjectSlot slot = findSlot(name, type); if (slot == null || (type == null && slot.field.getType().isPrimitive())) { throw new IllegalArgumentException("no field '" + name + "' of type " + type); } return slot; }
/** * Find and set the double value of a given field named <code>name</code> * in the receiver. * * @param name * A String, the name of the field to set * @param value * New value for the field. */ @Override public void put(String name, double value) { emulatedFields.put(name, value); }
/** * Find and set the double value of a given field named {@code name} in the * receiver. * * @param name * the name of the field to set. * @param value * new value for the field. * * @throws IllegalArgumentException * if the corresponding field can not be found. */ public void put(String name, double value) throws IllegalArgumentException { ObjectSlot slot = findMandatorySlot(name, double.class); slot.fieldValue = Double.valueOf(value); slot.defaulted = false; // No longer default value }
/** * Find and return the char value of a given field named <code>name</code> * in the receiver. If the field has not been assigned any value yet, the * default value <code>defaultValue</code> 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, or the * default value otherwise * * @throws IOException * If an IO error occurs * @throws IllegalArgumentException * If the corresponding field can not be found. */ @Override public char get(String name, char defaultValue) throws IOException, IllegalArgumentException { return emulatedFields.get(name, defaultValue); }
EmulatedFields.ObjectSlot[] slots = emulatedFields.emulatedFields().slots(); for (ObjectSlot element : slots) { element.defaulted = false;
/** * Returns {@code true} indicating the field called {@code name} has not had * a value explicitly assigned and that it still holds a default value for * its type, or {@code false} indicating that the field named has been * assigned a value explicitly. * * @param name * the name of the field to test. * @return {@code true} if {@code name} still holds its default value, * {@code false} otherwise * * @throws IllegalArgumentException * if {@code name} is {@code null} */ public boolean defaulted(String name) throws IllegalArgumentException { ObjectSlot slot = findSlot(name, null); if (slot == null) { throw new IllegalArgumentException("no field '" + name + "'"); } return slot.defaulted; }
/** * Constructs a new instance of EmulatedFieldsForDumping. * * @param streamClass * an ObjectStreamClass, defining the class for which to emulate * fields. */ EmulatedFieldsForLoading(ObjectStreamClass streamClass) { this.streamClass = streamClass; emulatedFields = new EmulatedFields(streamClass.getLoadFields(), streamClass.fields()); }
/** * Find and set the Object value of a given field named <code>name</code> * in the receiver. * * @param name * A String, the name of the field to set * @param value * New value for the field. */ @Override public void put(String name, Object value) { emulatedFields.put(name, value); }
/** * Find and set the float value of a given field named {@code name} in the * receiver. * * @param name * the name of the field to set. * @param value * new value for the field. * * @throws IllegalArgumentException * if the corresponding field can not be found. */ public void put(String name, float value) throws IllegalArgumentException { ObjectSlot slot = findMandatorySlot(name, float.class); slot.fieldValue = Float.valueOf(value); slot.defaulted = false; // No longer default value }
/** * Find and return the float value of a given field named <code>name</code> * in the receiver. If the field has not been assigned any value yet, the * default value <code>defaultValue</code> 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, or the * default value otherwise * * @throws IOException * If an IO error occurs * @throws IllegalArgumentException * If the corresponding field can not be found. */ @Override public float get(String name, float defaultValue) throws IOException, IllegalArgumentException { return emulatedFields.get(name, defaultValue); }
throw new IllegalArgumentException("Attempting to write to a different stream than the one that created this PutField"); for (EmulatedFields.ObjectSlot slot : emulatedFields.slots()) { Object fieldValue = slot.getFieldValue(); Class<?> type = slot.getField().getType();
/** * 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. */ public EmulatedFields(ObjectStreamField[] fields, ObjectStreamField[] declared) { // We assume the slots are already sorted in the right shape for dumping buildSlots(fields); declaredFields = declared; }