ObjectStreamField.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* ObjectStreamField.java -- Class used to store name and class of fields
  2. Copyright (C) 1998, 1999, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. import gnu.java.lang.reflect.TypeSignature;
  33. import java.lang.reflect.Field;
  34. import java.security.AccessController;
  35. import java.security.PrivilegedAction;
  36. /**
  37. * This class intends to describe the field of a class for the serialization
  38. * subsystem. Serializable fields in a serializable class can be explicitly
  39. * exported using an array of ObjectStreamFields.
  40. *
  41. * @author Tom Tromey (tromey@redhat.com)
  42. * @author Jeroen Frijters (jeroen@frijters.net)
  43. * @author Guilhem Lavaux (guilhem@kaffe.org)
  44. * @author Michael Koch (konqueror@gmx.de)
  45. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  46. */
  47. public class ObjectStreamField
  48. implements Comparable<Object>
  49. {
  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;
  57. Field field;
  58. ObjectStreamField (Field field)
  59. {
  60. this (field.getName(), field.getType());
  61. this.field = field;
  62. }
  63. /**
  64. * This constructor creates an ObjectStreamField instance
  65. * which represents a field named <code>name</code> and is
  66. * of the type <code>type</code>.
  67. *
  68. * @param name Name of the field to export.
  69. * @param type Type of the field in the concerned class.
  70. */
  71. public ObjectStreamField (String name, Class<?> type)
  72. {
  73. this (name, type, false);
  74. }
  75. /**
  76. * This constructor creates an ObjectStreamField instance
  77. * which represents a field named <code>name</code> and is
  78. * of the type <code>type</code>.
  79. *
  80. * @param name Name of the field to export.
  81. * @param type Type of the field in the concerned class.
  82. * @param unshared true if field will be unshared, false otherwise.
  83. */
  84. public ObjectStreamField (String name, Class<?> type, boolean unshared)
  85. {
  86. if (name == null)
  87. throw new NullPointerException();
  88. this.name = name;
  89. this.type = type;
  90. this.typename = TypeSignature.getEncodingOfClass(type);
  91. this.unshared = unshared;
  92. }
  93. /**
  94. * There are many cases you can not get java.lang.Class from typename
  95. * if your context class loader cannot load it, then use typename to
  96. * construct the field.
  97. *
  98. * @param name Name of the field to export.
  99. * @param typename The coded name of the type for this field.
  100. */
  101. ObjectStreamField (String name, String typename)
  102. {
  103. this.name = name;
  104. this.typename = typename;
  105. }
  106. void resolveType(ClassLoader loader)
  107. {
  108. try
  109. {
  110. type = TypeSignature.getClassForEncoding(typename, true, loader);
  111. }
  112. catch(ClassNotFoundException e)
  113. {
  114. }
  115. }
  116. /**
  117. * This method returns the name of the field represented by the
  118. * ObjectStreamField instance.
  119. *
  120. * @return A string containing the name of the field.
  121. */
  122. public String getName ()
  123. {
  124. return name;
  125. }
  126. /**
  127. * This method returns the class representing the type of the
  128. * field which is represented by this instance of ObjectStreamField.
  129. *
  130. * @return A class representing the type of the field.
  131. */
  132. public Class<?> getType ()
  133. {
  134. return type;
  135. }
  136. /**
  137. * This method returns the char encoded type of the field which
  138. * is represented by this instance of ObjectStreamField.
  139. *
  140. * @return A char representing the type of the field.
  141. */
  142. public char getTypeCode ()
  143. {
  144. return typename.charAt (0);
  145. }
  146. /**
  147. * This method returns a more explicit type name than
  148. * {@link #getTypeCode()} in the case the type is a real
  149. * class (and not a primitive).
  150. *
  151. * @return The name of the type (class name) if it is not a
  152. * primitive, in the other case null is returned.
  153. */
  154. public String getTypeString ()
  155. {
  156. // use intern()
  157. if (isPrimitive())
  158. return null;
  159. return typename.intern();
  160. }
  161. /**
  162. * This method returns the current offset of the field in
  163. * the serialization stream relatively to the other fields.
  164. * The offset is expressed in bytes.
  165. *
  166. * @return The offset of the field in bytes.
  167. * @see #setOffset(int)
  168. */
  169. public int getOffset ()
  170. {
  171. return offset;
  172. }
  173. /**
  174. * This method sets the current offset of the field.
  175. *
  176. * @param off The offset of the field in bytes.
  177. * @see #getOffset()
  178. */
  179. protected void setOffset (int off)
  180. {
  181. offset = off;
  182. }
  183. /**
  184. * This method returns whether the field represented by this object is
  185. * unshared or not.
  186. *
  187. * @return Tells if this field is unshared or not.
  188. */
  189. public boolean isUnshared ()
  190. {
  191. return unshared;
  192. }
  193. /**
  194. * This method returns true if the type of the field
  195. * represented by this instance is a primitive.
  196. *
  197. * @return true if the type is a primitive, false
  198. * in the other case.
  199. */
  200. public boolean isPrimitive ()
  201. {
  202. return typename.length() == 1;
  203. }
  204. /**
  205. * Compares this object to the given object.
  206. *
  207. * @param obj the object to compare to.
  208. *
  209. * @return -1, 0 or 1.
  210. */
  211. public int compareTo (Object obj)
  212. {
  213. ObjectStreamField f = (ObjectStreamField) obj;
  214. boolean this_is_primitive = isPrimitive ();
  215. boolean f_is_primitive = f.isPrimitive ();
  216. if (this_is_primitive && !f_is_primitive)
  217. return -1;
  218. if (!this_is_primitive && f_is_primitive)
  219. return 1;
  220. return getName ().compareTo (f.getName ());
  221. }
  222. /**
  223. * This method is specific to classpath's implementation and so has the default
  224. * access. It changes the state of this field to "persistent". It means that
  225. * the field should not be changed when the stream is read (if it is not
  226. * explicitly specified using serialPersistentFields).
  227. *
  228. * @param persistent True if the field is persistent, false in the
  229. * other cases.
  230. * @see #isPersistent()
  231. */
  232. void setPersistent(boolean persistent)
  233. {
  234. this.persistent = persistent;
  235. }
  236. /**
  237. * This method returns true if the field is marked as persistent.
  238. *
  239. * @return True if persistent, false in the other cases.
  240. * @see #setPersistent(boolean)
  241. */
  242. boolean isPersistent()
  243. {
  244. return persistent;
  245. }
  246. /**
  247. * This method is specific to classpath's implementation and so
  248. * has the default access. It changes the state of this field as
  249. * to be set by ObjectInputStream.
  250. *
  251. * @param toset True if this field should be set, false in the other
  252. * cases.
  253. * @see #isToSet()
  254. */
  255. void setToSet(boolean toset)
  256. {
  257. this.toset = toset;
  258. }
  259. /**
  260. * This method returns true if the field is marked as to be
  261. * set.
  262. *
  263. * @return True if it is to be set, false in the other cases.
  264. * @see #setToSet(boolean)
  265. */
  266. boolean isToSet()
  267. {
  268. return toset;
  269. }
  270. /**
  271. * This method searches for its field reference in the specified class
  272. * object. It requests privileges. If an error occurs the internal field
  273. * reference is not modified.
  274. *
  275. * @throws NoSuchFieldException if the field name does not exist in this class.
  276. * @throws SecurityException if there was an error requesting the privileges.
  277. */
  278. void lookupField(Class clazz) throws NoSuchFieldException, SecurityException
  279. {
  280. final Field f = clazz.getDeclaredField(name);
  281. AccessController.doPrivileged(new PrivilegedAction()
  282. {
  283. public Object run()
  284. {
  285. f.setAccessible(true);
  286. return null;
  287. }
  288. });
  289. this.field = f;
  290. }
  291. /**
  292. * This method check whether the field described by this
  293. * instance of ObjectStreamField is compatible with the
  294. * actual implementation of this field.
  295. *
  296. * @throws NullPointerException if this field does not exist
  297. * in the real class.
  298. * @throws InvalidClassException if the types are incompatible.
  299. */
  300. void checkFieldType() throws InvalidClassException
  301. {
  302. Class<?> ftype = field.getType();
  303. if (!ftype.isAssignableFrom(type))
  304. throw new InvalidClassException
  305. ("invalid field type for " + name +
  306. " in class " + field.getDeclaringClass());
  307. }
  308. /**
  309. * Returns a string representing this object.
  310. *
  311. * @return the string.
  312. */
  313. public String toString ()
  314. {
  315. return "ObjectStreamField< " + type + " " + name + " >";
  316. }
  317. final void setBooleanField(Object obj, boolean val)
  318. {
  319. VMObjectStreamClass.setBooleanNative(field, obj, val);
  320. }
  321. final void setByteField(Object obj, byte val)
  322. {
  323. VMObjectStreamClass.setByteNative(field, obj, val);
  324. }
  325. final void setCharField(Object obj, char val)
  326. {
  327. VMObjectStreamClass.setCharNative(field, obj, val);
  328. }
  329. final void setShortField(Object obj, short val)
  330. {
  331. VMObjectStreamClass.setShortNative(field, obj, val);
  332. }
  333. final void setIntField(Object obj, int val)
  334. {
  335. VMObjectStreamClass.setIntNative(field, obj, val);
  336. }
  337. final void setLongField(Object obj, long val)
  338. {
  339. VMObjectStreamClass.setLongNative(field, obj, val);
  340. }
  341. final void setFloatField(Object obj, float val)
  342. {
  343. VMObjectStreamClass.setFloatNative(field, obj, val);
  344. }
  345. final void setDoubleField(Object obj, double val)
  346. {
  347. VMObjectStreamClass.setDoubleNative(field, obj, val);
  348. }
  349. final void setObjectField(Object obj, Object val)
  350. {
  351. VMObjectStreamClass.setObjectNative(field, obj, val);
  352. }
  353. }