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)
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
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
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. */
41 import gnu
.java
.lang
.reflect
.TypeSignature
;
44 * This class intends to describe the field of a class for the serialization
45 * subsystem. Serializable fields in a serializable class can be explicitly
46 * exported using an array of ObjectStreamFields.
48 public class ObjectStreamField
implements Comparable
52 private String typename
;
53 private int offset
= -1; // XXX make sure this is correct
54 private boolean unshared
;
55 private boolean persistent
= false;
56 private boolean toset
= true;
59 * This constructor creates an ObjectStreamField instance
60 * which represents a field named <code>name</code> and is
61 * of the type <code>type</code>.
63 * @param name Name of the field to export.
64 * @param type Type of the field in the concerned class.
66 public ObjectStreamField (String name
, Class type
)
68 this (name
, type
, false);
72 * This constructor creates an ObjectStreamField instance
73 * which represents a field named <code>name</code> and is
74 * of the type <code>type</code>.
76 * @param name Name of the field to export.
77 * @param type Type of the field in the concerned class.
79 public ObjectStreamField (String name
, Class type
, boolean unshared
)
82 throw new NullPointerException();
86 this.typename
= TypeSignature
.getEncodingOfClass(type
);
87 this.unshared
= unshared
;
91 * There are many cases you can not get java.lang.Class from typename
92 * if your context class loader cann not load it, then use typename to
93 * construct the field.
95 * @param name Name of the field to export.
96 * @param typename The coded name of the type for this field.
98 ObjectStreamField (String name
, String typename
)
101 this.typename
= typename
;
104 type
= TypeSignature
.getClassForEncoding(typename
);
106 catch(ClassNotFoundException e
)
108 type
= Object
.class; //FIXME: ???
113 * There are many cases you can not get java.lang.Class from typename
114 * if your context class loader cann not load it, then use typename to
115 * construct the field.
117 * @param name Name of the field to export.
118 * @param typename The coded name of the type for this field.
119 * @param loader The class loader to use to resolve class names.
121 ObjectStreamField (String name
, String typename
, ClassLoader loader
)
124 this.typename
= typename
;
127 type
= TypeSignature
.getClassForEncoding(typename
, true, loader
);
129 catch(ClassNotFoundException e
)
131 type
= Object
.class; // ALSO FIXME
136 * This method returns the name of the field represented by the
137 * ObjectStreamField instance.
139 * @return A string containing the name of the field.
141 public String
getName ()
147 * This method returns the class representing the type of the
148 * field which is represented by this instance of ObjectStreamField.
150 * @return A class representing the type of the field.
152 public Class
getType ()
158 * This method returns the char encoded type of the field which
159 * is represented by this instance of ObjectStreamField.
161 * @return A char representing the type of the field.
163 public char getTypeCode ()
165 return typename
.charAt (0);
169 * This method returns a more explicit type name than
170 * {@link #getTypeCode()} in the case the type is a real
171 * class (and not a primitive).
173 * @return The name of the type (class name) if it is not a
174 * primitive, in the other case null is returned.
176 public String
getTypeString ()
179 if (this.type
.isPrimitive())
181 return typename
.intern();
185 * This method returns the current offset of the field in
186 * the serialization stream relatively to the other fields.
187 * The offset is expressed in bytes.
189 * @return The offset of the field in bytes.
190 * @see #setOffset(int)
192 public int getOffset ()
198 * This method sets the current offset of the field.
200 * @param off The offset of the field in bytes.
203 protected void setOffset (int off
)
209 * This method returns whether the field represented by this object is
212 * @return Tells if this field is unshared or not.
214 public boolean isUnshared ()
220 * This method returns true if the type of the field
221 * represented by this instance is a primitive.
223 * @return true if the type is a primitive, false
226 public boolean isPrimitive ()
228 return type
.isPrimitive ();
231 public int compareTo (Object o
)
233 ObjectStreamField f
= (ObjectStreamField
)o
;
234 boolean this_is_primitive
= isPrimitive ();
235 boolean f_is_primitive
= f
.isPrimitive ();
237 if (this_is_primitive
&& !f_is_primitive
)
240 if (!this_is_primitive
&& f_is_primitive
)
243 return getName ().compareTo (f
.getName ());
247 * This method is specific to classpath's implementation and so has the default
248 * access. It changes the state of this field to "persistent". It means that
249 * the field should not be changed when the stream is read (if it is not
250 * explicitly specified using serialPersistentFields).
252 * @param persistent True if the field is persistent, false in the
254 * @see #isPersistent()
256 void setPersistent(boolean persistent
)
258 this.persistent
= persistent
;
262 * This method returns true if the field is marked as persistent.
264 * @return True if persistent, false in the other cases.
265 * @see #setPersistent(boolean)
267 boolean isPersistent()
273 * This method is specific to classpath's implementation and so
274 * has the default access. It changes the state of this field as
275 * to be set by ObjectInputStream.
277 * @param toset True if this field should be set, false in the other
281 void setToSet(boolean toset
)
287 * This methods returns true if the field is marked as to be
290 * @return True if it is to be set, false in the other cases.
291 * @see #setToSet(boolean)
298 public String
toString ()
300 return "ObjectStreamField< " + type
+ " " + name
+ " >";