001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.io;
019:
020: /**
021: * An EmulatedFieldsForDumping is an object that represents a set of emulated
022: * fields for an object being dumped. It is a concrete implementation for
023: * ObjectOutputStream.PutField
024: *
025: *
026: * @see ObjectOutputStream.PutField
027: * @see EmulatedFieldsForLoading
028: */
029: class EmulatedFieldsForDumping extends ObjectOutputStream.PutField {
030:
031: // The actual representation, with a more powerful API (set&get)
032: private EmulatedFields emulatedFields;
033:
034: /**
035: * Constructs a new instance of EmulatedFieldsForDumping.
036: *
037: * @param streamClass
038: * a ObjectStreamClass, which describe the fields to be emulated
039: * (names, types, etc).
040: */
041: EmulatedFieldsForDumping(ObjectStreamClass streamClass) {
042: super ();
043: emulatedFields = new EmulatedFields(streamClass.fields(),
044: (ObjectStreamField[]) null);
045: }
046:
047: /**
048: * Return the actual EmulatedFields instance used by the receiver. We have
049: * the actual work in a separate class so that the code can be shared. The
050: * receiver has to be of a subclass of PutField.
051: *
052: * @return array of ObjectSlot the receiver represents.
053: */
054: EmulatedFields emulatedFields() {
055: return emulatedFields;
056: }
057:
058: /**
059: * Find and set the byte value of a given field named <code>name</code> in
060: * the receiver.
061: *
062: * @param name
063: * A String, the name of the field to set
064: * @param value
065: * New value for the field.
066: */
067: @Override
068: public void put(String name, byte value) {
069: emulatedFields.put(name, value);
070: }
071:
072: /**
073: * Find and set the char value of a given field named <code>name</code> in
074: * the receiver.
075: *
076: * @param name
077: * A String, the name of the field to set
078: * @param value
079: * New value for the field.
080: */
081: @Override
082: public void put(String name, char value) {
083: emulatedFields.put(name, value);
084: }
085:
086: /**
087: * Find and set the double value of a given field named <code>name</code>
088: * in the receiver.
089: *
090: * @param name
091: * A String, the name of the field to set
092: * @param value
093: * New value for the field.
094: */
095: @Override
096: public void put(String name, double value) {
097: emulatedFields.put(name, value);
098: }
099:
100: /**
101: * Find and set the float value of a given field named <code>name</code>
102: * in the receiver.
103: *
104: * @param name
105: * A String, the name of the field to set
106: * @param value
107: * New value for the field.
108: */
109: @Override
110: public void put(String name, float value) {
111: emulatedFields.put(name, value);
112: }
113:
114: /**
115: * Find and set the int value of a given field named <code>name</code> in
116: * the receiver.
117: *
118: * @param name
119: * A String, the name of the field to set
120: * @param value
121: * New value for the field.
122: */
123: @Override
124: public void put(String name, int value) {
125: emulatedFields.put(name, value);
126: }
127:
128: /**
129: * Find and set the long value of a given field named <code>name</code> in
130: * the receiver.
131: *
132: * @param name
133: * A String, the name of the field to set
134: * @param value
135: * New value for the field.
136: */
137: @Override
138: public void put(String name, long value) {
139: emulatedFields.put(name, value);
140: }
141:
142: /**
143: * Find and set the Object value of a given field named <code>name</code>
144: * in the receiver.
145: *
146: * @param name
147: * A String, the name of the field to set
148: * @param value
149: * New value for the field.
150: */
151: @Override
152: public void put(String name, Object value) {
153: emulatedFields.put(name, value);
154: }
155:
156: /**
157: * Find and set the short value of a given field named <code>name</code>
158: * in the receiver.
159: *
160: * @param name
161: * A String, the name of the field to set
162: * @param value
163: * New value for the field.
164: */
165: @Override
166: public void put(String name, short value) {
167: emulatedFields.put(name, value);
168: }
169:
170: /**
171: * Find and set the boolean value of a given field named <code>name</code>
172: * in the receiver.
173: *
174: * @param name
175: * A String, the name of the field to set
176: * @param value
177: * New value for the field.
178: */
179: @Override
180: public void put(String name, boolean value) {
181: emulatedFields.put(name, value);
182: }
183:
184: /**
185: * Write the field values to the specified ObjectOutput.
186: *
187: * @param output
188: * the ObjectOutput
189: *
190: * @throws IOException
191: * If an IO exception happened when writing the field values.
192: */
193: @Override
194: @Deprecated
195: @SuppressWarnings("deprecation")
196: public void write(ObjectOutput output) throws IOException {
197: EmulatedFields.ObjectSlot[] slots = emulatedFields.slots();
198: for (int i = 0; i < slots.length; i++) {
199: EmulatedFields.ObjectSlot slot = slots[i];
200: Object fieldValue = slot.getFieldValue();
201: Class<?> type = slot.getField().getType();
202: // WARNING - default values exist for each primitive type
203: if (type == Integer.TYPE) {
204: output
205: .writeInt(fieldValue != null ? ((Integer) fieldValue)
206: .intValue()
207: : 0);
208: } else if (type == Byte.TYPE) {
209: output
210: .writeByte(fieldValue != null ? ((Byte) fieldValue)
211: .byteValue()
212: : (byte) 0);
213: } else if (type == Character.TYPE) {
214: output
215: .writeChar(fieldValue != null ? ((Character) fieldValue)
216: .charValue()
217: : (char) 0);
218: } else if (type == Short.TYPE) {
219: output
220: .writeShort(fieldValue != null ? ((Short) fieldValue)
221: .shortValue()
222: : (short) 0);
223: } else if (type == Boolean.TYPE) {
224: output
225: .writeBoolean(fieldValue != null ? ((Boolean) fieldValue)
226: .booleanValue()
227: : false);
228: } else if (type == Long.TYPE) {
229: output
230: .writeLong(fieldValue != null ? ((Long) fieldValue)
231: .longValue()
232: : (long) 0);
233: } else if (type == Float.TYPE) {
234: output
235: .writeFloat(fieldValue != null ? ((Float) fieldValue)
236: .floatValue()
237: : (float) 0);
238: } else if (type == Double.TYPE) {
239: output
240: .writeDouble(fieldValue != null ? ((Double) fieldValue)
241: .doubleValue()
242: : (double) 0);
243: } else {
244: // Either array or Object
245: output.writeObject(fieldValue);
246: }
247: }
248: }
249: }
|