Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / java / io / ObjectStreamField.java
blob61ccdc7db768f827cda54413b68255bc906f75bb
1 /* ObjectStreamField.java -- Class used to store name and class of fields
2 Copyright (C) 1998, 1999, 2003, 2004 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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 import java.lang.reflect.Field;
44 import java.security.AccessController;
45 import java.security.PrivilegedAction;
47 /**
48 * This class intends to describe the field of a class for the serialization
49 * subsystem. Serializable fields in a serializable class can be explicitly
50 * exported using an array of ObjectStreamFields.
52 public class ObjectStreamField implements Comparable
54 private String name;
55 private Class type;
56 private String typename;
57 private int offset = -1; // XXX make sure this is correct
58 private boolean unshared;
59 private boolean persistent = false;
60 private boolean toset = true;
61 private Field field;
63 ObjectStreamField (Field field)
65 this (field.getName(), field.getType());
66 this.field = field;
69 /**
70 * This constructor creates an ObjectStreamField instance
71 * which represents a field named <code>name</code> and is
72 * of the type <code>type</code>.
74 * @param name Name of the field to export.
75 * @param type Type of the field in the concerned class.
77 public ObjectStreamField (String name, Class type)
79 this (name, type, false);
82 /**
83 * This constructor creates an ObjectStreamField instance
84 * which represents a field named <code>name</code> and is
85 * of the type <code>type</code>.
87 * @param name Name of the field to export.
88 * @param type Type of the field in the concerned class.
89 * @param unshared true if field will be unshared, false otherwise.
91 public ObjectStreamField (String name, Class type, boolean unshared)
93 if (name == null)
94 throw new NullPointerException();
96 this.name = name;
97 this.type = type;
98 this.typename = TypeSignature.getEncodingOfClass(type);
99 this.unshared = unshared;
103 * There are many cases you can not get java.lang.Class from typename
104 * if your context class loader cannot load it, then use typename to
105 * construct the field.
107 * @param name Name of the field to export.
108 * @param typename The coded name of the type for this field.
110 ObjectStreamField (String name, String typename)
112 this.name = name;
113 this.typename = typename;
116 type = TypeSignature.getClassForEncoding(typename);
118 catch(ClassNotFoundException e)
124 * There are many cases you can not get java.lang.Class from typename
125 * if your context class loader cann not load it, then use typename to
126 * construct the field.
128 * @param name Name of the field to export.
129 * @param typename The coded name of the type for this field.
130 * @param loader The class loader to use to resolve class names.
132 ObjectStreamField (String name, String typename, ClassLoader loader)
134 this.name = name;
135 this.typename = typename;
138 type = TypeSignature.getClassForEncoding(typename, true, loader);
140 catch(ClassNotFoundException e)
146 * This method returns the name of the field represented by the
147 * ObjectStreamField instance.
149 * @return A string containing the name of the field.
151 public String getName ()
153 return name;
157 * This method returns the class representing the type of the
158 * field which is represented by this instance of ObjectStreamField.
160 * @return A class representing the type of the field.
162 public Class getType ()
164 return type;
168 * This method returns the char encoded type of the field which
169 * is represented by this instance of ObjectStreamField.
171 * @return A char representing the type of the field.
173 public char getTypeCode ()
175 return typename.charAt (0);
179 * This method returns a more explicit type name than
180 * {@link #getTypeCode()} in the case the type is a real
181 * class (and not a primitive).
183 * @return The name of the type (class name) if it is not a
184 * primitive, in the other case null is returned.
186 public String getTypeString ()
188 // use intern()
189 if (isPrimitive())
190 return null;
191 return typename.intern();
195 * This method returns the current offset of the field in
196 * the serialization stream relatively to the other fields.
197 * The offset is expressed in bytes.
199 * @return The offset of the field in bytes.
200 * @see #setOffset(int)
202 public int getOffset ()
204 return offset;
208 * This method sets the current offset of the field.
210 * @param off The offset of the field in bytes.
211 * @see #getOffset()
213 protected void setOffset (int off)
215 offset = off;
219 * This method returns whether the field represented by this object is
220 * unshared or not.
222 * @return Tells if this field is unshared or not.
224 public boolean isUnshared ()
226 return unshared;
230 * This method returns true if the type of the field
231 * represented by this instance is a primitive.
233 * @return true if the type is a primitive, false
234 * in the other case.
236 public boolean isPrimitive ()
238 return typename.length() == 1;
242 * Compares this object to the given object.
244 * @param obj the object to compare to.
246 * @return -1, 0 or 1.
248 public int compareTo (Object obj)
250 ObjectStreamField f = (ObjectStreamField) obj;
251 boolean this_is_primitive = isPrimitive ();
252 boolean f_is_primitive = f.isPrimitive ();
254 if (this_is_primitive && !f_is_primitive)
255 return -1;
257 if (!this_is_primitive && f_is_primitive)
258 return 1;
260 return getName ().compareTo (f.getName ());
264 * This method is specific to classpath's implementation and so has the default
265 * access. It changes the state of this field to "persistent". It means that
266 * the field should not be changed when the stream is read (if it is not
267 * explicitly specified using serialPersistentFields).
269 * @param persistent True if the field is persistent, false in the
270 * other cases.
271 * @see #isPersistent()
273 void setPersistent(boolean persistent)
275 this.persistent = persistent;
279 * This method returns true if the field is marked as persistent.
281 * @return True if persistent, false in the other cases.
282 * @see #setPersistent(boolean)
284 boolean isPersistent()
286 return persistent;
290 * This method is specific to classpath's implementation and so
291 * has the default access. It changes the state of this field as
292 * to be set by ObjectInputStream.
294 * @param toset True if this field should be set, false in the other
295 * cases.
296 * @see #isToSet()
298 void setToSet(boolean toset)
300 this.toset = toset;
304 * This method returns true if the field is marked as to be
305 * set.
307 * @return True if it is to be set, false in the other cases.
308 * @see #setToSet(boolean)
310 boolean isToSet()
312 return toset;
316 * This method searches for its field reference in the specified class
317 * object. It requests privileges. If an error occurs the internal field
318 * reference is not modified.
320 * @throws NoSuchFieldException if the field name does not exist in this class.
321 * @throws SecurityException if there was an error requesting the privileges.
323 void lookupField(Class clazz) throws NoSuchFieldException, SecurityException
325 final Field f = clazz.getDeclaredField(name);
327 AccessController.doPrivileged(new PrivilegedAction()
329 public Object run()
331 f.setAccessible(true);
332 return null;
336 this.field = f;
340 * This method check whether the field described by this
341 * instance of ObjectStreamField is compatible with the
342 * actual implementation of this field.
344 * @throws NullPointerException if this field does not exist
345 * in the real class.
346 * @throws InvalidClassException if the types are incompatible.
348 void checkFieldType() throws InvalidClassException
350 Class ftype = field.getType();
352 if (!ftype.isAssignableFrom(type))
353 throw new InvalidClassException
354 ("invalid field type for " + name +
355 " in class " + field.getDeclaringClass());
359 * Returns a string representing this object.
361 * @return the string.
363 public String toString ()
365 return "ObjectStreamField< " + type + " " + name + " >";
368 final void setBooleanField(Object obj, boolean val)
370 VMObjectStreamClass.setBooleanNative(field, obj, val);
373 final void setByteField(Object obj, byte val)
375 VMObjectStreamClass.setByteNative(field, obj, val);
378 final void setCharField(Object obj, char val)
380 VMObjectStreamClass.setCharNative(field, obj, val);
383 final void setShortField(Object obj, short val)
385 VMObjectStreamClass.setShortNative(field, obj, val);
388 final void setIntField(Object obj, int val)
390 VMObjectStreamClass.setIntNative(field, obj, val);
393 final void setLongField(Object obj, long val)
395 VMObjectStreamClass.setLongNative(field, obj, val);
398 final void setFloatField(Object obj, float val)
400 VMObjectStreamClass.setFloatNative(field, obj, val);
403 final void setDoubleField(Object obj, double val)
405 VMObjectStreamClass.setDoubleNative(field, obj, val);
408 final void setObjectField(Object obj, Object val)
410 VMObjectStreamClass.setObjectNative(field, obj, val);