2004-02-06 Jeroen Frijters <jeroen@frijters.net>
[official-gcc.git] / libjava / java / io / ObjectStreamField.java
blob603fe30917d2581978edc414739a8d10f8d73fe0
1 /* ObjectStreamField.java -- Class used to store name and class of fields
2 Copyright (C) 1998, 1999, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.io;
41 import java.lang.reflect.Field;
42 import java.lang.reflect.Modifier;
43 import gnu.java.lang.reflect.TypeSignature;
45 /**
46 * This class intends to describe the field of a class for the serialization
47 * subsystem. Serializable fields in a serializable class can be explicitly
48 * exported using an array of ObjectStreamFields.
50 public class ObjectStreamField implements Comparable
52 private String name;
53 private Class type;
54 private String typename;
55 private int offset = -1; // XXX make sure this is correct
56 private boolean unshared;
57 private boolean persistent = false;
58 private boolean toset = true;
59 private Field field;
61 ObjectStreamField (Field field)
63 this (field.getName(), field.getType());
64 this.field = field;
65 toset = !Modifier.isFinal(field.getModifiers());
68 /**
69 * This constructor creates an ObjectStreamField instance
70 * which represents a field named <code>name</code> and is
71 * of the type <code>type</code>.
73 * @param name Name of the field to export.
74 * @param type Type of the field in the concerned class.
76 public ObjectStreamField (String name, Class type)
78 this (name, type, false);
81 /**
82 * This constructor creates an ObjectStreamField instance
83 * which represents a field named <code>name</code> and is
84 * of the type <code>type</code>.
86 * @param name Name of the field to export.
87 * @param type Type of the field in the concerned class.
89 public ObjectStreamField (String name, Class type, boolean unshared)
91 if (name == null)
92 throw new NullPointerException();
94 this.name = name;
95 this.type = type;
96 this.typename = TypeSignature.getEncodingOfClass(type);
97 this.unshared = unshared;
101 * There are many cases you can not get java.lang.Class from typename
102 * if your context class loader cann not load it, then use typename to
103 * construct the field.
105 * @param name Name of the field to export.
106 * @param typename The coded name of the type for this field.
108 ObjectStreamField (String name, String typename)
110 this.name = name;
111 this.typename = typename;
114 type = TypeSignature.getClassForEncoding(typename);
116 catch(ClassNotFoundException e)
122 * There are many cases you can not get java.lang.Class from typename
123 * if your context class loader cann not load it, then use typename to
124 * construct the field.
126 * @param name Name of the field to export.
127 * @param typename The coded name of the type for this field.
128 * @param loader The class loader to use to resolve class names.
130 ObjectStreamField (String name, String typename, ClassLoader loader)
132 this.name = name;
133 this.typename = typename;
136 type = TypeSignature.getClassForEncoding(typename, true, loader);
138 catch(ClassNotFoundException e)
144 * This method returns the name of the field represented by the
145 * ObjectStreamField instance.
147 * @return A string containing the name of the field.
149 public String getName ()
151 return name;
155 * This method returns the class representing the type of the
156 * field which is represented by this instance of ObjectStreamField.
158 * @return A class representing the type of the field.
160 public Class getType ()
162 return type;
166 * This method returns the char encoded type of the field which
167 * is represented by this instance of ObjectStreamField.
169 * @return A char representing the type of the field.
171 public char getTypeCode ()
173 return typename.charAt (0);
177 * This method returns a more explicit type name than
178 * {@link #getTypeCode()} in the case the type is a real
179 * class (and not a primitive).
181 * @return The name of the type (class name) if it is not a
182 * primitive, in the other case null is returned.
184 public String getTypeString ()
186 // use intern()
187 if (isPrimitive())
188 return null;
189 return typename.intern();
193 * This method returns the current offset of the field in
194 * the serialization stream relatively to the other fields.
195 * The offset is expressed in bytes.
197 * @return The offset of the field in bytes.
198 * @see #setOffset(int)
200 public int getOffset ()
202 return offset;
206 * This method sets the current offset of the field.
208 * @param off The offset of the field in bytes.
209 * @see getOffset()
211 protected void setOffset (int off)
213 offset = off;
217 * This method returns whether the field represented by this object is
218 * unshared or not.
220 * @return Tells if this field is unshared or not.
222 public boolean isUnshared ()
224 return unshared;
228 * This method returns true if the type of the field
229 * represented by this instance is a primitive.
231 * @return true if the type is a primitive, false
232 * in the other case.
234 public boolean isPrimitive ()
236 return typename.length() == 1;
239 public int compareTo (Object o)
241 ObjectStreamField f = (ObjectStreamField)o;
242 boolean this_is_primitive = isPrimitive ();
243 boolean f_is_primitive = f.isPrimitive ();
245 if (this_is_primitive && !f_is_primitive)
246 return -1;
248 if (!this_is_primitive && f_is_primitive)
249 return 1;
251 return getName ().compareTo (f.getName ());
255 * This method is specific to classpath's implementation and so has the default
256 * access. It changes the state of this field to "persistent". It means that
257 * the field should not be changed when the stream is read (if it is not
258 * explicitly specified using serialPersistentFields).
260 * @param persistent True if the field is persistent, false in the
261 * other cases.
262 * @see #isPersistent()
264 void setPersistent(boolean persistent)
266 this.persistent = persistent;
270 * This method returns true if the field is marked as persistent.
272 * @return True if persistent, false in the other cases.
273 * @see #setPersistent(boolean)
275 boolean isPersistent()
277 return persistent;
281 * This method is specific to classpath's implementation and so
282 * has the default access. It changes the state of this field as
283 * to be set by ObjectInputStream.
285 * @param toset True if this field should be set, false in the other
286 * cases.
287 * @see #isToSet()
289 void setToSet(boolean toset)
291 this.toset = toset;
295 * This methods returns true if the field is marked as to be
296 * set.
298 * @return True if it is to be set, false in the other cases.
299 * @see #setToSet(boolean)
301 boolean isToSet()
303 return toset;
306 public String toString ()
308 return "ObjectStreamField< " + type + " " + name + " >";
311 final void setBooleanField(Object obj, boolean val)
315 field.setBoolean(obj, val);
317 catch(IllegalAccessException x)
319 throw new InternalError(x.getMessage());
323 final void setByteField(Object obj, byte val)
327 field.setByte(obj, val);
329 catch(IllegalAccessException x)
331 throw new InternalError(x.getMessage());
335 final void setCharField(Object obj, char val)
339 field.setChar(obj, val);
341 catch(IllegalAccessException x)
343 throw new InternalError(x.getMessage());
347 final void setShortField(Object obj, short val)
351 field.setShort(obj, val);
353 catch(IllegalAccessException x)
355 throw new InternalError(x.getMessage());
359 final void setIntField(Object obj, int val)
363 field.setInt(obj, val);
365 catch(IllegalAccessException x)
367 throw new InternalError(x.getMessage());
371 final void setLongField(Object obj, long val)
375 field.setLong(obj, val);
377 catch(IllegalAccessException x)
379 throw new InternalError(x.getMessage());
383 final void setFloatField(Object obj, float val)
387 field.setFloat(obj, val);
389 catch(IllegalAccessException x)
391 throw new InternalError(x.getMessage());
395 final void setDoubleField(Object obj, double val)
399 field.setDouble(obj, val);
401 catch(IllegalAccessException x)
403 throw new InternalError(x.getMessage());
407 final void setObjectField(Object obj, Object val)
411 field.set(obj, val);
413 catch(IllegalAccessException x)
415 throw new InternalError(x.getMessage());