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 EmulatedFieldsForLoading is an object that represents a set of emulated
022: * fields for an object being loaded. It is a concrete implementation for
023: * ObjectInputStream.GetField
024: *
025: * @see ObjectInputStream.GetField
026: * @see EmulatedFieldsForDumping
027: */
028: class EmulatedFieldsForLoading extends ObjectInputStream.GetField {
029:
030: // The class descriptor with the declared fields the receiver emulates
031: private ObjectStreamClass streamClass;
032:
033: // The actual representation, with a more powerful API (set&get)
034: private EmulatedFields emulatedFields;
035:
036: /**
037: * Constructs a new instance of EmulatedFieldsForDumping.
038: *
039: * @param streamClass
040: * an ObjectStreamClass, defining the class for which to emulate
041: * fields.
042: */
043: EmulatedFieldsForLoading(ObjectStreamClass streamClass) {
044: super ();
045: this .streamClass = streamClass;
046: emulatedFields = new EmulatedFields(
047: streamClass.getLoadFields(), streamClass.fields());
048: }
049:
050: /**
051: * Return a boolean indicating if the field named <code>name</code> has
052: * been assigned a value explicitly (false) or if it still holds a default
053: * value for the type (true) because it hasn't been assigned to yet.
054: *
055: * @param name
056: * A String, the name of the field to test
057: * @return <code>true</code> if the field holds it default value,
058: * <code>false</code> otherwise.
059: *
060: * @throws IOException
061: * If an IO error occurs
062: * @throws IllegalArgumentException
063: * If the corresponding field can not be found.
064: */
065: @Override
066: public boolean defaulted(String name) throws IOException,
067: IllegalArgumentException {
068: return emulatedFields.defaulted(name);
069: }
070:
071: /**
072: * Return the actual EmulatedFields instance used by the receiver. We have
073: * the actual work in a separate class so that the code can be shared. The
074: * receiver has to be of a subclass of GetField.
075: *
076: * @return array of ObjectSlot the receiver represents.
077: */
078: EmulatedFields emulatedFields() {
079: return emulatedFields;
080: }
081:
082: /**
083: * Find and return the byte value of a given field named <code>name</code>
084: * in the receiver. If the field has not been assigned any value yet, the
085: * default value <code>defaultValue</code> is returned instead.
086: *
087: * @param name
088: * A String, the name of the field to find
089: * @param defaultValue
090: * Return value in case the field has not been assigned to yet.
091: * @return the value of the given field if it has been assigned, or the
092: * default value otherwise
093: *
094: * @throws IOException
095: * If an IO error occurs
096: * @throws IllegalArgumentException
097: * If the corresponding field can not be found.
098: */
099: @Override
100: public byte get(String name, byte defaultValue) throws IOException,
101: IllegalArgumentException {
102: return emulatedFields.get(name, defaultValue);
103: }
104:
105: /**
106: * Find and return the char value of a given field named <code>name</code>
107: * in the receiver. If the field has not been assigned any value yet, the
108: * default value <code>defaultValue</code> is returned instead.
109: *
110: * @param name
111: * A String, the name of the field to find
112: * @param defaultValue
113: * Return value in case the field has not been assigned to yet.
114: * @return the value of the given field if it has been assigned, or the
115: * default value otherwise
116: *
117: * @throws IOException
118: * If an IO error occurs
119: * @throws IllegalArgumentException
120: * If the corresponding field can not be found.
121: */
122: @Override
123: public char get(String name, char defaultValue) throws IOException,
124: IllegalArgumentException {
125: return emulatedFields.get(name, defaultValue);
126: }
127:
128: /**
129: * Find and return the double value of a given field named <code>name</code>
130: * in the receiver. If the field has not been assigned any value yet, the
131: * default value <code>defaultValue</code> is returned instead.
132: *
133: * @param name
134: * A String, the name of the field to find
135: * @param defaultValue
136: * Return value in case the field has not been assigned to yet.
137: * @return the value of the given field if it has been assigned, or the
138: * default value otherwise
139: *
140: * @throws IOException
141: * If an IO error occurs
142: * @throws IllegalArgumentException
143: * If the corresponding field can not be found.
144: */
145: @Override
146: public double get(String name, double defaultValue)
147: throws IOException, IllegalArgumentException {
148: return emulatedFields.get(name, defaultValue);
149: }
150:
151: /**
152: * Find and return the float value of a given field named <code>name</code>
153: * in the receiver. If the field has not been assigned any value yet, the
154: * default value <code>defaultValue</code> is returned instead.
155: *
156: * @param name
157: * A String, the name of the field to find
158: * @param defaultValue
159: * Return value in case the field has not been assigned to yet.
160: * @return the value of the given field if it has been assigned, or the
161: * default value otherwise
162: *
163: * @throws IOException
164: * If an IO error occurs
165: * @throws IllegalArgumentException
166: * If the corresponding field can not be found.
167: */
168: @Override
169: public float get(String name, float defaultValue)
170: throws IOException, IllegalArgumentException {
171: return emulatedFields.get(name, defaultValue);
172: }
173:
174: /**
175: * Find and return the int value of a given field named <code>name</code>
176: * in the receiver. If the field has not been assigned any value yet, the
177: * default value <code>defaultValue</code> is returned instead.
178: *
179: * @param name
180: * A String, the name of the field to find
181: * @param defaultValue
182: * Return value in case the field has not been assigned to yet.
183: * @return the value of the given field if it has been assigned, or the
184: * default value otherwise
185: *
186: * @throws IOException
187: * If an IO error occurs
188: * @throws IllegalArgumentException
189: * If the corresponding field can not be found.
190: */
191: @Override
192: public int get(String name, int defaultValue) throws IOException,
193: IllegalArgumentException {
194: return emulatedFields.get(name, defaultValue);
195: }
196:
197: /**
198: * Find and return the long value of a given field named <code>name</code>
199: * in the receiver. If the field has not been assigned any value yet, the
200: * default value <code>defaultValue</code> is returned instead.
201: *
202: * @param name
203: * A String, the name of the field to find
204: * @param defaultValue
205: * Return value in case the field has not been assigned to yet.
206: * @return the value of the given field if it has been assigned, or the
207: * default value otherwise
208: *
209: * @throws IOException
210: * If an IO error occurs
211: * @throws IllegalArgumentException
212: * If the corresponding field can not be found.
213: */
214: @Override
215: public long get(String name, long defaultValue) throws IOException,
216: IllegalArgumentException {
217: return emulatedFields.get(name, defaultValue);
218: }
219:
220: /**
221: * Find and return the Object value of a given field named <code>name</code>
222: * in the receiver. If the field has not been assigned any value yet, the
223: * default value <code>defaultValue</code> is returned instead.
224: *
225: * @param name
226: * A String, the name of the field to find
227: * @param defaultValue
228: * Return value in case the field has not been assigned to yet.
229: * @return the value of the given field if it has been assigned, or the
230: * default value otherwise
231: *
232: * @throws IOException
233: * If an IO error occurs
234: * @throws IllegalArgumentException
235: * If the corresponding field can not be found.
236: */
237: @Override
238: public Object get(String name, Object defaultValue)
239: throws IOException, IllegalArgumentException {
240: return emulatedFields.get(name, defaultValue);
241: }
242:
243: /**
244: * Find and return the short value of a given field named <code>name</code>
245: * in the receiver. If the field has not been assigned any value yet, the
246: * default value <code>defaultValue</code> is returned instead.
247: *
248: * @param name
249: * A String, the name of the field to find
250: * @param defaultValue
251: * Return value in case the field has not been assigned to yet.
252: * @return the value of the given field if it has been assigned, or the
253: * default value otherwise
254: *
255: * @throws IOException
256: * If an IO error occurs
257: * @throws IllegalArgumentException
258: * If the corresponding field can not be found.
259: */
260: @Override
261: public short get(String name, short defaultValue)
262: throws IOException, IllegalArgumentException {
263: return emulatedFields.get(name, defaultValue);
264: }
265:
266: /**
267: * Find and return the boolean value of a given field named
268: * <code>name</code> in the receiver. If the field has not been assigned
269: * any value yet, the default value <code>defaultValue</code> is returned
270: * instead.
271: *
272: * @param name
273: * A String, the name of the field to find
274: * @param defaultValue
275: * Return value in case the field has not been assigned to yet.
276: * @return the value of the given field if it has been assigned, or the
277: * default value otherwise
278: *
279: * @throws IOException
280: * If an IO error occurs
281: * @throws IllegalArgumentException
282: * If the corresponding field can not be found.
283: */
284: @Override
285: public boolean get(String name, boolean defaultValue)
286: throws IOException, IllegalArgumentException {
287: return emulatedFields.get(name, defaultValue);
288: }
289:
290: /**
291: * Return the class descriptor for which the emulated fields are defined.
292: *
293: * @return ObjectStreamClass The class descriptor for which the emulated
294: * fields are defined.
295: */
296: @Override
297: public ObjectStreamClass getObjectStreamClass() {
298: return streamClass;
299: }
300: }
|