fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / libjava / java / io / ObjectStreamField.java
blobcb2fdbc0d250b62846227f3048a30d30c64f78ba
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 gnu.java.lang.reflect.TypeSignature;
43 /**
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
50 private String name;
51 private Class type;
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;
58 /**
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);
71 /**
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)
81 if (name == null)
82 throw new NullPointerException();
84 this.name = name;
85 this.type = type;
86 this.typename = TypeSignature.getEncodingOfClass(type);
87 this.unshared = unshared;
90 /**
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)
100 this.name = name;
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)
123 this.name = name;
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 ()
143 return name;
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 ()
154 return type;
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 ()
178 // use intern()
179 if (this.type.isPrimitive())
180 return null;
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 ()
194 return offset;
198 * This method sets the current offset of the field.
200 * @param off The offset of the field in bytes.
201 * @see getOffset()
203 protected void setOffset (int off)
205 offset = off;
209 * This method returns whether the field represented by this object is
210 * unshared or not.
212 * @return Tells if this field is unshared or not.
214 public boolean isUnshared ()
216 return unshared;
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
224 * in the other case.
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)
238 return -1;
240 if (!this_is_primitive && f_is_primitive)
241 return 1;
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
253 * other cases.
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()
269 return persistent;
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
278 * cases.
279 * @see #isToSet()
281 void setToSet(boolean toset)
283 this.toset = toset;
287 * This methods returns true if the field is marked as to be
288 * set.
290 * @return True if it is to be set, false in the other cases.
291 * @see #setToSet(boolean)
293 boolean isToSet()
295 return toset;
298 public String toString ()
300 return "ObjectStreamField< " + type + " " + name + " >";