1. /*
  2. * @(#)ObjectStreamField.java 1.24 04/04/07
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * Licensed Materials - Property of IBM
  9. * RMI-IIOP v1.0
  10. * Copyright IBM Corp. 1998 1999 All Rights Reserved
  11. *
  12. * US Government Users Restricted Rights - Use, duplication or
  13. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  14. */
  15. package com.sun.corba.se.impl.io;
  16. import java.lang.reflect.Field;
  17. import java.lang.Comparable;
  18. import java.util.Hashtable;
  19. import sun.corba.Bridge ;
  20. import java.security.AccessController ;
  21. import java.security.PrivilegedAction ;
  22. /**
  23. * A description of a field in a serializable class.
  24. * A array of these is used to declare the persistent fields of
  25. * a class.
  26. *
  27. */
  28. public class ObjectStreamField implements Comparable
  29. {
  30. private static final Bridge bridge =
  31. (Bridge)AccessController.doPrivileged(
  32. new PrivilegedAction() {
  33. public Object run() {
  34. return Bridge.get() ;
  35. }
  36. }
  37. ) ;
  38. /**
  39. * Create a named field with the specified type.
  40. */
  41. ObjectStreamField(String n, Class clazz) {
  42. name = n;
  43. this.clazz = clazz;
  44. // Compute the typecode for easy switching
  45. if (clazz.isPrimitive()) {
  46. if (clazz == Integer.TYPE) {
  47. type = 'I';
  48. } else if (clazz == Byte.TYPE) {
  49. type = 'B';
  50. } else if (clazz == Long.TYPE) {
  51. type = 'J';
  52. } else if (clazz == Float.TYPE) {
  53. type = 'F';
  54. } else if (clazz == Double.TYPE) {
  55. type = 'D';
  56. } else if (clazz == Short.TYPE) {
  57. type = 'S';
  58. } else if (clazz == Character.TYPE) {
  59. type = 'C';
  60. } else if (clazz == Boolean.TYPE) {
  61. type = 'Z';
  62. }
  63. } else if (clazz.isArray()) {
  64. type = '[';
  65. typeString = ObjectStreamClass.getSignature(clazz);
  66. } else {
  67. type = 'L';
  68. typeString = ObjectStreamClass.getSignature(clazz);
  69. }
  70. if (typeString != null)
  71. signature = typeString;
  72. else
  73. signature = String.valueOf(type);
  74. }
  75. ObjectStreamField(Field field) {
  76. this(field.getName(), field.getType());
  77. setField( field ) ;
  78. }
  79. /**
  80. * Create an ObjectStreamField containing a reflected Field.
  81. */
  82. ObjectStreamField(String n, char t, Field f, String ts)
  83. {
  84. name = n;
  85. type = t;
  86. setField( f ) ;
  87. typeString = ts;
  88. if (typeString != null)
  89. signature = typeString;
  90. else
  91. signature = String.valueOf(type);
  92. }
  93. /**
  94. * Get the name of this field.
  95. */
  96. public String getName() {
  97. return name;
  98. }
  99. /**
  100. * Get the type of the field.
  101. */
  102. public Class getType() {
  103. if (clazz != null)
  104. return clazz;
  105. switch (type) {
  106. case 'B': clazz = Byte.TYPE;
  107. break;
  108. case 'C': clazz = Character.TYPE;
  109. break;
  110. case 'S': clazz = Short.TYPE;
  111. break;
  112. case 'I': clazz = Integer.TYPE;
  113. break;
  114. case 'J': clazz = Long.TYPE;
  115. break;
  116. case 'F': clazz = Float.TYPE;
  117. break;
  118. case 'D': clazz = Double.TYPE;
  119. break;
  120. case 'Z': clazz = Boolean.TYPE;
  121. break;
  122. case '[':
  123. case 'L':
  124. clazz = Object.class;
  125. break;
  126. }
  127. return clazz;
  128. }
  129. public char getTypeCode() {
  130. return type;
  131. }
  132. public String getTypeString() {
  133. return typeString;
  134. }
  135. Field getField() {
  136. return field;
  137. }
  138. void setField(Field field) {
  139. this.field = field;
  140. this.fieldID = bridge.objectFieldOffset( field ) ;
  141. }
  142. /*
  143. * Default constructor creates an empty field.
  144. * Usually used just to get to the sort functions.
  145. */
  146. ObjectStreamField() {
  147. }
  148. /**
  149. * test if this field is a primitive or not.
  150. */
  151. public boolean isPrimitive() {
  152. return (type != '[' && type != 'L');
  153. }
  154. /**
  155. * Compare this with another ObjectStreamField.
  156. * return -1 if this is smaller, 0 if equal, 1 if greater
  157. * types that are primitives are "smaller" than objects.
  158. * if equal, the names are compared.
  159. */
  160. public int compareTo(Object o) {
  161. ObjectStreamField f2 = (ObjectStreamField)o;
  162. boolean thisprim = (this.typeString == null);
  163. boolean otherprim = (f2.typeString == null);
  164. if (thisprim != otherprim) {
  165. return (thisprim ? -1 : 1);
  166. }
  167. return this.name.compareTo(f2.name);
  168. }
  169. /**
  170. * Compare the types of two class descriptors.
  171. * The match if they have the same primitive types.
  172. * or if they are both objects and the object types match.
  173. */
  174. public boolean typeEquals(ObjectStreamField other) {
  175. if (other == null || type != other.type)
  176. return false;
  177. /* Return true if the primitive types matched */
  178. if (typeString == null && other.typeString == null)
  179. return true;
  180. return ObjectStreamClass.compareClassNames(typeString,
  181. other.typeString,
  182. '/');
  183. }
  184. /* Returns the signature of the Field.
  185. *
  186. */
  187. public String getSignature() {
  188. return signature;
  189. }
  190. /**
  191. * Return a string describing this field.
  192. */
  193. public String toString() {
  194. if (typeString != null)
  195. return typeString + " " + name;
  196. else
  197. return type + " " + name;
  198. }
  199. public Class getClazz() {
  200. return clazz;
  201. }
  202. /* Returns the Field ID
  203. *
  204. */
  205. public long getFieldID() {
  206. return fieldID ;
  207. }
  208. private String name; // the name of the field
  209. private char type; // type first byte of the type signature
  210. private Field field; // Reflected field
  211. private String typeString; // iff object, typename
  212. private Class clazz; // the type of this field, if has been resolved
  213. // the next 2 things are RMI-IIOP specific, it can be easily
  214. // removed, if we can figure out all place where there are dependencies
  215. // to this. Signature is esentially equal to typestring. Then
  216. // essentially we can use the java.io.ObjectStreamField as such.
  217. private String signature; // the signature of the field
  218. private long fieldID = Bridge.INVALID_FIELD_OFFSET ;
  219. }